Skip to content

Commit 42aab5c

Browse files
authored
31주차 (#27)
1 parent 8f54eff commit 42aab5c

File tree

4 files changed

+546
-0
lines changed

4 files changed

+546
-0
lines changed
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
package org.example._31week;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.StringTokenizer;
7+
8+
public class BeadEscape {
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
12+
private static final int SUCCESS_CODE = 1;
13+
private static final int FAIL_CODE = 0;
14+
private static final int EXIT_CODE = 2;
15+
16+
private static int rowSize;
17+
private static int colSize;
18+
private static final char EMPTY = '.';
19+
private static final char WALL = '#';
20+
private static final char EXIT = 'O';
21+
private static final char RED_BREAD = 'R';
22+
private static final char BLUE_BREAD = 'B';
23+
24+
private static int answer = Integer.MAX_VALUE;
25+
26+
public static void main(String[] args) throws IOException {
27+
StringTokenizer st = new StringTokenizer(br.readLine());
28+
rowSize = Integer.parseInt(st.nextToken());
29+
colSize = Integer.parseInt(st.nextToken());
30+
char[][] board = new char[rowSize][colSize];
31+
32+
for (int i = 0; i < rowSize; i++) {
33+
board[i] = br.readLine().toCharArray();
34+
}
35+
36+
// rowSize = 5;
37+
// colSize = 2;
38+
// char[][] board = new char[][]{{'.', EXIT}, {'R', 'R'}, {'#', '.'}, {'.', '.'}, {'B', '.'}};
39+
40+
// printArray(board);
41+
dfs(board, 1);
42+
43+
System.out.println(answer == Integer.MAX_VALUE ? -1 : answer);
44+
}
45+
46+
private static void dfs(char[][] board, int depth) {
47+
if (depth == 11) {
48+
return;
49+
}
50+
51+
upSwipe(board, depth);
52+
downSwipe(board, depth);
53+
rightSwipe(board, depth);
54+
leftSwipe(board, depth);
55+
}
56+
57+
private static char[][] copyBoard(char[][] board) {
58+
char[][] newBoard = new char[rowSize][colSize];
59+
60+
for (int i = 0; i < rowSize; i++) {
61+
for (int j = 0; j < colSize; j++) {
62+
newBoard[i][j] = board[i][j];
63+
}
64+
}
65+
66+
return newBoard;
67+
}
68+
69+
private static void leftSwipe(char[][] board, int depth) {
70+
char[][] newBoard = copyBoard(board);
71+
boolean exits = false;
72+
73+
for (int row = 0; row < rowSize; row++) {
74+
int colPivot = 0;
75+
76+
for (int col = 0; col < colSize; col++) {
77+
// 비어있는 경우
78+
if (newBoard[row][col] == EMPTY) {
79+
continue;
80+
}
81+
82+
if (newBoard[row][col] == WALL) {
83+
colPivot = col + 1;
84+
continue;
85+
}
86+
87+
if (newBoard[row][col] == EXIT) {
88+
colPivot = col;
89+
continue;
90+
}
91+
92+
if (newBoard[row][col] == RED_BREAD || newBoard[row][col] == BLUE_BREAD) {
93+
//구멍에 빠지는 경우를 고려할 수 있도록 수정해야함.
94+
if (newBoard[row][colPivot] == EXIT) {
95+
if (newBoard[row][col] == RED_BREAD) {
96+
exits = true;
97+
}
98+
99+
if (newBoard[row][col] == BLUE_BREAD) {
100+
return;
101+
}
102+
103+
} else {
104+
newBoard[row][colPivot] = newBoard[row][col];
105+
if (colPivot != col) {
106+
newBoard[row][col] = EMPTY;
107+
}
108+
109+
colPivot++;
110+
}
111+
}
112+
}
113+
114+
if (exits) {
115+
answer = Math.min(answer, depth);
116+
return;
117+
}
118+
}
119+
120+
dfs(newBoard, depth + 1);
121+
}
122+
123+
private static void rightSwipe(char[][] board, int depth) {
124+
char[][] newBoard = copyBoard(board);
125+
boolean exits = false;
126+
127+
for (int row = 0; row < rowSize; row++) {
128+
int colPivot = colSize - 1;
129+
130+
for (int col = colSize - 1; col >= 0; col--) {
131+
// 비어있는 경우
132+
if (newBoard[row][col] == EMPTY) {
133+
continue;
134+
}
135+
136+
if (newBoard[row][col] == WALL) {
137+
colPivot = col - 1;
138+
continue;
139+
}
140+
141+
if (newBoard[row][col] == EXIT) {
142+
colPivot = col;
143+
continue;
144+
}
145+
146+
if (newBoard[row][col] == RED_BREAD || newBoard[row][col] == BLUE_BREAD) {
147+
//구멍에 빠지는 경우를 고려할 수 있도록 수정해야함.
148+
if (newBoard[row][colPivot] == EXIT) {
149+
if (newBoard[row][col] == RED_BREAD) {
150+
exits = true;
151+
}
152+
153+
if (newBoard[row][col] == BLUE_BREAD) {
154+
return;
155+
}
156+
157+
} else {
158+
newBoard[row][colPivot] = newBoard[row][col];
159+
if (colPivot != col) {
160+
newBoard[row][col] = EMPTY;
161+
}
162+
colPivot--;
163+
}
164+
}
165+
}
166+
167+
if (exits) {
168+
answer = Math.min(answer, depth);
169+
return;
170+
}
171+
}
172+
173+
dfs(newBoard, depth + 1);
174+
}
175+
176+
private static void upSwipe(char[][] board, int depth) {
177+
char[][] newBoard = copyBoard(board);
178+
boolean exits = false;
179+
180+
for (int col = 0; col < colSize; col++) {
181+
int rowPivot = 0;
182+
183+
for (int row = 0; row < rowSize; row++) {
184+
// 비어있는 경우
185+
if (newBoard[row][col] == EMPTY) {
186+
continue;
187+
}
188+
189+
if (newBoard[row][col] == WALL) {
190+
rowPivot = row + 1;
191+
continue;
192+
}
193+
194+
if (newBoard[row][col] == EXIT) {
195+
rowPivot = row;
196+
continue;
197+
}
198+
199+
if (newBoard[row][col] == RED_BREAD || newBoard[row][col] == BLUE_BREAD) {
200+
//구멍에 빠지는 경우를 고려할 수 있도록 수정해야함.
201+
if (newBoard[rowPivot][col] == EXIT) {
202+
if (newBoard[row][col] == RED_BREAD) {
203+
exits = true;
204+
}
205+
206+
if (newBoard[row][col] == BLUE_BREAD) {
207+
return;
208+
}
209+
210+
} else {
211+
newBoard[rowPivot][col] = newBoard[row][col];
212+
if (rowPivot != row) {
213+
newBoard[row][col] = EMPTY;
214+
}
215+
rowPivot++;
216+
}
217+
}
218+
}
219+
220+
if (exits) {
221+
answer = Math.min(answer, depth);
222+
return;
223+
}
224+
}
225+
226+
dfs(newBoard, depth + 1);
227+
}
228+
229+
private static void downSwipe(char[][] board, int depth) {
230+
char[][] newBoard = copyBoard(board);
231+
boolean exits = false;
232+
233+
for (int col = 0; col < colSize; col++) {
234+
int rowPivot = rowSize - 1;
235+
236+
for (int row = rowSize - 1; row >= 0; row--) {
237+
// 비어있는 경우
238+
if (newBoard[row][col] == EMPTY) {
239+
continue;
240+
}
241+
242+
if (newBoard[row][col] == WALL) {
243+
rowPivot = row - 1;
244+
continue;
245+
}
246+
247+
if (newBoard[row][col] == EXIT) {
248+
rowPivot = row;
249+
continue;
250+
}
251+
252+
if (newBoard[row][col] == RED_BREAD || newBoard[row][col] == BLUE_BREAD) {
253+
//구멍에 빠지는 경우를 고려할 수 있도록 수정해야함.
254+
if (newBoard[rowPivot][col] == EXIT) {
255+
if (newBoard[row][col] == RED_BREAD) {
256+
exits = true;
257+
}
258+
259+
if (newBoard[row][col] == BLUE_BREAD) {
260+
return;
261+
}
262+
263+
} else {
264+
newBoard[rowPivot][col] = newBoard[row][col];
265+
if (rowPivot != row) {
266+
newBoard[row][col] = EMPTY;
267+
}
268+
269+
rowPivot--;
270+
}
271+
}
272+
}
273+
274+
if (exits) {
275+
answer = Math.min(answer, depth);
276+
return;
277+
}
278+
}
279+
280+
dfs(newBoard, depth + 1);
281+
}
282+
283+
private static void printArray(char[][] board) {
284+
System.out.println("------print board!!------");
285+
for (int i = 0; i < board.length; i++) {
286+
for (int j = 0; j < board[0].length; j++) {
287+
System.out.print(board[i][j] + " ");
288+
}
289+
System.out.println();
290+
}
291+
System.out.println();
292+
}
293+
}

0 commit comments

Comments
 (0)