-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALLIZZWELL.cpp
83 lines (81 loc) · 1.37 KB
/
ALLIZZWELL.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <bits/stdc++.h>
using namespace std;
string s[200];
char a[10]={'A','L','L','I','Z','Z','W','E','L','L'};
int row,col;
int arr1[8] = {-1,-1,-1,0,1,1,1,0};
int arr2[8] = {1,0,-1,-1,-1,0,1,1};
bool visited[1000][1000];
int r,c;
int dfs(int indexX,int indexY,int patternIndex)
{
int ans;
visited[indexX][indexY] = true;
if(patternIndex==9)
{
return 1;
}
else
{
for (int i = 0; i < 8; ++i)
{
int tempx = indexX+arr1[i];
int tempy = indexY+arr2[i];
//printf("TempX: %d TempY: %d\n", tempx,tempy);
if(tempx>=0 && tempx <=r && tempy>=0 && tempy<=c)
{
char ch = s[tempx][tempy];
if((ch == a[patternIndex+1]) && (!visited[tempx][tempy]))
{
visited[tempx][tempy] = true;
ans = dfs(tempx,tempy,patternIndex+1);
if(ans==1)
{
return ans;
visited[tempx][tempy] = 0;
}
}
}
}
}
visited[indexX][indexY] = 0;
return 0;
}
int main()
{
int t,i,j,k;
scanf("%d",&t);
while(t--)
{
memset(visited, 0, sizeof(visited));
int flag=0;
scanf("%d%d",&r,&c);
for (int i = 0; i < r; ++i)
{
//printf("%d\n",i);
cin>>s[i];
}
for (int i = 0; i < r; ++i)
{
for (int j = 0; j < c; ++j)
{
if (s[i][j]=='A')
{
int ans = dfs(i,j,0);
if (ans)
{
printf("YES\n");
flag=1;
break;
}
}
}
if(flag)
break;
}
if(!flag)
{
printf("NO\n");
}
}
}