2022. 12. 2. 14:28ใ์นดํ ๊ณ ๋ฆฌ ์์
๋ฐฑ์ค - ์ ์์ ํฌ(Swift)
๋ฌธ์ ์ค๋ช
https://www.acmicpc.net/problem/1303
๋์ ํ์ด
๊ฐ๋จํ๋ DFS, BFS๋ฌธ์ ์์ง๋ง ํ์์ ๊บผ๋ด๋ ๊ฐ์ var๋ก ๋ ์ผ๋ก์จ ๋์ ๋์ง์์๋๋ ๋์ ๋์๊ธฐ ๋๋ฌธ์ ์๊ฐ์ด ์กฐ๊ธ ์ค๋๊ฑธ๋ ธ๋ค.
๊ทธ๋ฆฌ๊ณ ๋ญ์ณ์๋ B์ W๋ค์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ๊ฒ์ด ํฌ์ธํธ์๋๋ฐ BFS๋ก ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ค๊ณ ํ๋ ํ๋จ์ค๋ฅ๊ฐ ์์๋ค.
๊ทธ๋์ BFS๋ก ์ ๊ทผํ๋ ค๊ณ ํ๋ค. (DFS๋ BFS๋ ์๊ด์์ง๋ง ์ด ๋ฌธ์ ๋ DFS์ ์ธ ์ฌ๊ณ ๊ฐ ๋ ์ฌ๋ฐ๋ฅธ ํด๋ต์ผ๋ก ๋ณด์ธ๋ค)
๊ทธ๋ฆฌ๊ณ ๋ค๋ฅธ์ฌ๋์ ํ์ด๋ฅผ ๋ณด๋ ๊ตณ์ด WBFS์ BBFS๋ก ๋๋์ง ์๊ณ BFS๋ฅผ ํธ์ถํ ๋ G[i][j]๊ฐ๋ ๋๊ฒจ์ค์ผ๋ก์จ ์ด์ ์ ๊ฐ๊ณผ ๊ฐ์ ๊ฐ์ ๋ํด์๋ง ํ์ํ ์ ์๊ฒ ๋ง๋ค์๋ค.
import Foundation
let nm = readLine()!.components(separatedBy:" ").map{Int($0)!}
let dx = [-1, 0, 1, 0]
let dy = [0, -1, 0, 1]
let N = nm[1], M = nm[0]
let xRange = 0..<N
let yRange = 0..<M
var G = [[String]]()
var visited = Array(repeating: Array(repeating: false, count: M), count: N)
var distance = Array(repeating: Array(repeating: 0, count: M), count: N)
var blueCnt = 0
var whiteCnt = 0
for _ in 0..<N {
G.append(readLine()!.map{String($0)})
}
func BFS(_ x: Int, _ y: Int) {
if G[x][y] == "B" {
BBFS(x, y)
} else {
WBFS(x, y)
}
}
func BBFS(_ x: Int, _ y: Int) {
var cnt = 1
var index = 0
var queue = [(Int, Int)]()
var maxBlue = 1
visited[x][y] = true
queue.append((x, y))
while index < queue.count {
let curr = queue[index]
let x = curr.0, y = curr.1
index += 1
for i in 0..<4 {
let nx = x + dx[i]
let ny = y + dy[i]
if xRange ~= nx, yRange ~= ny, !visited[nx][ny], G[nx][ny] == "B" {
cnt += 1
visited[nx][ny] = true
queue.append((nx, ny))
}
}
}
blueCnt += (cnt * cnt)
}
func WBFS(_ x: Int, _ y: Int) {
var cnt = 1
var index = 0
var queue = [(Int, Int)]()
var maxWhite = 1
visited[x][y] = true
queue.append((x, y))
while index < queue.count {
let curr = queue[index]
let x = curr.0, y = curr.1
index += 1
for i in 0..<4 {
let nx = x + dx[i]
let ny = y + dy[i]
if xRange ~= nx, yRange ~= ny {
if G[nx][ny] == "W", !visited[nx][ny] {
cnt += 1
visited[nx][ny] = true
queue.append((nx, ny))
}
}
}
}
whiteCnt += (cnt * cnt)
}
for i in 0..<N {
for j in 0..<M {
if !visited[i][j] {
BFS(i, j)
}
}
}
print("\(whiteCnt) \(blueCnt)")
๋ค๋ฅธ ์ฌ๋์ ํ์ด
import Foundation
let nm = readLine()!.components(separatedBy:" ").map{Int($0)!}
let dx = [-1, 0, 1, 0]
let dy = [0, -1, 0, 1]
let N = nm[1], M = nm[0]
let xRange = 0..<N
let yRange = 0..<M
var G = [[String]]()
var visited = Array(repeating: Array(repeating: false, count: M), count: N)
var distance = Array(repeating: Array(repeating: 0, count: M), count: N)
var blueCnt = 0
var whiteCnt = 0
for _ in 0..<N {
G.append(readLine()!.map{String($0)})
}
func BFS(_ x: Int, _ y: Int, _ c: String) {
var cnt = 1
var index = 0
var queue = [(Int, Int)]()
var maxWhite = 1
visited[x][y] = true
queue.append((x, y))
while index < queue.count {
let curr = queue[index]
let x = curr.0, y = curr.1
index += 1
for i in 0..<4 {
let nx = x + dx[i]
let ny = y + dy[i]
if xRange ~= nx, yRange ~= ny {
if G[nx][ny] == c, !visited[nx][ny] {
cnt += 1
visited[nx][ny] = true
queue.append((nx, ny))
}
}
}
}
if G[x][y] == "W" {
whiteCnt += (cnt * cnt)
} else {
blueCnt += (cnt * cnt)
}
}
for i in 0..<N {
for j in 0..<M {
if !visited[i][j] {
BFS(i, j, G[i][j])
}
}
}
print("\(whiteCnt) \(blueCnt)")