-
Notifications
You must be signed in to change notification settings - Fork 7
/
ALLIZWEL.cpp
67 lines (65 loc) · 1.15 KB
/
ALLIZWEL.cpp
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
#include "bits/stdc++.h"
using namespace std;
int dx[] = { -1,-1,-1,0,0,1,1,1 };
int dy[] = { -1,0,1,-1,1,-1,0,1 };
bool visit[105][105];
int r, c;
char arr[105][105];
bool issafe(int i, int j)
{
return (i >= 0) && (i < r) && (j >= 0) && (j < c);
}
string pattern = "ALLIZZWELL";
bool dfs(int i, int j,int level)
{
//printf("%c ", arr[i][j]);
visit[i][j] = 1;
if (level == 9)return true;
for (int k = 0; k < 8; k++)
{
if (issafe(i + dx[k], j + dy[k]))
{
if ((arr[i + dx[k]][j + dy[k]] == pattern[level + 1]) && (!visit[i + dx[k]][j + dy[k]]))
{
visit[i + dx[k]][j + dy[k]] = 1;
if (dfs(i + dx[k], j + dy[k], level + 1))
return true;
visit[i + dx[k]][j + dy[k]] = 0;
}
}
}
visit[i][j] = 0;
return false;
}
bool solve()
{
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
if (arr[i][j] == 'A' && dfs(i, j, 0))
{
return true;
}
}
}
return false;
}
int main()
{
//READ;
int t;
scanf("%d", &t);
while (t--)
{
memset(visit, 0, sizeof(visit));
scanf("%d%d", &r, &c);
for (int i = 0; i < r; i++)
{
scanf("%s", arr[i]);
}
if (solve())printf("YES\n");
else printf("NO\n");
}
return 0;
}