|
| 1 | +# [Gold II] 통나무 옮기기 - 1938 |
| 2 | + |
| 3 | +[문제 링크](https://www.acmicpc.net/problem/1938) |
| 4 | + |
| 5 | +### 성능 요약 |
| 6 | + |
| 7 | +메모리: 2052 KB, 시간: 0 ms |
| 8 | + |
| 9 | +### 분류 |
| 10 | + |
| 11 | +구현, 그래프 이론, 그래프 탐색, 너비 우선 탐색 |
| 12 | + |
| 13 | +### 제출 일자 |
| 14 | + |
| 15 | +2025년 11월 17일 11:37:32 |
| 16 | + |
| 17 | +### 문제 설명 |
| 18 | + |
| 19 | +<p>가로와 세로의 길이가 같은 평지에서 벌목을 한다. 그 지형은 0과 1로 나타나 있다. 1은 아직 잘려지지 않은 나무를 나타내고 0은 아무 것도 없음을 나타낸다. 다음 지형을 보자.</p> |
| 20 | + |
| 21 | +<pre><strong>B</strong> 0 0 1 1 |
| 22 | +<strong>B</strong> 0 0 0 0 |
| 23 | +<strong>B</strong> 0 0 0 0 |
| 24 | +1 1 0 0 0 |
| 25 | +<strong>E</strong> <strong>E</strong> <strong>E</strong> 0 0</pre> |
| 26 | + |
| 27 | +<p>위의 지형에서 길이 3인 통나무 BBB를 밀거나 회전시켜 EEE의 위치로 옮기는 작업을 하는 문제를 생각해 보자. BBB와 EEE의 위치는 임의로 주어진다. 단 문제에서 통나무의 길이는 항상 3이며 B의 개수와 E의 개수는 같다. 통나무를 움직이는 방법은 아래와 같이 상하좌우(Up, Down, Left, Right)와 회전(Turn)이 있다.</p> |
| 28 | + |
| 29 | +<table class="table table-bordered" style="width:50%"> |
| 30 | + <thead> |
| 31 | + <tr> |
| 32 | + <th style="text-align: center;">코드</th> |
| 33 | + <th style="text-align: center;">의미</th> |
| 34 | + </tr> |
| 35 | + </thead> |
| 36 | + <tbody> |
| 37 | + <tr> |
| 38 | + <td style="text-align:center"><code>U</code></td> |
| 39 | + <td>통나무를 위로 한 칸 옮긴다.</td> |
| 40 | + </tr> |
| 41 | + <tr> |
| 42 | + <td style="text-align:center"><code>D</code></td> |
| 43 | + <td>통나무를 아래로 한 칸 옮긴다.</td> |
| 44 | + </tr> |
| 45 | + <tr> |
| 46 | + <td style="text-align:center"><code>L</code></td> |
| 47 | + <td>통나무를 왼쪽으로 한 칸 옮긴다.</td> |
| 48 | + </tr> |
| 49 | + <tr> |
| 50 | + <td style="text-align:center"><code>R</code></td> |
| 51 | + <td>통나무를 오른쪽으로 한 칸 옮긴다.</td> |
| 52 | + </tr> |
| 53 | + <tr> |
| 54 | + <td style="text-align:center"><code>T</code></td> |
| 55 | + <td>중심점을 중심으로 90도 회전시킨다.</td> |
| 56 | + </tr> |
| 57 | + </tbody> |
| 58 | +</table> |
| 59 | + |
| 60 | +<p>예를 들면, 다음과 같다. (초기상태로부터의 이동)</p> |
| 61 | + |
| 62 | +<table class="table table-bordered" style="width:100%"> |
| 63 | + <thead> |
| 64 | + <tr> |
| 65 | + <th style="text-align: center;">초기상태</th> |
| 66 | + <th style="text-align: center;">상(<code>U</code>)</th> |
| 67 | + <th style="text-align: center;">하(<code>D</code>)</th> |
| 68 | + <th style="text-align: center;">좌(<code>L</code>)</th> |
| 69 | + <th style="text-align: center;">우(<code>R</code>)</th> |
| 70 | + <th style="text-align: center;">회전(<code>T</code>)</th> |
| 71 | + </tr> |
| 72 | + </thead> |
| 73 | + <tbody> |
| 74 | + <tr> |
| 75 | + <td> |
| 76 | + <pre style="text-align:center">0 0 0 0 0 0 |
| 77 | +0 0 0 0 0 0 |
| 78 | +0 0 0 0 0 0 |
| 79 | +0 <strong>B B B</strong> 0 0 |
| 80 | +0 0 0 0 0 0 |
| 81 | +0 0 0 1 0 0</pre> |
| 82 | + </td> |
| 83 | + <td> |
| 84 | + <pre style="text-align:center">0 0 0 0 0 0 |
| 85 | +0 0 0 0 0 0 |
| 86 | +0 <strong>B B B</strong> 0 0 |
| 87 | +0 0 0 0 0 0 |
| 88 | +0 0 0 0 0 0 |
| 89 | +0 0 0 1 0 0</pre> |
| 90 | + </td> |
| 91 | + <td> |
| 92 | + <pre style="text-align:center">0 0 0 0 0 0 |
| 93 | +0 0 0 0 0 0 |
| 94 | +0 0 0 0 0 0 |
| 95 | +0 0 0 0 0 0 |
| 96 | +0 <strong>B B B</strong> 0 0 |
| 97 | +0 0 0 1 0 0</pre> |
| 98 | + </td> |
| 99 | + <td> |
| 100 | + <pre style="text-align:center">0 0 0 0 0 0 |
| 101 | +0 0 0 0 0 0 |
| 102 | +0 0 0 0 0 0 |
| 103 | +<strong>B B B</strong> 0 0 0 |
| 104 | +0 0 0 0 0 0 |
| 105 | +0 0 0 1 0 0</pre> |
| 106 | + </td> |
| 107 | + <td> |
| 108 | + <pre style="text-align:center">0 0 0 0 0 0 |
| 109 | +0 0 0 0 0 0 |
| 110 | +0 0 0 0 0 0 |
| 111 | +0 0 <strong>B B B</strong> 0 |
| 112 | +0 0 0 0 0 0 |
| 113 | +0 0 0 1 0 0</pre> |
| 114 | + </td> |
| 115 | + <td> |
| 116 | + <pre style="text-align:center">0 0 0 0 0 0 |
| 117 | +0 0 0 0 0 0 |
| 118 | +0 0 <strong>B</strong> 0 0 0 |
| 119 | +0 0 <strong>B</strong> 0 0 0 |
| 120 | +0 0 <strong>B</strong> 0 0 0 |
| 121 | +0 0 0 1 0 0</pre> |
| 122 | + </td> |
| 123 | + </tr> |
| 124 | + </tbody> |
| 125 | +</table> |
| 126 | + |
| 127 | +<p>이와 같은 방식으로 이동시킬 때에 그 움직일 위치에 다른 나무, 즉 1이 없어야만 움직일 수 있다. 그리고 움직임은 위의 그림과 같이 한 번에 한 칸씩만 움직인다. 단 움직이는 통나무는 어떤 경우이든지 중간단계에서 한 행이나 한 열에만 놓일 수 있다. 예를 들면 아래 그림에서 a와 같은 단계는 불가능하다. 그리고 회전의 경우에서는 반드시 중심점을 중심으로 90도 회전을 해야 한다. (항상 통나무의 길이가 3이므로 중심점이 있음)</p> |
| 128 | + |
| 129 | +<p>그리고 이런 회전(Turn)이 가능하기 위해서는 그 통나무를 둘러싸고 있는 3*3 정사각형의 구역에 단 한 그루의 나무도 없어야만 한다. 즉, 아래그림 b, d와 같이 ?로 표시된 지역에 다른 나무, 즉 1이 없어야만 회전시킬 수 있다. 따라서 c와 같은 경우에, 통나무는 왼쪽 아직 벌채되지 않은 나무 때문에 회전시킬 수 없다.</p> |
| 130 | + |
| 131 | +<table class="table table-bordered" style="width:100%"> |
| 132 | + <thead> |
| 133 | + <tr> |
| 134 | + <th style="text-align: center;">a</th> |
| 135 | + <th style="text-align: center;">b</th> |
| 136 | + <th style="text-align: center;">c</th> |
| 137 | + <th style="text-align: center;">d</th> |
| 138 | + </tr> |
| 139 | + </thead> |
| 140 | + <tbody> |
| 141 | + <tr> |
| 142 | + <td> |
| 143 | + <pre style="text-align:center">0 0 0 0 0 0 |
| 144 | +0 0 0 0 0 0 |
| 145 | +0 0 0 B 0 0 |
| 146 | +0 0 B 0 0 0 |
| 147 | +0 B 0 0 0 0 |
| 148 | +0 0 0 1 0 0</pre> |
| 149 | + </td> |
| 150 | + <td> |
| 151 | + <pre style="text-align:center">0 0 0 0 0 0 |
| 152 | +0 0 0 0 0 0 |
| 153 | +0 0 ? ? ? 0 |
| 154 | +0 0 <strong>B B B </strong>0 |
| 155 | +0 0 ? ? ? 0 |
| 156 | +0 0 0 0 0 0</pre> |
| 157 | + </td> |
| 158 | + <td> |
| 159 | + <pre style="text-align:center">0 0 0 0 0 0 |
| 160 | +0 0 0 0 0 0 |
| 161 | +0 0 1 <strong>B</strong> 0 0 |
| 162 | +0 0 0 <strong>B</strong> 0 0 |
| 163 | +0 0 0 <strong>B</strong> 0 0 |
| 164 | +0 0 0 0 0 0</pre> |
| 165 | + </td> |
| 166 | + <td> |
| 167 | + <pre style="text-align:center">0 0 0 0 0 0 |
| 168 | +0 0 0 0 0 0 |
| 169 | +0 0 ? <strong>B</strong> ? 0 |
| 170 | +0 0 ? <strong>B</strong> ? 0 |
| 171 | +0 0 ? <strong>B</strong> ? 0 |
| 172 | +0 0 0 0 0 0</pre> |
| 173 | + </td> |
| 174 | + </tr> |
| 175 | + </tbody> |
| 176 | +</table> |
| 177 | + |
| 178 | +<p>문제는 통나무를 5개의 기본동작(<code>U</code>, <code>D</code>, <code>L</code>, <code>R</code>, <code>T</code>)만을 사용하여 처음위치(BBB)에서 최종위치(EEE)로 옮기는 프로그램을 작성하는 것이다. 단, 최소 횟수의 단위 동작을 사용해야 한다.</p> |
| 179 | + |
| 180 | +### 입력 |
| 181 | + |
| 182 | + <p>첫째 줄에 주어진 평지의 한 변의 길이 N이 주어진다. (4 ≤ N ≤ 50) 주어진다. 이어서 그 지형의 정보가 0, 1, B, E로 이루어진 문자열로 주어진다. 한 줄에 입력되는 문자열의 길이는 N이며 입력 문자 사이에는 빈칸이 없다. 통나무와 최종 위치의 개수는 1개이다.</p> |
| 183 | + |
| 184 | +### 출력 |
| 185 | + |
| 186 | + <p>첫째 줄에 최소 동작 횟수를 출력한다. 이동이 불가능하면 0만을 출력한다.</p> |
| 187 | + |
0 commit comments