forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.java
More file actions
127 lines (121 loc) Β· 5.29 KB
/
8.java
File metadata and controls
127 lines (121 loc) Β· 5.29 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
import java.util.*;
class Node {
private int pos1X;
private int pos1Y;
private int pos2X;
private int pos2Y;
private int distance;
public int getPos1X() {
return this.pos1X;
}
public int getPos1Y() {
return this.pos1Y;
}
public int getPos2X() {
return this.pos2X;
}
public int getPos2Y() {
return this.pos2Y;
}
public int getDistance() {
return this.distance;
}
public Node(int pos1X, int pos1Y, int pos2X, int pos2Y, int distance) {
this.pos1X = pos1X;
this.pos1Y = pos1Y;
this.pos2X = pos2X;
this.pos2Y = pos2Y;
this.distance = distance;
}
}
class Solution {
public ArrayList<Node> getNextPos(Node pos, int[][] board) {
// λ°ν κ²°κ³Ό(μ΄λ κ°λ₯ν μμΉλ€)
ArrayList<Node> nextPos = new ArrayList<Node>();
// (μ, ν, μ’, μ°)λ‘ μ΄λνλ κ²½μ°μ λν΄μ μ²λ¦¬
int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};
for (int i = 0; i < 4; i++) {
int pos1NextX = pos.getPos1X() + dx[i];
int pos1NextY = pos.getPos1Y() + dy[i];
int pos2NextX = pos.getPos2X() + dx[i];
int pos2NextY = pos.getPos2Y() + dy[i];
int distanceNext = pos.getDistance() + 1;
// μ΄λνκ³ μ νλ λ μΉΈμ΄ λͺ¨λ λΉμ΄ μλ€λ©΄
if (board[pos1NextX][pos1NextY] == 0 && board[pos2NextX][pos2NextY] == 0) {
nextPos.add(new Node(pos1NextX, pos1NextY, pos2NextX, pos2NextY, distanceNext));
}
}
// νμ¬ λ‘λ΄μ΄ κ°λ‘λ‘ λμ¬ μλ κ²½μ°
int[] hor = {-1, 1};
if (pos.getPos1X() == pos.getPos2X()) {
for (int i = 0; i < 2; i++) { // μμͺ½μΌλ‘ νμ νκ±°λ, μλμͺ½μΌλ‘ νμ
// μμͺ½ νΉμ μλμͺ½ λ μΉΈμ΄ λͺ¨λ λΉμ΄ μλ€λ©΄
if (board[pos.getPos1X() + hor[i]][pos.getPos1Y()] == 0 && board[pos.getPos2X() + hor[i]][pos.getPos2Y()] == 0) {
nextPos.add(new Node(pos.getPos1X(), pos.getPos1Y(), pos.getPos1X() + hor[i], pos.getPos1Y(), pos.getDistance() + 1));
nextPos.add(new Node(pos.getPos2X(), pos.getPos2Y(), pos.getPos2X() + hor[i], pos.getPos2Y(), pos.getDistance() + 1));
}
}
}
// νμ¬ λ‘λ΄μ΄ μΈλ‘λ‘ λμ¬ μλ κ²½μ°
int[] ver = {-1, 1};
if (pos.getPos1Y() == pos.getPos2Y()) {
for (int i = 0; i < 2; i++) { // μΌμͺ½μΌλ‘ νμ νκ±°λ, μ€λ₯Έμͺ½μΌλ‘ νμ
// μΌμͺ½ νΉμ μ€λ₯Έμͺ½ λ μΉΈμ΄ λͺ¨λ λΉμ΄ μλ€λ©΄
if (board[pos.getPos1X()][pos.getPos1Y() + ver[i]] == 0 && board[pos.getPos2X()][pos.getPos2Y() + ver[i]] == 0) {
nextPos.add(new Node(pos.getPos1X(), pos.getPos1Y(), pos.getPos1X(), pos.getPos1Y() + ver[i], pos.getDistance() + 1));
nextPos.add(new Node(pos.getPos2X(), pos.getPos2Y(), pos.getPos2X(), pos.getPos2Y() + ver[i], pos.getDistance() + 1));
}
}
}
// νμ¬ μμΉμμ μ΄λν μ μλ μμΉλ₯Ό λ°ν
return nextPos;
}
public int solution(int[][] board) {
// λ§΅ μΈκ³½μ λ²½μ λλ ννλ‘ λ§΅ λ³ν
int n = board.length;
int[][] newBoard = new int[n + 2][n + 2];
for (int i = 0; i < n + 2; i++) {
for (int j = 0; j < n + 2; j++) {
newBoard[i][j] = 1;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
newBoard[i + 1][j + 1] = board[i][j];
}
}
// λλΉ μ°μ νμ(BFS) μν
Queue<Node> q = new LinkedList<>();
ArrayList<Node> visited = new ArrayList<>();
Node pos = new Node(1, 1, 1, 2, 0); // μμ μμΉ μ€μ
q.offer(pos); // νμ μ½μ
ν λ€μ
visited.add(pos); // λ°©λ¬Έ μ²λ¦¬
// νκ° λΉ λκΉμ§ λ°λ³΅
while (!q.isEmpty()) {
pos = q.poll();
// (n, n) μμΉμ λ‘λ΄μ΄ λλ¬νλ€λ©΄, μ΅λ¨ 거리μ΄λ―λ‘ λ°ν
if ((pos.getPos1X() == n && pos.getPos1Y() == n) || (pos.getPos2X() == n && pos.getPos2Y() == n)) {
return pos.getDistance();
}
// νμ¬ μμΉμμ μ΄λν μ μλ μμΉ νμΈ
ArrayList<Node> nextPos = getNextPos(pos, newBoard);
for (int i = 0; i < nextPos.size(); i++) {
// μμ§ λ°©λ¬Ένμ§ μμ μμΉλΌλ©΄ νμ μ½μ
νκ³ λ°©λ¬Έ μ²λ¦¬
boolean check = true;
pos = nextPos.get(i);
for (int j = 0; j < visited.size(); j++) {
if (pos.getPos1X() == visited.get(j).getPos1X() && pos.getPos1Y() == visited.get(j).getPos1Y() && pos.getPos2X() == visited.get(j).getPos2X() && pos.getPos2Y() == visited.get(j).getPos2Y()) {
check = false;
break;
}
}
if (check) {
q.offer(pos);
visited.add(pos);
}
}
}
return 0;
}
}