|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + static int N; |
| 8 | + static char[][] map; |
| 9 | + static List<int[]> blank = new ArrayList<>(); |
| 10 | + static List<int[]> teacher = new ArrayList<>(); |
| 11 | + static boolean found = false; // 성공 여부 |
| 12 | + |
| 13 | + static int[] dr = {-1, 1, 0, 0}; |
| 14 | + static int[] dc = {0, 0, -1, 1}; |
| 15 | + |
| 16 | + public static void main(String[] args) throws Exception { |
| 17 | + |
| 18 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 19 | + N = Integer.parseInt(br.readLine()); |
| 20 | + |
| 21 | + map = new char[N][N]; |
| 22 | + |
| 23 | + for (int i = 0; i < N; i++) { |
| 24 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 25 | + for (int j = 0; j < N; j++) { |
| 26 | + map[i][j] = st.nextToken().charAt(0); |
| 27 | + |
| 28 | + if (map[i][j] == 'X') blank.add(new int[]{i, j}); |
| 29 | + if (map[i][j] == 'T') teacher.add(new int[]{i, j}); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + dfs(0, 0); |
| 34 | + |
| 35 | + System.out.println(found ? "YES" : "NO"); |
| 36 | + } |
| 37 | + |
| 38 | + static void dfs(int idx, int cnt) { |
| 39 | + if (found) return; |
| 40 | + |
| 41 | + if (cnt == 3) { |
| 42 | + if (check()) found = true; |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + if (idx >= blank.size()) return; |
| 47 | + |
| 48 | + int r = blank.get(idx)[0]; |
| 49 | + int c = blank.get(idx)[1]; |
| 50 | + |
| 51 | + map[r][c] = 'O'; |
| 52 | + dfs(idx + 1, cnt + 1); |
| 53 | + |
| 54 | + map[r][c] = 'X'; |
| 55 | + dfs(idx + 1, cnt); |
| 56 | + } |
| 57 | + |
| 58 | + static boolean check() { |
| 59 | + for (int[] t : teacher) { |
| 60 | + int tr = t[0]; |
| 61 | + int tc = t[1]; |
| 62 | + |
| 63 | + for (int d = 0; d < 4; d++) { |
| 64 | + int nr = tr; |
| 65 | + int nc = tc; |
| 66 | + |
| 67 | + while (true) { |
| 68 | + nr += dr[d]; |
| 69 | + nc += dc[d]; |
| 70 | + |
| 71 | + if (nr < 0 || nc < 0 || nr >= N || nc >= N) break; |
| 72 | + |
| 73 | + if (map[nr][nc] == 'O') break; |
| 74 | + if (map[nr][nc] == 'S') return false; |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + return true; |
| 79 | + } |
| 80 | +} |
| 81 | +``` |
0 commit comments