๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 19941 - ํ–„๋ฒ„๊ฑฐ ๋ถ„๋ฐฐ

by HandHand 2021. 3. 8.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 19941๋ฒˆ

ํ’€์ด ๊ณผ์ •

ํ–„๋ฒ„๊ฑฐ์™€ ์‚ฌ๋žŒ์˜ ์œ„์น˜๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๊ฐ€์žฅ ๋งŽ์€ ํ–„๋ฒ„๊ฑฐ๋ฅผ ๋จน๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ๋Š” ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ผ์ • ๋ฒ”์œ„๋‚ด์˜ ํ–„๋ฒ„๊ฑฐ๋ฅผ ๋จน์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์™ผ์ชฝ๋ถ€ํ„ฐ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•˜๋‚˜์”ฉ ๋จน์„ ์ˆ˜ ์žˆ๋Š” ํ–„๋ฒ„๊ฑฐ๋ฅผ ์ฐพ์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys

N, K = list(map(int, sys.stdin.readline().split()))
table = sys.stdin.readline().strip()


def solution():
    eat = [0] * N
    answer = 0

    for i in range(N):
        if table[i] == 'P':
            for offset in range(-K, K + 1):
                ni = i + offset
                if 0 <= ni < N and table[ni] == 'H' and not eat[ni]:
                    eat[ni] = 1
                    answer += 1
                    break

    return answer


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 6186 - Best Grass  (0) 2021.03.08
BOJ 8972 - ๋ฏธ์นœ ์•„๋‘์ด๋…ธ  (0) 2021.03.08
BOJ 16469 - ์†Œ๋…„ ์ ํ”„  (0) 2021.03.05
BOJ 13565 - ์นจํˆฌ  (0) 2021.03.05
BOJ 1105 - ํŒ”  (0) 2021.03.03

๐Ÿ’ฌ ๋Œ“๊ธ€