-
Notifications
You must be signed in to change notification settings - Fork 2
/
ykm.java
84 lines (76 loc) · 2.56 KB
/
ykm.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package P18428;
import java.util.*;
import java.io.*;
public class Main {
static int N;
static char[][] map; // T: teacher, S:student , W:wall , V:visit
static ArrayList<int[]> positionT = new ArrayList<int[]>(); // inital position of teachers
static int[] mx = {-1,1,0,0};
static int[] my = {0,0,-1,1};
static boolean ansFlag = false;
public static void main(String[] args) throws IOException{
System.setIn(new FileInputStream("P18428/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
map = new char[N][N];
for(int i = 0 ; i<N; i++){
StringTokenizer st = new StringTokenizer(br.readLine());
for(int j =0; j<N; j++){
String person = st.nextToken();
if(person.equals("S")){
map[i][j]='S';
}else if(person.equals("T")){
map[i][j]='T';
positionT.add(new int[] {i,j});
}else{
map[i][j]='\0';
}
}
}
// 벽세우기 (dfs)
wall(0,0,0);
if(!ansFlag) System.out.println("NO");
br.close();
}
static void wall(int count, int x, int y){
if(count==3){
// 확인하기
if(check()) {
ansFlag = true;
System.out.println("YES");
System.exit(1);
}
return;
}
else{
for(int i = x ; i< N ; i++){
for(int j =(i==x? y:0 ); j<N ; j++){
if(map[i][j]=='\0'){
map[i][j]='W';
wall(count+1,i,j);
map[i][j]='\0';
}
}
}
}
}
static boolean check(){
for(int i = 0 ; i<positionT.size(); i++){
int[] position = positionT.get(i);
for(int j = 0 ; j<4 ; j++){
int m = 0;
while(m ++ < N){ // 벽을 만날때 까지
int nextX = position[0] + m*mx[j];
int nextY = position[1] + m*my[j];
if(nextX>=0 && nextY>=0 && nextX<N && nextY<N){ // 범위내
if(map[nextX][nextY]=='S'){
return false;
}else if(map[nextX][nextY]=='W') break;
}
else break;
}
}
}
return true;
}
}