Skip to content
On this page

luogo p1101 单词方阵

解析

明显是枚举思想:把所有的可能性都试一遍

- 遍历每个字符

  • 如果字符是ty,尝试八个方向
  • 使用一个istrue二维数据来保存是不是单词

代码

c
#include <cstdio>

#define N 110

bool istrue[N][N] = {0};
char map[N][N];
char word[] = "yizhong";
int len = 7;

int n;

bool inmap(int x,int y){
    if( x>=1 && x<=n && y >=1 && y <=n)
        return true;
    return false;
}

//8个方向
int fx[][2]  = {
    {0,-1}, //上    0
    {1,-1}, //右上  1
    {1,0}, //右     2
    {1,1},//右下    3
    {0,1},//下      4
    {-1,1},//左下   5
    {-1,0},//左     6
    {-1,-1},//左上  7
};

//某一方向去检查
bool check_fx(int x,int y,int f){
    int i;
    for(i=0;i<len;i++){
        int tx = x+fx[f][0]*i;
        int ty = y+fx[f][1]*i;
        if( !inmap(tx,ty) || map[tx][ty] != word[i])
            return false;
    }

    //set true
    for(i=0;i<len;i++){
        int tx = x+fx[f][0]*i;
        int ty = y+fx[f][1]*i;
        istrue[tx][ty] = 1;
    }
    return true;
}

void check(int x,int y){
    int i,j;
    for (i=0;i<=7;i++){ //八个方向
        check_fx(x,y,i);
    }
}

int main(){
    scanf("%d",&n);
    int i,j;
    for(i=1;i<=n;i++){ //读取数据
        scanf("%s",map[i]+1);
    }

    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++)
        {
            if( map[i][j] == 'y'){
                check(i,j); //从i,j开始检查
            }
        }
    }

    //输出
    for(i=1;i<=n;i++){
        for (j=1;j<=n;j++){
            if(istrue[i][j])
                printf("%c",map[i][j]);
            else
                printf("*");
        }
        printf("\n");
    }


    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85