-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDH_250134.java
More file actions
130 lines (100 loc) Β· 4.43 KB
/
DH_250134.java
File metadata and controls
130 lines (100 loc) Β· 4.43 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
125
126
127
128
129
130
import java.util.*;
public class DH_250134 {
static final int RED = 0, BLUE = 1, INF = Integer.MAX_VALUE;
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
static class Node {
int r, c, color;
int[][][] v;
public Node() {}
public Node(int color, int r, int c, int[][][] v) {
this.r = r;
this.c = c;
this.color = color;
this.v = v;
}
public void update(int r, int c) {
this.r = r;
this.c = c;
}
@Override
public String toString() {
return "[r: " + r + ", c: " + c + ", color: " + color + "]";
}
}
static int answer = INF;
static int solution(int[][] maze) {
// 0: λΉ¨κ° μλ μμΉ, 1: νλ μλ μμΉ, 2: λΉ¨κ° μλ λμ°© μΉΈ, 2: νλ μλ λμ°© μΉΈ
int[][] positionInfo = new int[4][2];
initPoints(positionInfo, maze);
bfs(RED, BLUE, positionInfo, maze);
return answer == INF ? 0 : answer;
}
static void bfs(int red, int blue, int[][] positionInfo, int[][] maze) {
ArrayDeque<Node> q = new ArrayDeque<>();
int[][][] rv = new int[2][maze.length][maze[0].length];
rv[red][positionInfo[red][0]][positionInfo[red][1]] = 1;
rv[blue][positionInfo[blue][0]][positionInfo[blue][1]] = 1;
int[][][] bv = new int[2][maze.length][maze[0].length];
bv[red][positionInfo[red][0]][positionInfo[red][1]] = 1;
bv[blue][positionInfo[blue][0]][positionInfo[blue][1]] = 1;
q.add(new Node(red, positionInfo[red][0], positionInfo[red][1], rv));
q.add(new Node(blue, positionInfo[blue][0], positionInfo[blue][1], bv));
int flag = 0;
int fr = -1, fc = -1;
int maxDis = 0;
while(!q.isEmpty()) {
Node current = q.poll();
if(positionInfo[current.color + 2][0] == current.r && positionInfo[current.color + 2][1] == current.c) {
System.out.println("color: " + current.color + " , depth: " + (current.v[current.color][current.r][current.c] - 1));
System.out.println("λμ°©: " + current.r + " " + current.c + " " + (current.v[current.color][current.r][current.c] - 1));
flag |= 1 << current.color;
fr = current.r;
fc = current.c;
maxDis = current.v[current.color][current.r][current.c] - 1;
}
if(flag == ((1 << 2) - 1)) {
System.out.println("color: " + current.color + " , depth: " + (current.v[current.color][current.r][current.c] - 1));
answer = Math.max(maxDis, current.v[current.color][current.r][current.c] - 1);
System.out.println("λλ° !!");
break;
}
if((flag & (1 << current.color)) > 0) continue;
for(int d = 0; d < 4; d++) {
int nr = current.r + dr[d];
int nc = current.c + dc[d];
int[][][] currentV = current.v;
int[][][] tmpV = new int[2][currentV[0].length][currentV[0][0].length];
for(int k = 0; k < currentV.length; k++) {
for(int r = 0; r < currentV[0].length; r++) {
for(int c = 0; c < currentV[0][0].length; c++) tmpV[k][r][c] = currentV[k][r][c];
}
}
if(!check(nr, nc, maze.length, maze[0].length)) continue;
if(nr == fr && fc == nc) continue;
if(tmpV[current.color][nr][nc] > 0 ||
maze[nr][nc] == 5 ||
(tmpV[current.color ^ 1][nr][nc] == tmpV[current.color][current.r][current.c] + 1)) continue;
System.out.println((current.color == 0 ? "RED" : "BLUE") + " " + nr + " " + nc + " " + current.v[current.color][current.r][current.c]);
tmpV[current.color][nr][nc] = tmpV[current.color][current.r][current.c] + 1;
q.add(new Node(current.color, nr, nc, tmpV));
}
}
}
static void initPoints(int[][] positionInfo, int[][] maze) {
for(int r = 0; r < maze.length; r++) {
for(int c = 0; c < maze[0].length; c++) {
if(maze[r][c] == 0 || maze[r][c] == 5) continue;
int status = maze[r][c] - 1;
positionInfo[status][0] = r;
positionInfo[status][1] = c;
}
}
}
static boolean check(int nr, int nc, int r, int c) {
return nr >= 0 && nr < r && nc >= 0 && nc < c;
}
public static void main(String[] args) {
int[][] maze = {{1, 0, 2}, {0, 0, 0}, {5, 0, 5}, {4, 0, 3}};
System.out.println(solution(maze));
}
}