File size: 1,952 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;

int r, c;
char graph[105][105];
string sample = "ALLIZZWELL";
bool checker = 0, visit[109][109];
char test[20];

void dfs(int row, int col, int indx)

{
    if(indx > 9) return;
    if(row < 0 || col < 0 || row >= r || col >= c) {
        return;
    }
    if(visit[row][col]) {
        //cout << 5 << endl;
        return;
    }

    test[indx] = graph[row][col];
    if(test[indx] != sample[indx]) {
        return;
    }

    visit[row][col] = 1;
    //cout << test[indx] << endl
    if(indx == 9 && test[9] == sample[9]) {
        checker = 1;
    }
    if(checker)
        return;

    dfs(row - 1, col - 1, indx + 1);  dfs(row - 1, col, indx + 1);  dfs(row - 1, col + 1, indx + 1);
    dfs(row, col -1, indx + 1);  dfs(row, col + 1, indx + 1);
    dfs(row + 1, col - 1, indx + 1);  dfs(row + 1, col, indx + 1);   dfs(row + 1, col + 1, indx + 1);
    visit[row][col] = 0;
}

int main()

{
    int t;
    scanf("%d", &t);
    while(t--) {
        scanf("%d %d", &r, &c);

        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                scanf(" %c", &graph[i][j]);
            }
        }

        checker = 0;
        memset(visit, 0, sizeof(visit));

        for(int i = 0; i < r; i++) {
            for(int j = 0; j < c; j++) {
                if(graph[i][j] == 'A') {
                    test[0] = graph[i][j];
                    dfs(i, j, 0);

                    for(int k = 0; k < r; k++) {
                        for(int l = 0; l < c; l++) {
                            visit[k][l] = 0;
                        }
                    }

                }
                if(checker)
                    break;
            }

            if(checker)
                break;
        }

        if(checker == 1) {
            printf("YES\n");
        }
        else
            printf("NO\n");

        /*if(t != 0)
            printf("\n");*/
    }

    return 0;
}