๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ฏ | ๋ฐฑ์ค€/๐Ÿ˜€ | Bronze

[Kotlin, B1] ๋ฐฑ์ค€ 21771๋ฒˆ ๊ฐ€ํฌ์•ผ ๊ฑฐ๊ธฐ์„œ ์ž๋Š” ๊ฑฐ ์•„๋‹ˆ์•ผ

by immgga 2024. 9. 19.

์ถœ์ฒ˜: unsplash.com

 

๊ฐ€ํฌ์•ผ ๊ฑฐ๊ธฐ์„œ ์ž๋Š” ๊ฑฐ ์•„๋‹ˆ์•ผ(21771๋ฒˆ)

Bronze 1

#๊ตฌํ˜„ #๋ฌธ์ž์—ด

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

 

๋ฌธ์ œ ๋‚ด์šฉ

 

 

๋ฌธ์ œ ์ ‘๊ทผ

๊ฐ€ํฌ(G)์™€ ๋ฒ ๊ฐœ(P)๊ฐ€ ์ž…๋ ฅ๋  ๋•Œ, ๊ฐ€ํฌ๊ฐ€ ๋ฒ ๊ฐœ ์œ„์— ์žˆ์œผ๋ฉด 1, ์•„๋‹ˆ๋ฉด 0์„ ์ถœ๋ ฅ

๋ฒ ๊ฐœ ์œ„์— ์žˆ๋Š” ์กฐ๊ฑด์ด G๊ฐ€ P์— ๊ฑธ์ณ ์žˆ์–ด๋„ ๋ฒ ๊ฐœ ์œ„์— ์žˆ๋‹ค๊ณ  ์นœ๋‹ค.

 

1์ด ์ถœ๋ ฅ๋˜๋ ค๋ฉด P์˜ ์˜์—ญ ์•ˆ์— G๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

๊ทธ๊ฒƒ์„ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” P์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๋ฉด ์‰ฝ๊ฒŒ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

P์˜ ๊ฐœ์ˆ˜๊ฐ€ P ์˜์—ญ์˜ ๋„“์ด๋ณด๋‹ค ์ž‘์œผ๋ฉด G๊ฐ€ P ์˜์—ญ ์•ˆ์— ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค.

์ž…๋ ฅ ์˜ˆ์ œ 1์„ ์˜ˆ๋กœ ๋“ค์–ด ๋ณด์ž.

11 11
5 4 6 10
....GGGG...
....GGGG...
....GGGG...
.PPPGGGGPPP
.PPPGGGGPPP
.PPPPPPPPPP
.PPPPPPPPPP
.PPPPPPPPPP
.PPPPPPPPPP
...........
...........

๋ฒ ๊ฐœ์˜ ์˜์—ญ์€ ์ด 60๊ฐœ(6 * 10)์ด๋‹ค. ์—ฌ๊ธฐ์„œ P์˜ ๊ฐœ์ˆ˜๋Š” 60 - 8 = 52๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€ํฌ(G)๊ฐ€ P ์˜์—ญ ์•ˆ์— ์žˆ์–ด์„œ P์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ค„์–ด๋“  ๊ฒƒ์ด๋‹ค.

P์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ค„์–ด๋“œ๋ ค๋ฉด ๋ฌด์กฐ๊ฑด G๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ์ฝ”๋“œ

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val (roomH, roomW) = readLine().split(" ").map { it.toInt() }
    val (dogH, dogW, pillowH, pillowW) = readLine().split(" ").map { it.toInt() }
    var pTotalCnt = 0

    for (i in 0 until roomH) {
        val roomInput = readLine()
        pTotalCnt += roomInput.filter { it == 'P' }.length
    }

    if (pTotalCnt < pillowW * pillowH) println(1)
    else println(0)
}

 

๋ฌธ์ œ ํ’€์ด

๋ฒ ๊ฐœ์˜ ๋„“์ด๋ฅผ pillowH, pillowW์˜ ๊ณฑ์œผ๋กœ ๊ตฌํ•˜๊ณ , P์˜ ๊ฐœ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์ค€๋‹ค.

P์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋ฒ ๊ฐœ์˜ ๋„“์ด๋ณด๋‹ค ์ž‘์œผ๋ฉด ๊ฐ€ํฌ๊ฐ€ ๋ฒ ๊ฐœ ์œ„์— ์žˆ๋‹ค๋Š” ๋œป์ด๋‹ค.

 

 

๋ฌธ์ œ ํ•ด๊ฒฐ ๊ณผ์ •

P์˜ ๊ฐœ์ˆ˜๋งŒ ์„ธ๋„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๊นจ๋‹ซ์ง€ ๋ชปํ•ด์„œ ๊ฐ€ํฌ๊ฐ€ ๋ฒ ๊ฐœ์— ์žˆ์„ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ์กฐ๊ฑด์œผ๋กœ ์žก์•„์„œ ํŒ๋ณ„ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ์—ˆ๋‹ค.

 

์•„๋ž˜์— ์ฝ”๋“œ๋ฅผ ๋‚จ๊ฒจ ๋†“๊ฒ ๋‹ค.

์ด ์ฝ”๋“œ๋„ ์ •๋‹ต ์ฝ”๋“œ์ด๋‹ค.

๋”๋ณด๊ธฐ
import java.io.BufferedReader
import java.io.InputStreamReader

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {
    val (roomH, roomW) = readLine().split(" ").map { it.toInt() }
    val (dogH, dogW, pillowH, pillowW) = readLine().split(" ").map { it.toInt() }
    var answer = 0
    var gCnt = 0
    var pTotalCnt = 0

    for (i in 0 until roomH) {
        val roomInput = readLine()
        gCnt += roomInput.filter { it == 'G' }.length
        pTotalCnt += roomInput.filter { it == 'P' }.length
        
        if (roomInput.contains('G') && roomInput.contains('P')) {
            val pCnt = roomInput.filter { it == 'P' }.length
            if (pCnt != pillowW) answer = 1
        }
    }
    
    if (pTotalCnt < pillowW * pillowH && gCnt == dogW * dogH) answer = 1

    println(answer)
}

P์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•๋งŒ์œผ๋กœ๋„ ํŽธํ•˜๊ฒŒ ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

728x90