forked from vengateshsubramaniyan/Spoj-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathALLIZWEL.cpp
54 lines (54 loc) · 958 Bytes
/
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
#include<bits/stdc++.h>
using namespace std;
char arr[101][101];
char s[]="ALLIZZWELL";
int row[]={1,0,-1,0,1,1,-1,-1};
int col[]={0,1,0,-1,1,-1,1,-1};
bool dfs(int i,int j,int l,int r,int c,vector< vector<bool> > &visit)
{
if(l==10)
return true;
for(int k=0;k<8;k++)
{
int ci=i+row[k];
int cj=j+col[k];
if(ci>=0 && ci<r && cj>=0 && cj<c && !visit[ci][cj] && arr[ci][cj]==s[l])
{
visit[ci][cj]=true;
if(dfs(ci,cj,l+1,r,c,visit))
return true;
visit[ci][cj]=false;
}
}
return false;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int r,c;
scanf("%d%d",&r,&c);
vector< vector<bool> > visit(r,vector<bool>(c,false));
for(int i=0;i<r;i++)
{
scanf("%s",arr[i]);
}
bool fo=false;
for(int i=0;i<r && !fo;i++)
{
for(int j=0;j<c && !fo;j++)
{
if(arr[i][j]=='A')
{
if(dfs(i,j,1,r,c,visit))
fo=true;
}
}
}
fo==true?printf("YES"):printf("NO");
printf("\n");
}
return 0;
}