๋ฐฑ์ค€ - ๊ธฐ์ƒ์บ์Šคํ„ฐ(Swift)

2022. 11. 10. 10:37ใ†Algorithm

๋ฐฑ์ค€ - ๊ธฐ์ƒ์บ์Šคํ„ฐ(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

https://www.acmicpc.net/problem/10709

 

10709๋ฒˆ: ๊ธฐ์ƒ์บ์Šคํ„ฐ

์ถœ๋ ฅ์€ H ํ–‰์œผ๋กœ, ๊ฐ ํ–‰์—๋Š” ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋œ W ๊ฐœ์˜ ์ •์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ถœ๋ ฅ์˜ i ๋ฒˆ์งธ ํ–‰ j ๋ฒˆ์งธ ์ •์ˆ˜ (1 โ‰ฆ i โ‰ฆ H, 1 โ‰ฆ j โ‰ฆ W) ๋Š”, ์ง€๊ธˆ๋ถ€ํ„ฐ ๋ช‡ ๋ถ„ํ›„์— ์ฒ˜์Œ์œผ๋กœ ๊ตฌ์—ญ (i, j) ์— ๊ตฌ๋ฆ„์ด ๋œจ๋Š”์ง€๋ฅผ ํ‘œ์‹œ

www.acmicpc.net

 

 ๋‚˜์˜ ํ’€์ด

 

๊ตฌ๋ฆ„์ด ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ๋’ค์—๋ฅผ ๋นผ์ฃผ๊ณ  ์•ž์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ์‹์œผ๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ํ˜„์žฌ๋‚ ์”จ์ƒํ™ฉ์„ ์™„์ „ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๊ตฌ๋ฆ„์ด ์žˆ์„๋•Œ ํ•œ ๋ฒˆ๋„ ๊ตฌ๋ฆ„์ด ์™”๋‹ค๊ฐ€์ง€์•Š์€ ๊ณณ์ด๋ผ๋ฉด ๊ทธ์ „ ๊ฐ’ + 1์„ ํ•จ์œผ๋กœ์จ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

import Foundation

var timeArr = [[Int]]() // ๋ˆ„์ ๋œ ๊ตฌ๋ฆ„์˜ ์ผ์ˆ˜
var cloudArr = [[Character]]() // ํ˜„์žฌ ๊ธฐํ›„์ƒํ™ฉ

let input = readLine()!.components(separatedBy:" ").map{Int($0)!}

let row = input[0], col = input[1]
for _ in 0..<row {
    let rawVal = readLine()!
    timeArr.append(rawVal.map{$0 == "." ? -1 : 0})
    cloudArr.append(rawVal.map{$0})
}

for _ in 0..<col-1 { // ์—ด์˜ ๊ฐœ์ˆ˜-1๋งŒํผ ๊ตฌ๋ฆ„์ด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
    for r in 0..<row { // ํ–‰์˜ ๊ฐœ์ˆ˜ ๋งŒํผ ๊ตฌ๋ฆ„ ์ด๋™์‹œํ‚ด
        cloudArr[r].removeLast()
        cloudArr[r].insert(".", at: 0)
    }
    for i in 0..<cloudArr.count {
        for j in 0..<cloudArr[i].count {
            if cloudArr[i][j] == "c" && timeArr[i][j] == -1 { // ํ•œ๋ฒˆ๋„ ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์€ ๊ณณ์ด๋ฉด์„œ ๊ตฌ๋ฆ„์ด ์žˆ๋Š” ๊ณณ์ด๋ผ๋ฉด
                timeArr[i][j] = timeArr[i][j-1] + 1
            }
        }
    }
}
for i in 0..<timeArr.count {
    print(timeArr[i].map{String($0)}.joined(separator: " "))
}