-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
86 lines (68 loc) Β· 2.89 KB
/
Main.java
File metadata and controls
86 lines (68 loc) Β· 2.89 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
package _2020_KAKAO_BLIND_RECRUITMENT.P7;
import java.util.*;
public class Main {
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.solution(new int[][]{{0, 0, 0, 1, 1},{0, 0, 0, 1, 0},{0, 1, 0, 1, 1},{1, 1, 0, 0, 1},{0, 0, 0, 0, 0}}));
}
}
class Solution {
static int N;
static int[][] my_board;
static boolean[][][] visited;
static int[] di = {-1, 0, 1, 0};
static int[] dj = {0, 1, 0, -1};
static int[] rot = {-1, 1};
public int solution(int[][] board) {
N = board.length;
my_board = board;
visited = new boolean[N][N][2];
Queue<Position> q = new LinkedList<>();
q.offer(new Position(new int[]{0, 0}, new int[]{0, 1}, 0));
while (!q.isEmpty()) {
Position cur = q.poll();
int cur_dir = (cur.fir[0] == cur.sec[0]) ? 0 : 1;
visited[cur.fir[0]][cur.fir[1]][cur_dir] = true;
for (int t = 0; t < 4; t++) {
int[] fir_to = new int[]{cur.fir[0] + di[t], cur.fir[1] + dj[t]};
int[] sec_to = new int[]{cur.sec[0] + di[t], cur.sec[1] + dj[t]};
if (isValidPath(fir_to[0], fir_to[1]) && isValidPath(sec_to[0], sec_to[1])
&& !visited[fir_to[0]][fir_to[1]][cur_dir]) {
if ((fir_to[0] == N-1 && fir_to[1] == N-1) || (sec_to[0] == N-1 && sec_to[1] == N-1)) return cur.time + 1;
q.offer(new Position(fir_to, sec_to, cur.time + 1));
}
}
for (int t = 0; t < 2; t++) {
int[] fir_to, sec_to;
int to_dir = (cur_dir == 0) ? 1 : 0;
if (cur.fir[0] == cur.sec[0]) {
fir_to = new int[]{cur.fir[0] + rot[t], cur.fir[1]};
sec_to = new int[]{cur.sec[0] + rot[t], cur.sec[1]};
} else {
fir_to = new int[]{cur.fir[0], cur.fir[1] + rot[t]};
sec_to = new int[]{cur.sec[0], cur.sec[1] + rot[t]};
}
if (isValidPath(fir_to[0], fir_to[1]) && isValidPath(sec_to[0], sec_to[1])
&& !visited[cur.fir[0]][cur.fir[1]][to_dir] && !visited[cur.sec[0]][cur.sec[1]][to_dir]) {
if ((fir_to[0] == N-1 && fir_to[1] == N-1) || (sec_to[0] == N-1 && sec_to[1] == N-1)) return cur.time + 1;
q.offer(new Position(cur.fir, fir_to, cur.time + 1));
q.offer(new Position(cur.sec, sec_to, cur.time + 1));
}
}
}
return 0;
}
private static boolean isValidPath(int i, int j) {
return 0 <= i && i < N && 0 <= j && j < N && my_board[i][j] == 0;
}
}
class Position {
int[] fir;
int[] sec;
int time;
public Position(int[] fir, int[] sec, int time) {
this.fir = fir;
this.sec = sec;
this.time = time;
}
}