-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALLIZZWELL.java
61 lines (60 loc) · 1.38 KB
/
ALLIZZWELL.java
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
package ques;
import java.util.Scanner;
import java.util.*;
public class ALLIZZWELL {
public static void main(String[] args)throws Exception {
Scanner scanner=new Scanner(System.in);
int t=scanner.nextInt();
while(t>0){
t--;
int r=scanner.nextInt();
int c=scanner.nextInt();
char[][] arr=new char[r][c];
for(int i=0;i<r;i++){
arr[i]=scanner.next().toCharArray();
}
ArrayList<Integer> x=new ArrayList<Integer>();
ArrayList<Integer> y=new ArrayList<Integer>();
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(arr[i][j]=='A'){
x.add(i);
y.add(j);
}
}
}
if(x.size()==0){
System.out.println("NO");
continue;
}
int i=0;
for(i=0;i<x.size();i++){
if(dfs(arr,x.get(i),y.get(i),0,"ALLIZZWELL")){
System.out.println("YES");
break;
}
}
if(i==x.size())
System.out.println("NO");
String newline=scanner.nextLine();
}
}
public static boolean dfs(char[][] arr, int i, int j, int index, String s){
if(index==s.length())
return true;
if((i<0 || i>=arr.length || j<0 || j>=arr[0].length)|| arr[i][j]=='#')
return false;
int[] a={1,1,1,-1,-1,-1,0,0};
int[] b={1,-1,0,1,-1,0,1,-1};
if(arr[i][j]!=s.charAt(index))
return false;
arr[i][j]='#';
for(int t=0;t<8;t++){
if(dfs(arr,i+a[t],j+b[t],index+1,s)){
return true;
}
}
arr[i][j]=s.charAt(index);
return false;
}
}