-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHW_250134.java
More file actions
124 lines (109 loc) ยท 4.75 KB
/
HW_250134.java
File metadata and controls
124 lines (109 loc) ยท 4.75 KB
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
// ํผ์ฆ์ ํธ๋๋ฐ ํ์ํ ํด์ ์ต์๊ฐ ๊ตฌํ๊ธฐ
// ์ต๋จ ๊ฒฝ๋ก (BFS)
// ์๊ฐ ๋ณต์ก๋ : ๊ตฌํ ๋ฌธ์ ๋ผ ๊ณ ๋ คX
// ํผ์ฆ์ ํ ์ ์๋ ๊ฒฝ์ฐ 0 return
// ์กฐ๊ฑด1) ๋ฒฝ(5)์ ์ง๋๊ฐ ์ ์์
// ์กฐ๊ฑด2) ๋์์ ์๋ ๋ฅผ ๊ฐ์ ์นธ์ผ๋ก ์์ง์ผ ์๋ ์์
// ์กฐ๊ฑด3) ์๋ฆฌ๋ฅผ ๊ตํํ๋ฉฐ ์ด๋ ๋ถ๊ฐ
import java.util.*;
class HW_250134 {
static int answer = Integer.MAX_VALUE; // ์ต์ ์ด๋ ํ์๋ฅผ ์ ์ฅ
static int[] dx = {-1, 1, 0, 0}; // ์ํ์ข์ฐ
static int[] dy = {0, 0, -1, 1};
static boolean[][] redVisited, blueVisited; // ๋ฐฉ๋ฌธ ๋ฐฐ์ด
static int[][] map; // ํผ์ฆํ ๋ณต์ฌ
static int n, m; // ํผ์ฆํ ํฌ๊ธฐ
static int redEndX, redEndY, blueEndX, blueEndY; // ๋์ฐฉ ์์น
public int solution(int[][] maze) {
n = maze.length;
m = maze[0].length;
map = new int[n][m];
redVisited = new boolean[n][m];
blueVisited = new boolean[n][m];
int redStartX = 0, redStartY = 0;
int blueStartX = 0, blueStartY = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
map[i][j] = maze[i][j];
if (maze[i][j] == 1) { // ๋นจ๊ฐ ์๋ ์์์
redStartX = i;
redStartY = j;
redVisited[i][j] = true;
} else if (maze[i][j] == 2) { // ํ๋ ์๋ ์์์
blueStartX = i;
blueStartY = j;
blueVisited[i][j] = true;
} else if (maze[i][j] == 3) { // ๋นจ๊ฐ ์๋ ๋์ฐฉ์
redEndX = i;
redEndY = j;
} else if (maze[i][j] == 4) { // ํ๋ ์๋ ๋์ฐฉ์
blueEndX = i;
blueEndY = j;
}
}
}
backtrack(redStartX, redStartY, blueStartX, blueStartY, 0, false, false);
return answer == Integer.MAX_VALUE ? 0 : answer;
}
static void backtrack(int redX, int redY, int blueX, int blueY, int moveCount, boolean redArrived, boolean blueArrived) {
if (!redArrived && redX == redEndX && redY == redEndY) { // ๋์ฐฉ
redArrived = true;
}
if (!blueArrived && blueX == blueEndX && blueY == blueEndY) {
blueArrived = true;
}
if (redArrived && blueArrived) {
answer = Math.min(answer, moveCount); // ์ต์๊ฐ ๋ฐํ
return;
}
List<int[]> redMoves = new ArrayList<>(); // ์ด๋ ๊ฐ๋ฅํ ๋ฆฌ์คํธ
List<int[]> blueMoves = new ArrayList<>();
// ๋นจ๊ฐ ์๋ ์ด๋
if (!redArrived) {
for (int i = 0; i < 4; i++) {
int nx = redX + dx[i];
int ny = redY + dy[i];
if (isValid(nx, ny) && !redVisited[nx][ny] && map[nx][ny] != 5) { // ์กฐ๊ฑด1) ๋ฒฝ(5)์ ์ง๋ ์ ์์
redMoves.add(new int[]{nx, ny}); // ์ด๋ ๊ฐ๋ฅํ ์์น ์ถ๊ฐ
}
}
} else { // ์ด๋ฏธ ๋์ฐฉํ ๊ฒฝ์ฐ ํ์ฌ ์์น ์ ์ง
redMoves.add(new int[]{redX, redY});
}
// ํ๋ ์๋ ์ด๋
if (!blueArrived) {
for (int i = 0; i < 4; i++) {
int nx = blueX + dx[i];
int ny = blueY + dy[i];
if (isValid(nx, ny) && !blueVisited[nx][ny] && map[nx][ny] != 5) {
blueMoves.add(new int[]{nx, ny}); // ์ด๋ ๊ฐ๋ฅํ ์์น ์ถ๊ฐ
}
}
} else { // ์ด๋ฏธ ๋์ฐฉํ ๊ฒฝ์ฐ ํ์ฌ ์์น ์ ์ง
blueMoves.add(new int[]{blueX, blueY});
}
// ๋ ์๋ ์ ์ด๋ ์๋ฎฌ๋ ์ด์
for (int[] redMove : redMoves) {
for (int[] blueMove : blueMoves) {
int nRedX = redMove[0], nRedY = redMove[1];
int nBlueX = blueMove[0], nBlueY = blueMove[1];
// ์กฐ๊ฑด2) ๋์์ ์๋ ๋ฅผ ๊ฐ์ ์นธ์ผ๋ก ์์ง์ผ ์๋ ์์
if (nRedX == nBlueX && nRedY == nBlueY) {
continue;
}
// ์กฐ๊ฑด3) ์๋ฆฌ๋ฅผ ๊ตํํ๋ฉฐ ์ด๋ ๋ถ๊ฐ
if (nRedX == blueX && nRedY == blueY && nBlueX == redX && nBlueY == redY) continue;
// ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
redVisited[nRedX][nRedY] = true;
blueVisited[nBlueX][nBlueY] = true;
backtrack(nRedX, nRedY, nBlueX, nBlueY, moveCount + 1, redArrived, blueArrived); // ์ฌ๊ท ํธ์ถ
// ๋ฐฉ๋ฌธ ๋ณต๊ตฌ
redVisited[nRedX][nRedY] = false;
blueVisited[nBlueX][nBlueY] = false;
}
}
}
static boolean isValid(int x, int y) {
return 0 <= x && x < n && 0 <= y && y < m;
}
}