ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

21610๋ฒˆ: ๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด์™€ ๋น„๋ฐ”๋ผ๊ธฐ

๋งˆ๋ฒ•์‚ฌ ์ƒ์–ด๋Š” ํŒŒ์ด์–ด๋ณผ, ํ† ๋„ค์ด๋„, ํŒŒ์ด์–ด์Šคํ†ฐ, ๋ฌผ๋ณต์‚ฌ๋ฒ„๊ทธ ๋งˆ๋ฒ•์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋Š˜ ์ƒˆ๋กœ ๋ฐฐ์šด ๋งˆ๋ฒ•์€ ๋น„๋ฐ”๋ผ๊ธฐ์ด๋‹ค. ๋น„๋ฐ”๋ผ๊ธฐ๋ฅผ ์‹œ์ „ํ•˜๋ฉด ํ•˜๋Š˜์— ๋น„๊ตฌ๋ฆ„์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. ์˜ค๋Š˜์€ ๋น„๋ฐ”๋ผ๊ธฐ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 8 ๋ฐฉํ–ฅ ํƒ์ƒ‰๊ณผ, ๋Œ€๊ฐ์„  4๋ฐฉํ–ฅ ํƒ์ƒ‰์„ ์‚ฌ์šฉํ•˜์—ฌ์„œ ๋ฌธ์ œ์˜ ์š”๊ตฌ์‚ฌํ•ญ์— ๋”ฐ๋ผ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์ •๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

  1. ๋ชจ๋“  ๊ตฌ๋ฆ„์ด d ๋ฐฉํ–ฅ์œผ๋กœ s์นธ ๋งŒํผ ์ด๋™ํ•œ๋‹ค.
    • ๊ฒฉ์ž๋Š” 1๋ฒˆ๊ณผ N๋ฒˆ ์—ด, 1ํ–‰๊ณผ N๋ฒˆ ์—ด์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค.
    • ์ด๋Ÿฐ ์กฐ๊ฑด์ด ๋‚˜์˜ค๋ฉด ๋ชจ๋“ˆ๋Ÿฌ(%) ์—ฐ์‚ฐ์„ ํ™œ์šฉํ•˜๋ฉด ๋œ๋‹ค.
  2. ๊ฐ ๊ตฌ๋ฆ„์—์„œ ๋น„๊ฐ€ ๋‚ด๋ ค ๊ตฌ๋ฆ„์ด ์žˆ๋Š” ์นธ์˜ ๋ฐ”๊ตฌ๋‹ˆ์— ์ €์žฅ๋œ ๋ฌผ์˜ ์–‘์ด 1 ์ฆ๊ฐ€ํ•œ๋‹ค.
  3. ๋น„๋ฅผ ๋‚ด๋ฆฐ ๊ตฌ๋ฆ„์€ ๋ชจ๋‘ ์‚ฌ๋ผ์ง„๋‹ค.
  4. 2์—์„œ ๋ฌผ์ด ์ฆ๊ฐ€ํ•œ ์นธ(๊ตฌ๋ฆ„์ด ์žˆ์—ˆ ๋˜ ์นธ)์— ๋ฌผ๋ณต์‚ฌ๋ฒ„๊ทธ ๋งˆ๋ฒ•์„ ์‹œ์ „ํ•œ๋‹ค. ๋ฌผ๋ณต์‚ฌ ๋ฒ„๊ทธ ๋งˆ๋ฒ•์€ ๋Œ€๊ฐ์„  4๋ฐฉํ–ฅ์œผ๋กœ ๊ฑฐ๋ฆฌ๊ฐ€ 1์ธ ์นธ์— ๋ฌผ์ด ์žˆ๋Š” ๋ฐ”๊ตฌ๋‹ˆ์˜ ์ˆ˜๋งŒํผ ๋ฌผ์˜ ์–‘์ด ์ฆ๊ฐ€ํ•œ๋‹ค.
    • ์—ฐ๊ฒฐ๋œ ๊ฒฉ์ž์™€ ๋‹ฌ๋ฆฌ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๋Š” ๊ฒฝ์šฐ๋Š” ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค.
  5. ๋ฐ”๊ตฌ๋‹ˆ์— ์ €์žฅ๋œ ๋ฌผ์˜ ์–‘์ด 2 ์ด์ƒ์ธ ๋ชจ๋“  ์นธ์— ๊ตฌ๋ฆ„์ด ์ƒ๊ธฐ๊ณ , ๋ฌผ์˜ ์–‘์ด 2 ์ค„์–ด๋“ ๋‹ค. ์ด๋•Œ ๊ตฌ๋ฆ„์ด ์ƒ๊ธฐ๋Š” ์นธ์€ 3์—์„œ ๊ตฌ๋ฆ„์ด ์‚ฌ๋ผ์ง„ ์นธ์ด ์•„๋‹ˆ์–ด์•ผ ํ•œ๋‹ค.

 

์ฝ”๋“œ

sys import stdin
from copy import deepcopy


def visitable(r, c):
    return 0 <= r < N and 0 <= c < N


def simulation(cur_d, cur_s):
    global cloud_loc
    new_cloud_loc = []

    dr, dc = [a * b for a, b in zip(move_dirs[cur_d], [cur_s, cur_s])]

    # case 1
    for r, c in cloud_loc:
        next_r, next_c = (r + dr) % N, (c + dc) % N
        # case 2
        board[next_r][next_c] += 1
        new_cloud_loc.append((next_r, next_c))

    # case 4
    for r, c in new_cloud_loc:
        for dr, dc in search_dirs:
            next_r, next_c = r + dr, c + dc

            if visitable(next_r, next_c) and board[next_r][next_c] > 0:
                board[r][c] += 1

    next_cloud_loc = []
    # case 5
    for r in range(N):
        for c in range(N):
            if board[r][c] >= 2 and (r, c) not in new_cloud_loc:
                board[r][c] -= 2
                next_cloud_loc.append((r, c))

    cloud_loc = deepcopy(next_cloud_loc)


if __name__ == '__main__':
    # ←, โ†–, ↑, โ†—, →, โ†˜, ↓, โ†™
    move_dirs = ((0, -1), (-1, -1), (-1, 0), (-1, 1),  (0, 1), (1, 1), (1, 0), (1, -1))
    # โ†–, โ†—, โ†˜, โ†™
    search_dirs = [(-1, -1), (-1, 1), (1, 1), (1, -1)]

    N, M = map(int, stdin.readline().split())
    board = [list(map(int, stdin.readline().split())) for _ in range(N)]
    cloud_loc = ((N - 1, 0), (N - 1, 1), (N - 2, 0), (N - 2, 1))

    for _ in range(M):
        d, s = map(int, stdin.readline().split())
        simulation(d - 1, s)

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€