BOJ: #2583 - ์์ญ ๊ตฌํ๊ธฐ
์ฃผ์ด์ง๋ ์ผ์ชฝ ์๋ ๊ผญ์ง์ , ์ค๋ฅธ์ชฝ ์ ๊ผญ์ง์ ์ ๊ฐ์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ก ์นํํ๋ ๊ณผ์ ์ ๋ฐ๋ก ์๊ฐํ๊ธฐ๊ฐ ์ด๋ ค์์ ์์ผ๋ก ์ด์ฌํ ๊ณ์ฐํ๋ฉด์ ํ์๋ค. ์ด ๊ณผ์ ์ดํ์๋ ํ์ ๋ฌธ์ ์ ๋์ผํ๊ฒ ํ๋ฉด ๋๋ค. ๊ฐ ์์ญ์ ๋์ด๋ visited
์ ํฌ๊ธฐ๋ฅผ ๋ฐํํ๋ฉด ๋๋๋ฐ, ์ด ๋ ์์ธ์ง๋ ๋ชจ๋ฅด๊ฒ ์ง๋ง ์ค๋ณต๊ฐ์ด ๋ค์ด๊ฐ์ ํฌ๊ธฐ๊ฐ ๋ง์ง ์์ visited
๋ฅผ Set
์ผ๋ก ์ ์ธํ์ฌ ์ค๋ณต๊ฐ ์ฒ๋ฆฌ๋ฅผ ํด์คฌ๋ค.
๋ฌธ์
https://www.acmicpc.net/problem/2583
๋ด๊ฐ ์์ฑํ ์ฝ๋
Swift
let n = readLine()!.split(separator: " ").map { Int(String($0))! }
var board = Array(repeating: Array(repeating: 0, count: n[1]), count: n[0])
for _ in 0..<n[2] {
let area = readLine()!.split(separator: " ").map { Int(String($0))! }
let startX = n[0] - area[1] - 1
let startY = area[0]
let endX = n[0] - area[3]
let endY = area[2] - 1
for i in endX...startX {
for j in startY...endY {
board[i][j] = 1
}
}
}
var area = [Int]()
let dx = [1, 0, -1, 0]
let dy = [0, 1, 0, -1]
var count = 0
for i in 0..<n[0] {
for j in 0..<n[1] {
if board[i][j] == 0 {
var visited = Set<[Int]>()
visited.insert([i, j])
var queue = [[i, j]]
while !queue.isEmpty {
let v = queue.removeFirst()
for (x, y) in zip(dx, dy) {
let nx = x + v[0]
let ny = y + v[1]
if nx < 0 || nx >= n[0] || ny < 0 || ny >= n[1] || board[nx][ny] == 1 {
continue
}
if board[nx][ny] == 0 {
visited.insert([nx, ny])
queue.append([nx, ny])
board[nx][ny] = 1
}
}
}
area.append(visited.count)
count += 1
}
}
}
print(count)
print(area.sorted().map({ String($0) }).joined(separator: " "))
๋ฐ์ํ
๋๊ธ
์ด ๊ธ ๊ณต์ ํ๊ธฐ
-
๊ตฌ๋
ํ๊ธฐ
๊ตฌ๋ ํ๊ธฐ
-
์นด์นด์คํก
์นด์นด์คํก
-
๋ผ์ธ
๋ผ์ธ
-
ํธ์ํฐ
ํธ์ํฐ
-
Facebook
Facebook
-
์นด์นด์ค์คํ ๋ฆฌ
์นด์นด์ค์คํ ๋ฆฌ
-
๋ฐด๋
๋ฐด๋
-
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
๋ค์ด๋ฒ ๋ธ๋ก๊ทธ
-
Pocket
Pocket
-
Evernote
Evernote