Skip to content

Commit d39d68b

Browse files
authored
Update 8.java
1 parent cb4676e commit d39d68b

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

13/8.java

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import java.util.*;
2+
3+
class Node {
4+
private int pos1X;
5+
private int pos1Y;
6+
private int pos2X;
7+
private int pos2Y;
8+
private int distance;
9+
10+
public int getPos1X() {
11+
return this.pos1X;
12+
}
13+
public int getPos1Y() {
14+
return this.pos1Y;
15+
}
16+
public int getPos2X() {
17+
return this.pos2X;
18+
}
19+
public int getPos2Y() {
20+
return this.pos2Y;
21+
}
22+
public int getDistance() {
23+
return this.distance;
24+
}
25+
26+
public Node(int pos1X, int pos1Y, int pos2X, int pos2Y, int distance) {
27+
this.pos1X = pos1X;
28+
this.pos1Y = pos1Y;
29+
this.pos2X = pos2X;
30+
this.pos2Y = pos2Y;
31+
this.distance = distance;
32+
}
33+
}
34+
35+
class Solution {
36+
37+
public ArrayList<Node> getNextPos(Node pos, int[][] board) {
38+
// 반환 결과(이동 가능한 위치들)
39+
ArrayList<Node> nextPos = new ArrayList<Node>();
40+
// (상, 하, 좌, 우)로 이동하는 경우에 대해서 처리
41+
int[] dx = {-1, 1, 0, 0};
42+
int[] dy = {0, 0, -1, 1};
43+
for (int i = 0; i < 4; i++) {
44+
int pos1NextX = pos.getPos1X() + dx[i];
45+
int pos1NextY = pos.getPos1Y() + dy[i];
46+
int pos2NextX = pos.getPos2X() + dx[i];
47+
int pos2NextY = pos.getPos2Y() + dy[i];
48+
int distanceNext = pos.getDistance() + 1;
49+
// 이동하고자 하는 두 칸이 모두 비어 있다면
50+
if (board[pos1NextX][pos1NextY] == 0 && board[pos2NextX][pos2NextY] == 0) {
51+
nextPos.add(new Node(pos1NextX, pos1NextY, pos2NextX, pos2NextY, distanceNext));
52+
}
53+
}
54+
// 현재 로봇이 가로로 놓여 있는 경우
55+
int[] hor = {-1, 1};
56+
if (pos.getPos1X() == pos.getPos2X()) {
57+
for (int i = 0; i < 2; i++) { // 위쪽으로 회전하거나, 아래쪽으로 회전
58+
// 위쪽 혹은 아래쪽 두 칸이 모두 비어 있다면
59+
if (board[pos.getPos1X() + hor[i]][pos.getPos1Y()] == 0 && board[pos.getPos2X() + hor[i]][pos.getPos2Y()] == 0) {
60+
nextPos.add(new Node(pos.getPos1X(), pos.getPos1Y(), pos.getPos1X() + hor[i], pos.getPos1Y(), pos.getDistance() + 1));
61+
nextPos.add(new Node(pos.getPos2X(), pos.getPos2Y(), pos.getPos2X() + hor[i], pos.getPos2Y(), pos.getDistance() + 1));
62+
}
63+
}
64+
}
65+
// 현재 로봇이 세로로 놓여 있는 경우
66+
int[] ver = {-1, 1};
67+
if (pos.getPos1Y() == pos.getPos2Y()) {
68+
for (int i = 0; i < 2; i++) { // 왼쪽으로 회전하거나, 오른쪽으로 회전
69+
// 왼쪽 혹은 오른쪽 두 칸이 모두 비어 있다면
70+
if (board[pos.getPos1X()][pos.getPos1Y() + ver[i]] == 0 && board[pos.getPos2X()][pos.getPos2Y() + ver[i]] == 0) {
71+
nextPos.add(new Node(pos.getPos1X(), pos.getPos1Y(), pos.getPos1X(), pos.getPos1Y() + ver[i], pos.getDistance() + 1));
72+
nextPos.add(new Node(pos.getPos2X(), pos.getPos2Y(), pos.getPos2X(), pos.getPos2Y() + ver[i], pos.getDistance() + 1));
73+
}
74+
}
75+
}
76+
// 현재 위치에서 이동할 수 있는 위치를 반환
77+
return nextPos;
78+
}
79+
80+
public int solution(int[][] board) {
81+
// 맵 외곽에 벽을 두는 형태로 맵 변형
82+
int n = board.length;
83+
int[][] newBoard = new int[n + 2][n + 2];
84+
for (int i = 0; i < n + 2; i++) {
85+
for (int j = 0; j < n + 2; j++) {
86+
newBoard[i][j] = 1;
87+
}
88+
}
89+
for (int i = 0; i < n; i++) {
90+
for (int j = 0; j < n; j++) {
91+
newBoard[i + 1][j + 1] = board[i][j];
92+
}
93+
}
94+
// 너비 우선 탐색(BFS) 수행
95+
Queue<Node> q = new LinkedList<>();
96+
ArrayList<Node> visited = new ArrayList<>();
97+
Node pos = new Node(1, 1, 1, 2, 0); // 시작 위치 설정
98+
q.offer(pos); // 큐에 삽입한 뒤에
99+
visited.add(pos); // 방문 처리
100+
// 큐가 빌 때까지 반복
101+
while (!q.isEmpty()) {
102+
pos = q.poll();
103+
// (n, n) 위치에 로봇이 도달했다면, 최단 거리이므로 반환
104+
if ((pos.getPos1X() == n && pos.getPos1Y() == n) || (pos.getPos2X() == n && pos.getPos2Y() == n)) {
105+
return pos.getDistance();
106+
}
107+
// 현재 위치에서 이동할 수 있는 위치 확인
108+
ArrayList<Node> nextPos = getNextPos(pos, newBoard);
109+
for (int i = 0; i < nextPos.size(); i++) {
110+
// 아직 방문하지 않은 위치라면 큐에 삽입하고 방문 처리
111+
boolean check = true;
112+
pos = nextPos.get(i);
113+
for (int j = 0; j < visited.size(); j++) {
114+
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()) {
115+
check = false;
116+
break;
117+
}
118+
}
119+
if (check) {
120+
q.offer(pos);
121+
visited.add(pos);
122+
}
123+
}
124+
}
125+
return 0;
126+
}
127+
}

0 commit comments

Comments
 (0)