fork download
  1. # include <iostream>
  2. using namespace std;
  3. char arr[105][105];
  4. int q[10050],front=-1,rear=-1,maxcount=0,pos=0;
  5. char str[10]={'A','L','L','I','Z','Z','W','E','L','L'};
  6. void enqueue(int xe,int ye)
  7. {
  8. if (front==-1)
  9. front=0;
  10. q[++rear]=xe;
  11. q[++rear]=ye;
  12. }
  13.  
  14. int dequeue()
  15. {
  16. if(front>rear)
  17. return -1;
  18. return(q[front++]);
  19. }
  20.  
  21. void bfs(int x,int y,int h1,int w1)
  22. {
  23. if(arr[x][y]==65)
  24. {
  25. front=-1;rear=-1;
  26. }
  27. int newx,newy,x1,y1,flag=0;
  28. char n1,n2;
  29. int turnx[]={0,0,1,1,-1,-1,1,-1};
  30. int turny[]={1,-1,1,-1,1,-1,0,0};
  31. //n1=arr[x][y];
  32. for(int i=0;i<8;i++)
  33. {
  34. newx=x+turnx[i];
  35. newy=y+turny[i];
  36. n2=arr[newx][newy];
  37. //cout<<str[pos]<<" bahar wala "<<str[pos+1]<<" must be equal to "<<n2<<"\n";
  38. if((newx>=0)&&(newx<h1)&&(newy>=0)&&(newy<w1)&&(str[pos+1]==n2))
  39. {
  40. //cout<<"in enqueue"<<" "<<n2<<"\n";
  41. enqueue(newx,newy);
  42. maxcount=pos;
  43. flag=1;
  44. }
  45.  
  46.  
  47. }
  48. if(flag==1)
  49. pos++;
  50. }
  51.  
  52. int main()
  53. {
  54. int t;
  55. cin>>t;
  56. while(t)
  57. {
  58. int h,w,x1,y1,acount=0,max=0;
  59.  
  60. cin>>h;
  61. cin>>w;
  62. for(int i=0;i<h;i++)
  63. {
  64. for(int j=0;j<w;j++)
  65. {
  66. cin>>arr[i][j];
  67. }
  68. }
  69. for(int i=0;i<h;i++)
  70. {
  71. for(int j=0;j<w;j++)
  72. {
  73. front=-1;rear=-1;
  74. if(arr[i][j]==65)
  75. {maxcount=0;
  76. enqueue(i,j);
  77. x1=dequeue();
  78. y1=dequeue();
  79. while((x1!=-1)&&(y1!=-1))
  80. {
  81. bfs(x1,y1,h,w);
  82. x1=dequeue();
  83. y1=dequeue();
  84.  
  85.  
  86. }
  87. }
  88. }
  89. }
  90. if(maxcount==8)
  91. cout<<"YES"<<"\n";
  92. else
  93. cout<<"NO"<<"\n";
  94.  
  95. t--;
  96. }
  97. return 0;
  98. }
  99.  
Success #stdin #stdout 0.01s 2732KB
stdin
5



3 6

AWE.QX

LLL.EO

IZZWLL



1 10

ALLIZZWELL



2 9

A.L.Z.E..

.L.I.W.L.



3 3

AEL

LWZ

LIZ



1 10

LLEWZZILLA
stdout
YES
NO
NO
NO
NO