forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6.java
180 lines (162 loc) Β· 5.86 KB
/
6.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import java.util.*;
class Combination {
private int n;
private int r;
private int[] now; // νμ¬ μ‘°ν©
private ArrayList<ArrayList<Position>> result; // λͺ¨λ μ‘°ν©
public ArrayList<ArrayList<Position>> getResult() {
return result;
}
public Combination(int n, int r) {
this.n = n;
this.r = r;
this.now = new int[r];
this.result = new ArrayList<ArrayList<Position>>();
}
public void combination(ArrayList<Position> arr, int depth, int index, int target) {
if (depth == r) {
ArrayList<Position> temp = new ArrayList<>();
for (int i = 0; i < now.length; i++) {
temp.add(arr.get(now[i]));
}
result.add(temp);
return;
}
if (target == n) return;
now[index] = target;
combination(arr, depth + 1, index + 1, target + 1);
combination(arr, depth, index, target + 1);
}
}
class Position {
private int x;
private int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
}
public class Main {
public static int n; // 볡λμ ν¬κΈ°
public static char[][] board = new char[6][6]; // 볡λ μ 보 (N x N)
public static ArrayList<Position> teachers = new ArrayList<>(); // λͺ¨λ μ μλ μμΉ μ 보
public static ArrayList<Position> spaces = new ArrayList<>(); // λͺ¨λ λΉ κ³΅κ° μμΉ μ 보
// νΉμ λ°©ν₯μΌλ‘ κ°μλ₯Ό μ§ν (νμ λ°κ²¬: true, νμ λ―Έλ°κ²¬: false)
public static boolean watch(int x, int y, int direction) {
// μΌμͺ½ λ°©ν₯μΌλ‘ κ°μ
if (direction == 0) {
while (y >= 0) {
if (board[x][y] == 'S') { // νμμ΄ μλ κ²½μ°
return true;
}
if (board[x][y] == 'O') { // μ₯μ λ¬Όμ΄ μλ κ²½μ°
return false;
}
y -= 1;
}
}
// μ€λ₯Έμͺ½ λ°©ν₯μΌλ‘ κ°μ
if (direction == 1) {
while (y < n) {
if (board[x][y] == 'S') { // νμμ΄ μλ κ²½μ°
return true;
}
if (board[x][y] == 'O') { // μ₯μ λ¬Όμ΄ μλ κ²½μ°
return false;
}
y += 1;
}
}
// μμͺ½ λ°©ν₯μΌλ‘ κ°μ
if (direction == 2) {
while (x >= 0) {
if (board[x][y] == 'S') { // νμμ΄ μλ κ²½μ°
return true;
}
if (board[x][y] == 'O') { // μ₯μ λ¬Όμ΄ μλ κ²½μ°
return false;
}
x -= 1;
}
}
// μλμͺ½ λ°©ν₯μΌλ‘ κ°μ
if (direction == 3) {
while (x < n) {
if (board[x][y] == 'S') { // νμμ΄ μλ κ²½μ°
return true;
}
if (board[x][y] == 'O') { // μ₯μ λ¬Όμ΄ μλ κ²½μ°
return false;
}
x += 1;
}
}
return false;
}
// μ₯μ λ¬Ό μ€μΉ μ΄νμ, ν λͺ
μ΄λΌλ νμμ΄ κ°μ§λλμ§ κ²μ¬
public static boolean process() {
// λͺ¨λ μ μμ μμΉλ₯Ό νλμ© νμΈ
for (int i = 0; i < teachers.size(); i++) {
int x = teachers.get(i).getX();
int y = teachers.get(i).getY();
// 4κ°μ§ λ°©ν₯μΌλ‘ νμμ κ°μ§ν μ μλμ§ νμΈ
for (int j = 0; j < 4; j++) {
if (watch(x, y, j)) {
return true;
}
}
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = sc.next().charAt(0);
// μ μλμ΄ μ‘΄μ¬νλ μμΉ μ μ₯
if (board[i][j] == 'T') {
teachers.add(new Position(i, j));
}
// μ₯μ λ¬Όμ μ€μΉν μ μλ (λΉ κ³΅κ°) μμΉ μ μ₯
if (board[i][j] == 'X') {
spaces.add(new Position(i, j));
}
}
}
// λΉ κ³΅κ°μμ 3κ°λ₯Ό λ½λ λͺ¨λ μ‘°ν©μ νμΈ
Combination comb = new Combination(spaces.size(), 3);
comb.combination(spaces, 0, 0, 0);
ArrayList<ArrayList<Position>> spaceList = comb.getResult();
// νμμ΄ ν λͺ
λ κ°μ§λμ§ μλλ‘ μ€μΉν μ μλμ§μ μ¬λΆ
boolean found = false;
for (int i = 0; i < spaceList.size(); i++) {
// μ₯μ λ¬Όλ€μ μ€μΉν΄λ³΄κΈ°
for (int j = 0; j < spaceList.get(i).size(); j++) {
int x = spaceList.get(i).get(j).getX();
int y = spaceList.get(i).get(j).getY();
board[x][y] = 'O';
}
// νμμ΄ ν λͺ
λ κ°μ§λμ§ μλ κ²½μ°
if (!process()) {
// μνλ κ²½μ°λ₯Ό λ°κ²¬ν κ²μ
found = true;
break;
}
// μ€μΉλ μ₯μ λ¬Όμ λ€μ μμ κΈ°
for (int j = 0; j < spaceList.get(i).size(); j++) {
int x = spaceList.get(i).get(j).getX();
int y = spaceList.get(i).get(j).getY();
board[x][y] = 'X';
}
}
if (found) System.out.println("YES");
else System.out.println("NO");
}
}