https://www.acmicpc.net/problem/1388
1388번: 바닥 장식
형택이는 건축가이다. 지금 막 형택이는 형택이의 남자 친구 기훈이의 집을 막 완성시켰다. 형택이는 기훈이 방의 바닥 장식을 디자인했고, 이제 몇 개의 나무 판자가 필요한지 궁금해졌다. 나
www.acmicpc.net
그래프 탐색문색 문제로 BFS, DFS를 사용해서 풀어도 되지만 입력되는 바닥의 크기가 작고, 시간제한이 여유로운 편이어서 그래프를 x축 탐색, y축으로 탐색하여 개수를 세도 정답을 받을 수 있다.
풀이
Bool 타입의 flag변수를 사용하여 처음 마주하는 장식의 갯수를 세면 된다. 가로축 탐색은 처음 장식을 입력받을 때 수행하고, 이후 세로축으로 한번 더 탐색을 수행하면 된다.
정답 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
let xy = readLine()!.split(separator: " ").map{Int(String($0))!} | |
let x = xy[0] | |
let y = xy[1] | |
var map = Array(repeating: Array(repeating: "", count: y), count: x) | |
var flag = false | |
var cnt = 0 | |
for i in 0..<x{ | |
let woods = readLine()!.map{String($0)} | |
flag = false | |
for k in 0..<y{ | |
map[i][k] = woods[k] | |
if map[i][k]=="-" && !flag{ | |
cnt += 1 | |
flag = true | |
}else if map[i][k]=="|"{ | |
flag = false | |
} | |
} | |
} | |
for k in 0..<y{ | |
flag = false | |
for i in 0..<x{ | |
if map[i][k]=="|" && !flag{ | |
cnt += 1 | |
flag = true | |
}else if map[i][k]=="-"{ | |
flag = false | |
} | |
} | |
} | |
print(cnt) |

'Problem Solving > BOJ' 카테고리의 다른 글
[1915] 가장 큰 정사각형 (0) | 2022.11.01 |
---|---|
[1347] 미로 만들기 (0) | 2022.10.31 |
[1063] 킹 (1) | 2022.10.28 |
[16920] 확장게임 (0) | 2022.10.26 |
[9328] 열쇠 (0) | 2022.10.25 |