|
| 1 | +importjava.util.*; |
| 2 | + |
| 3 | +classNode { |
| 4 | +privateintpos1X; |
| 5 | +privateintpos1Y; |
| 6 | +privateintpos2X; |
| 7 | +privateintpos2Y; |
| 8 | +privateintdistance; |
| 9 | + |
| 10 | +publicintgetPos1X() { |
| 11 | +returnthis.pos1X; |
| 12 | + } |
| 13 | +publicintgetPos1Y() { |
| 14 | +returnthis.pos1Y; |
| 15 | + } |
| 16 | +publicintgetPos2X() { |
| 17 | +returnthis.pos2X; |
| 18 | + } |
| 19 | +publicintgetPos2Y() { |
| 20 | +returnthis.pos2Y; |
| 21 | + } |
| 22 | +publicintgetDistance() { |
| 23 | +returnthis.distance; |
| 24 | + } |
| 25 | + |
| 26 | +publicNode(intpos1X,intpos1Y,intpos2X,intpos2Y,intdistance) { |
| 27 | +this.pos1X =pos1X; |
| 28 | +this.pos1Y =pos1Y; |
| 29 | +this.pos2X =pos2X; |
| 30 | +this.pos2Y =pos2Y; |
| 31 | +this.distance =distance; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +classSolution { |
| 36 | + |
| 37 | +publicArrayList<Node>getNextPos(Nodepos,int[][]board) { |
| 38 | +// 반환 결과(이동 가능한 위치들) |
| 39 | +ArrayList<Node>nextPos =newArrayList<Node>(); |
| 40 | +// (상, 하, 좌, 우)로 이동하는 경우에 대해서 처리 |
| 41 | +int[]dx = {-1,1,0,0}; |
| 42 | +int[]dy = {0,0, -1,1}; |
| 43 | +for (inti =0;i <4;i++) { |
| 44 | +intpos1NextX =pos.getPos1X() +dx[i]; |
| 45 | +intpos1NextY =pos.getPos1Y() +dy[i]; |
| 46 | +intpos2NextX =pos.getPos2X() +dx[i]; |
| 47 | +intpos2NextY =pos.getPos2Y() +dy[i]; |
| 48 | +intdistanceNext =pos.getDistance() +1; |
| 49 | +// 이동하고자 하는 두 칸이 모두 비어 있다면 |
| 50 | +if (board[pos1NextX][pos1NextY] ==0 &&board[pos2NextX][pos2NextY] ==0) { |
| 51 | +nextPos.add(newNode(pos1NextX,pos1NextY,pos2NextX,pos2NextY,distanceNext)); |
| 52 | + } |
| 53 | + } |
| 54 | +// 현재 로봇이 가로로 놓여 있는 경우 |
| 55 | +int[]hor = {-1,1}; |
| 56 | +if (pos.getPos1X() ==pos.getPos2X()) { |
| 57 | +for (inti =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(newNode(pos.getPos1X(),pos.getPos1Y(),pos.getPos1X() +hor[i],pos.getPos1Y(),pos.getDistance() +1)); |
| 61 | +nextPos.add(newNode(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 (inti =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(newNode(pos.getPos1X(),pos.getPos1Y(),pos.getPos1X(),pos.getPos1Y() +ver[i],pos.getDistance() +1)); |
| 72 | +nextPos.add(newNode(pos.getPos2X(),pos.getPos2Y(),pos.getPos2X(),pos.getPos2Y() +ver[i],pos.getDistance() +1)); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +// 현재 위치에서 이동할 수 있는 위치를 반환 |
| 77 | +returnnextPos; |
| 78 | + } |
| 79 | + |
| 80 | +publicintsolution(int[][]board) { |
| 81 | +// 맵 외곽에 벽을 두는 형태로 맵 변형 |
| 82 | +intn =board.length; |
| 83 | +int[][]newBoard =newint[n +2][n +2]; |
| 84 | +for (inti =0;i <n +2;i++) { |
| 85 | +for (intj =0;j <n +2;j++) { |
| 86 | +newBoard[i][j] =1; |
| 87 | + } |
| 88 | + } |
| 89 | +for (inti =0;i <n;i++) { |
| 90 | +for (intj =0;j <n;j++) { |
| 91 | +newBoard[i +1][j +1] =board[i][j]; |
| 92 | + } |
| 93 | + } |
| 94 | +// 너비 우선 탐색(BFS) 수행 |
| 95 | +Queue<Node>q =newLinkedList<>(); |
| 96 | +ArrayList<Node>visited =newArrayList<>(); |
| 97 | +Nodepos =newNode(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 | +returnpos.getDistance(); |
| 106 | + } |
| 107 | +// 현재 위치에서 이동할 수 있는 위치 확인 |
| 108 | +ArrayList<Node>nextPos =getNextPos(pos,newBoard); |
| 109 | +for (inti =0;i <nextPos.size();i++) { |
| 110 | +// 아직 방문하지 않은 위치라면 큐에 삽입하고 방문 처리 |
| 111 | +booleancheck =true; |
| 112 | +pos =nextPos.get(i); |
| 113 | +for (intj =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 | +return0; |
| 126 | + } |
| 127 | +} |