Skip to content

Commit d19fd21

Browse files
committed
[Gold II] Title: 가스관, Time: 8 ms, Memory: 79516 KB -BaekjoonHub
1 parent 01584f3 commit d19fd21

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# [Gold II] 가스관 - 2931
2+
3+
[문제 링크](https://www.acmicpc.net/problem/2931)
4+
5+
### 성능 요약
6+
7+
메모리: 79516 KB, 시간: 8 ms
8+
9+
### 분류
10+
11+
구현, 시뮬레이션
12+
13+
### 제출 일자
14+
15+
2025년 12월 1일 18:27:21
16+
17+
### 문제 설명
18+
19+
<p>러시아 가스를 크로아티아로 운반하기 위해 자그레브와 모스코바는 파이프라인을 디자인하고 있다. 두 사람은 실제 디자인을 하기 전에 파이프 매니아 게임을 이용해서 설계를 해보려고 한다.</p>
20+
21+
<p>이 게임에서 유럽은 R행 C열로 나누어져 있다. 각 칸은 비어있거나, 아래 그림과 같은 일곱가지 기본 블록으로 이루어져 있다.</p>
22+
23+
<table class="table table-bordered td-center">
24+
<tbody>
25+
<tr>
26+
<td><img alt="" src="https://upload.acmicpc.net/3a92cfe2-8d8f-4059-b4e1-1d23b2e7df12/-/crop/73x118/29,0/-/preview/" style="width: 37px; height: 59px;"></td>
27+
<td><img alt="" src="https://upload.acmicpc.net/3a92cfe2-8d8f-4059-b4e1-1d23b2e7df12/-/crop/127x118/168,0/-/preview/" style="width: 64px; height: 59px;"></td>
28+
<td><img alt="" src="https://upload.acmicpc.net/3a92cfe2-8d8f-4059-b4e1-1d23b2e7df12/-/crop/116x118/339,0/-/preview/" style="width: 58px; height: 59px;"></td>
29+
<td><img alt="" src="https://upload.acmicpc.net/3a92cfe2-8d8f-4059-b4e1-1d23b2e7df12/-/crop/91x118/519,0/-/preview/" style="width: 46px; height: 59px;"></td>
30+
<td><img alt="" src="https://upload.acmicpc.net/3a92cfe2-8d8f-4059-b4e1-1d23b2e7df12/-/crop/90x118/685,0/-/preview/" style="width: 45px; height: 59px;"></td>
31+
<td><img alt="" src="https://upload.acmicpc.net/3a92cfe2-8d8f-4059-b4e1-1d23b2e7df12/-/crop/89x118/853,0/-/preview/" style="width: 45px; height: 59px;"></td>
32+
<td><img alt="" src="https://upload.acmicpc.net/3a92cfe2-8d8f-4059-b4e1-1d23b2e7df12/-/crop/90x118/1018,0/-/preview/" style="width: 45px; height: 59px;"></td>
33+
</tr>
34+
<tr>
35+
<td>블록 '<code>|</code>'</td>
36+
<td>블록 '<code>-</code>'</td>
37+
<td>블록 '<code>+</code>'</td>
38+
<td>블록 '<code>1</code>'</td>
39+
<td>블록 '<code>2</code>'</td>
40+
<td>블록 '<code>3</code>'</td>
41+
<td>블록 '<code>4</code>'</td>
42+
</tr>
43+
</tbody>
44+
</table>
45+
46+
<p>가스는 모스크바에서 자그레브로 흐른다. 가스는 블록을 통해 양방향으로 흐를 수 있다. '<code>+</code>'는 특별한 블록으로, 아래 예시처럼 두 방향 (수직, 수평)으로 흘러야 한다.</p>
47+
48+
<p style="text-align: center;"><img alt="" src="https://upload.acmicpc.net/66956a75-fdf1-4706-923d-cb2794fc0ab9/-/preview/" style="width: 253px; height: 158px;"></p>
49+
50+
<p>파이프 라인의 설계를 마친 후 두 사람은 잠시 저녁을 먹으러 갔다. 그 사이 해커가 침임해 블록 하나를 지웠다. 지운 블록은 빈 칸이 되어있다.</p>
51+
52+
<p>해커가 어떤 칸을 지웠고, 그 칸에는 원래 어떤 블록이 있었는지 구하는 프로그램을 작성하시오.</p>
53+
54+
### 입력
55+
56+
<p>첫째 줄에 유럽의 크기 R과 C가 주어진다. (1 ≤ R, C ≤ 25)</p>
57+
58+
<p>다음 R개 줄에는 C개 글자가 주어지며, 다음과 같은 글자로 이루어져 있다.</p>
59+
60+
<ul>
61+
<li>빈칸을 나타내는 '<code>.</code>'</li>
62+
<li>블록을 나타내는 '<code>|</code>'(아스키 124), '<code>-</code>','<code>+</code>','<code>1</code>','<code>2</code>','<code>3</code>','<code>4</code>'</li>
63+
<li>모스크바의 위치를 나타내는 '<code>M</code>'과 자그레브를 나타내는 '<code>Z</code>'. 두 글자는 한 번만 주어진다.</li>
64+
</ul>
65+
66+
<p>항상 답이 존재하고, 가스의 흐름이 유일한 경우만 입력으로 주어진다, 또, 모스크바와 자그레브가 하나의 블록과 인접해 있는 입력만 주어진다. 또, 불필요한 블록이 존재하지 않는다. 즉, 없어진 블록을 추가하면, 모든 블록에 가스가 흐르게 된다.</p>
67+
68+
### 출력
69+
70+
<p>지워진 블록의 행과 열 위치를 출력하고, 어떤 블록이었는지를 출력한다.</p>
71+
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import Foundation
2+
3+
let input = readLine()!.split(separator: " ").map { Int($0)! }
4+
let R = input[0], C = input[1]
5+
6+
var board = Array(repeating: Array(repeating: Character("."), count: C), count: R)
7+
var notConnected = Array(
8+
repeating: Array(repeating: [Bool](repeating: false, count: 4), count: C),
9+
count: R
10+
)
11+
12+
let blocks: [Character] = ["|", "-", "+", "1", "2", "3", "4"]
13+
14+
let dr = [-1, 0, 1, 0]
15+
let dc = [0, -1, 0, 1]
16+
17+
let adjs: [[Bool]] = [
18+
[true, false, true, false], // |
19+
[false, true, false, true], // -
20+
[true, true, true, true], // +
21+
[false, false, true, true], // 1
22+
[true, false, false, true], // 2
23+
[true, true, false, false], // 3
24+
[false, true, true, false] // 4
25+
]
26+
27+
func getAdjacent(_ r: Int, _ c: Int) -> [Bool] {
28+
switch board[r][c] {
29+
case "|": return adjs[0]
30+
case "-": return adjs[1]
31+
case "+": return adjs[2]
32+
case "1": return adjs[3]
33+
case "2": return adjs[4]
34+
case "3": return adjs[5]
35+
case "4": return adjs[6]
36+
default: return []
37+
}
38+
}
39+
40+
func checkConnected(_ r: Int, _ c: Int) {
41+
let adj = getAdjacent(r, c)
42+
43+
for i in 0..<4 {
44+
if !adj[i] { continue }
45+
46+
let nr = r + dr[i]
47+
let nc = c + dc[i]
48+
49+
if nr < 0 || nr >= R || nc < 0 || nc >= C { continue }
50+
51+
if board[nr][nc] == "." {
52+
notConnected[nr][nc][(i + 2) % 4] = true
53+
}
54+
}
55+
}
56+
57+
func calcBlock(_ r: Int, _ c: Int) -> Character {
58+
for i in 0..<7 {
59+
if notConnected[r][c] == adjs[i] {
60+
return blocks[i]
61+
}
62+
}
63+
return "?"
64+
}
65+
66+
for i in 0..<R {
67+
let line = Array(readLine()!)
68+
board[i] = line
69+
}
70+
71+
for i in 0..<R {
72+
for j in 0..<C {
73+
let ch = board[i][j]
74+
if ch == "." || ch == "M" || ch == "Z" { continue }
75+
checkConnected(i, j)
76+
}
77+
}
78+
79+
for i in 0..<R {
80+
for j in 0..<C {
81+
for k in 0..<4 {
82+
if notConnected[i][j][k] {
83+
let block = calcBlock(i, j)
84+
print(i + 1, j + 1, block)
85+
exit(0)
86+
}
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)