Skip to content

Commit 64c2d1c

Browse files
committed
[Gold II] Title: Maaaaaaaaaze, Time: 152 ms, Memory: 79520 KB -BaekjoonHub
1 parent 0344683 commit 64c2d1c

2 files changed

Lines changed: 195 additions & 0 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import Foundation
2+
3+
let SIZE = 5
4+
let INF = 1_000_000
5+
6+
var boards = Array(repeating: Array(repeating: Array(repeating: 0, count: SIZE), count: SIZE), count: SIZE)
7+
8+
for k in 0..<SIZE {
9+
for i in 0..<SIZE {
10+
let line = readLine()!.split(separator: " ").map { Int($0)! }
11+
for j in 0..<SIZE {
12+
boards[k][i][j] = line[j]
13+
}
14+
}
15+
}
16+
17+
func rotate(_ board: [[Int]]) -> [[Int]] {
18+
var result = Array(repeating: Array(repeating: 0, count: SIZE), count: SIZE)
19+
for i in 0..<SIZE {
20+
for j in 0..<SIZE {
21+
result[j][SIZE - 1 - i] = board[i][j]
22+
}
23+
}
24+
return result
25+
}
26+
27+
var rotated = Array(
28+
repeating: Array(
29+
repeating: Array(repeating: Array(repeating: 0, count: SIZE), count: SIZE),
30+
count: 4
31+
),
32+
count: SIZE
33+
)
34+
35+
for i in 0..<SIZE {
36+
rotated[i][0] = boards[i]
37+
for r in 1..<4 {
38+
rotated[i][r] = rotate(rotated[i][r - 1])
39+
}
40+
}
41+
42+
let dx = [1, -1, 0, 0, 0, 0]
43+
let dy = [0, 0, 1, -1, 0, 0]
44+
let dz = [0, 0, 0, 0, 1, -1]
45+
46+
func bfs(_ cube: [[[Int]]]) -> Int {
47+
if cube[0][0][0] == 0 || cube[4][4][4] == 0 {
48+
return INF
49+
}
50+
51+
var visited = Array(
52+
repeating: Array(
53+
repeating: Array(repeating: false, count: SIZE),
54+
count: SIZE),
55+
count: SIZE
56+
)
57+
58+
var queue: [(Int, Int, Int, Int)] = [(0, 0, 0, 0)]
59+
visited[0][0][0] = true
60+
var idx = 0
61+
62+
while idx < queue.count {
63+
let (z, x, y, dist) = queue[idx]
64+
idx += 1
65+
66+
if z == 4 && x == 4 && y == 4 {
67+
return dist
68+
}
69+
70+
for d in 0..<6 {
71+
let nz = z + dz[d]
72+
let nx = x + dx[d]
73+
let ny = y + dy[d]
74+
75+
if nz < 0 || nx < 0 || ny < 0 || nz >= SIZE || nx >= SIZE || ny >= SIZE {
76+
continue
77+
}
78+
79+
if !visited[nz][nx][ny] && cube[nz][nx][ny] == 1 {
80+
visited[nz][nx][ny] = true
81+
queue.append((nz, nx, ny, dist + 1))
82+
}
83+
}
84+
}
85+
86+
return INF
87+
}
88+
89+
var used = Array(repeating: false, count: SIZE)
90+
var order = Array(repeating: 0, count: SIZE)
91+
var answer = INF
92+
93+
func permute(_ depth: Int) {
94+
if depth == SIZE {
95+
for r0 in 0..<4 {
96+
for r1 in 0..<4 {
97+
for r2 in 0..<4 {
98+
for r3 in 0..<4 {
99+
for r4 in 0..<4 {
100+
var cube = Array(
101+
repeating: Array(
102+
repeating: Array(repeating: 0, count: SIZE),
103+
count: SIZE),
104+
count: SIZE
105+
)
106+
107+
let rotations = [r0, r1, r2, r3, r4]
108+
109+
for z in 0..<SIZE {
110+
cube[z] = rotated[order[z]][rotations[z]]
111+
}
112+
113+
let result = bfs(cube)
114+
answer = min(answer, result)
115+
116+
if answer == 12 {
117+
print(12)
118+
exit(0)
119+
}
120+
}
121+
}
122+
}
123+
}
124+
}
125+
return
126+
}
127+
128+
for i in 0..<SIZE {
129+
if !used[i] {
130+
used[i] = true
131+
order[depth] = i
132+
permute(depth + 1)
133+
used[i] = false
134+
}
135+
}
136+
}
137+
138+
permute(0)
139+
140+
print(answer == INF ? -1 : answer)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# [Gold II] Maaaaaaaaaze - 16985
2+
3+
[문제 링크](https://www.acmicpc.net/problem/16985)
4+
5+
### 성능 요약
6+
7+
메모리: 79520 KB, 시간: 152 ms
8+
9+
### 분류
10+
11+
구현, 그래프 이론, 브루트포스 알고리즘, 그래프 탐색, 너비 우선 탐색
12+
13+
### 제출 일자
14+
15+
2025년 12월 30일 17:45:39
16+
17+
### 문제 설명
18+
19+
<p>평화롭게 문제를 경작하며 생활하는 BOJ 마을 사람들은 더 이상 2차원 미로에 흥미를 느끼지 않는다. 2차원 미로는 너무나 쉽게 탈출이 가능하기 때문이다. 미로를 이 세상 그 누구보다 사랑하는 준현이는 이런 상황을 매우 안타깝게 여겨 아주 큰 상금을 걸고 BOJ 마을 사람들의 관심을 확 끌 수 있는 3차원 미로 탈출 대회를 개최하기로 했다.</p>
20+
21+
<p>대회의 규칙은 아래와 같다.</p>
22+
23+
<ul>
24+
<li>5×5 크기의 판이 5개 주어진다. 이중 일부 칸은 참가자가 들어갈 수 있고 일부 칸은 참가자가 들어갈 수 없다. 그림에서 하얀 칸은 참가자가 들어갈 수 있는 칸을, 검은 칸은 참가자가 들어갈 수 없는 칸을 의미한다.</li>
25+
</ul>
26+
27+
<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/cd5b0b7c-6030-4c55-8776-2c7561cd5a73/-/preview/" style="width: 960px; height: 146px;"></p>
28+
29+
<ul>
30+
<li>참가자는 주어진 판들을 시계 방향, 혹은 반시계 방향으로 자유롭게 회전할 수 있다. 그러나 판을 뒤집을 수는 없다.</li>
31+
</ul>
32+
33+
<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/30dd4bb6-660a-4294-8dc3-a7cc348c307e/-/preview/" style="width: 960px; height: 161px;"></p>
34+
35+
<ul>
36+
<li>회전을 완료한 후 참가자는 판 5개를 쌓는다. 판을 쌓는 순서는 참가자가 자유롭게 정할 수 있다. 이렇게 판 5개를 쌓아 만들어진 5×5×5 크기의 큐브가 바로 참가자를 위한 미로이다. 이 때 큐브의 입구는 정육면체에서 참가자가 임의로 선택한 꼭짓점에 위치한 칸이고 출구는 입구와 면을 공유하지 않는 꼭짓점에 위치한 칸이다.</li>
37+
</ul>
38+
39+
<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/6b66d051-d9fa-43a1-8cf1-53a635f9217d/-/preview/" style="width: 600px; height: 567px;"></p>
40+
41+
<ul>
42+
<li>참가자는 현재 위치한 칸에서 면으로 인접한 칸이 참가자가 들어갈 수 있는 칸인 경우 그 칸으로 이동할 수 있다.</li>
43+
<li>참가자 중에서 본인이 설계한 미로를 가장 적은 이동 횟수로 탈출한 사람이 우승한다. 만약 미로의 입구 혹은 출구가 막혀있거나, 입구에서 출구에 도달할 수 있는 방법이 존재하지 않을 경우에는 탈출이 불가능한 것으로 간주한다.</li>
44+
</ul>
45+
46+
<p>이 대회에서 우승하기 위해서는 미로를 잘 빠져나올 수 있기 위한 담력 증진과 체력 훈련, 그리고 적절한 운이 제일 중요하지만, 가장 적은 이동 횟수로 출구에 도달할 수 있게끔 미로를 만드는 능력 또한 없어서는 안 된다. 주어진 판에서 가장 적은 이동 횟수로 출구에 도달할 수 있게끔 미로를 만들었을 때 몇 번 이동을 해야하는지 구해보자. </p>
47+
48+
### 입력
49+
50+
<p>첫째 줄부터 25줄에 걸쳐 판이 주어진다. 각 판은 5줄에 걸쳐 주어지며 각 줄에는 5개의 숫자가 빈칸을 사이에 두고 주어진다. 0은 참가자가 들어갈 수 없는 칸, 1은 참가자가 들어갈 수 있는 칸을 의미한다.</p>
51+
52+
### 출력
53+
54+
<p>첫째 줄에 주어진 판으로 설계된 미로를 탈출하는 가장 적은 이동 횟수를 출력한다. 단, 어떻게 설계하더라도 탈출이 불가능할 경우에는 -1을 출력한다.</p>
55+

0 commit comments

Comments
 (0)