Skip to content

Commit 55a3b63

Browse files
committed
[Gold III] Title: 소가 길을 건너간 이유 6, Time: 24 ms, Memory: 80176 KB -BaekjoonHub
1 parent 1171b2e commit 55a3b63

2 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [Gold III] 소가 길을 건너간 이유 6 - 14466
2+
3+
[문제 링크](https://www.acmicpc.net/problem/14466)
4+
5+
### 성능 요약
6+
7+
메모리: 80176 KB, 시간: 24 ms
8+
9+
### 분류
10+
11+
그래프 이론, 그래프 탐색, 너비 우선 탐색, 격자 그래프
12+
13+
### 제출 일자
14+
15+
2025년 12월 23일 18:07:48
16+
17+
### 문제 설명
18+
19+
<p>소가 길을 건너간 이유는 그냥 길이 많아서이다. 존의 농장에는 길이 너무 많아서, 길을 건너지 않고서는 별로 돌아다닐 수가 없다.</p>
20+
21+
<p>존의 농장에 대대적인 개편이 있었다. 이제 작은 정사각형 목초지가 N×N (2 ≤ N ≤ 100) 격자로 이루어져 있다. 인접한 목초지 사이는 일반적으로 자유롭게 건너갈 수 있지만, 그 중 일부는 길을 건너야 한다. <a href="https://www.acmicpc.net/problem/14469">농장의 바깥에는 높은 울타리</a>가 있어서 소가 농장 밖으로 나갈 일은 없다.</p>
22+
23+
<p>K마리의 (1 ≤ K ≤ 100,K ≤ N<sup>2</sup>) 소가 존의 농장에 있고, 각 소는 서로 다른 목초지에 있다. 어떤 두 소는 길을 건너지 않으면 만나지 못 할 수 있다. 이런 소가 몇 쌍인지 세어보자.</p>
24+
25+
### 입력
26+
27+
<p>첫 줄에 N, K, R이 주어진다. 다음 R줄에는 한 줄에 하나씩 길이 주어진다. 길은 상하좌우로 인접한 두 목초지를 잇고, r c r′ c′의 형태 (행, 열, 행, 열)로 주어진다. 각 수는 1 이상 N 이하이다. 그 다음 K줄에는 한 줄의 하나씩 소의 위치가 행과 열로 주어진다.</p>
28+
29+
### 출력
30+
31+
<p>길을 건너지 않으면 만날 수 없는 소가 몇 쌍인지 출력한다.</p>
32+
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import Foundation
2+
3+
let input = readLine()!.split(separator: " ").map { Int($0)! }
4+
let N = input[0]
5+
let K = input[1]
6+
let R = input[2]
7+
8+
let dr = [-1, 1, 0, 0]
9+
let dc = [0, 0, -1, 1]
10+
11+
var blocked = Array(
12+
repeating: Array(
13+
repeating: Array(repeating: false, count: 4),
14+
count: N
15+
),
16+
count: N
17+
)
18+
19+
for _ in 0..<R {
20+
let road = readLine()!.split(separator: " ").map { Int($0)! - 1 }
21+
let r1 = road[0], c1 = road[1]
22+
let r2 = road[2], c2 = road[3]
23+
24+
if r1 == r2 {
25+
if c1 < c2 {
26+
blocked[r1][c1][3] = true
27+
blocked[r2][c2][2] = true
28+
} else {
29+
blocked[r1][c1][2] = true
30+
blocked[r2][c2][3] = true
31+
}
32+
} else {
33+
if r1 < r2 {
34+
blocked[r1][c1][1] = true
35+
blocked[r2][c2][0] = true
36+
} else {
37+
blocked[r1][c1][0] = true
38+
blocked[r2][c2][1] = true
39+
}
40+
}
41+
}
42+
43+
var cows: [(Int, Int)] = []
44+
for _ in 0..<K {
45+
let pos = readLine()!.split(separator: " ").map { Int($0)! - 1 }
46+
cows.append((pos[0], pos[1]))
47+
}
48+
49+
var component = Array(repeating: Array(repeating: -1, count: N), count: N)
50+
var compId = 0
51+
52+
for r in 0..<N {
53+
for c in 0..<N {
54+
if component[r][c] != -1 { continue }
55+
56+
var queue = [(r, c)]
57+
component[r][c] = compId
58+
var idx = 0
59+
60+
while idx < queue.count {
61+
let (cr, cc) = queue[idx]
62+
idx += 1
63+
64+
for d in 0..<4 {
65+
if blocked[cr][cc][d] { continue }
66+
let nr = cr + dr[d]
67+
let nc = cc + dc[d]
68+
69+
if nr < 0 || nr >= N || nc < 0 || nc >= N { continue }
70+
if component[nr][nc] != -1 { continue }
71+
72+
component[nr][nc] = compId
73+
queue.append((nr, nc))
74+
}
75+
}
76+
77+
compId += 1
78+
}
79+
}
80+
81+
var cowCount = Array(repeating: 0, count: compId)
82+
for (r, c) in cows {
83+
cowCount[component[r][c]] += 1
84+
}
85+
86+
let totalPairs = K * (K - 1) / 2
87+
88+
var sameComponentPairs = 0
89+
for count in cowCount {
90+
sameComponentPairs += count * (count - 1) / 2
91+
}
92+
93+
print(totalPairs - sameComponentPairs)

0 commit comments

Comments
 (0)