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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

16235๋ฒˆ: ๋‚˜๋ฌด ์žฌํ…Œํฌ

๋ถ€๋™์‚ฐ ํˆฌ์ž๋กœ ์–ต๋Œ€์˜ ๋ˆ์„ ๋ฒˆ ์ƒ๋„๋Š” ์ตœ๊ทผ N×N ํฌ๊ธฐ์˜ ๋•…์„ ๊ตฌ๋งคํ–ˆ๋‹ค. ์ƒ๋„๋Š” ์†์‰ฌ์šด ๋•… ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•ด ๋•…์„ 1×1 ํฌ๊ธฐ์˜ ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด ๋†“์•˜๋‹ค. ๊ฐ๊ฐ์˜ ์นธ์€ (r, c)๋กœ ๋‚˜ํƒ€๋‚ด๋ฉฐ, r์€ ๊ฐ€์žฅ ์œ„์—์„œ๋ถ€ํ„ฐ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ๋ฌธ์ œ๋กœ ๋ฌธ์ œ ์š”๊ตฌ์‚ฌํ•ญ์— ๋”ฐ๋ผ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋œ๋‹ค. ๋‚˜๋ฌด๋“ค์€ ๊ณ„์ ˆ์— ๋”ฐ๋ผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์ด๊ฐ€ ๋ณ€ํ™”ํ•˜๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐ ๊ณ„์ ˆ์— ๋”ฐ๋ฅธ ๋กœ์ง์„ ๊ตฌํ˜„ํ•˜๊ณ  `K` ๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ฉด ๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

  • ๋ด„
    • ๋‚˜๋ฌด ๋‚˜์ด ๋งŒํผ์˜ ์–‘๋ถ„์„ ๋จน๊ณ  ๋‚˜์ด๊ฐ€ 1 ์ฆ๊ฐ€ํ•œ๋‹ค.
    • ์ž์‹ ์ด ์œ„์น˜ํ•œ ๊ณณ์˜ ์–‘๋ถ„๋งŒ ๋จน์„ ์ˆ˜ ์žˆ๋‹ค.
    • ํ•˜๋‚˜์˜ ์นธ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋‚˜๋ฌด๊ฐ€ ์žˆ๋‹ค๋ฉด, ๋‚˜์ด๊ฐ€ ์–ด๋ฆฐ ๋‚˜๋ฌด๋ถ€ํ„ฐ ์–‘๋ถ„์„ ์„ญ์ทจํ•œ๋‹ค.
    • ๋‚˜๋ฌด์˜ ๋‚˜์ด๋งŒํผ ์–‘๋ถ„์„ ๋จน์ง€ ๋ชปํ•˜๋ฉด ์ฃฝ๊ฒŒ ๋œ๋‹ค.
  • ์—ฌ๋ฆ„
    • ๋ด„์— ์ฃฝ์€ ๋‚˜๋ฌด๊ฐ€ ์–‘๋ถ„์œผ๋กœ ๋ณ€ํ•˜๊ฒŒ ๋œ๋‹ค.
    • ์ฃฝ์€ ๋‚˜๋ฌด์˜ ๋‚˜์ด๋ฅผ 2๋กœ ๋‚˜๋ˆˆ ๊ฐ’์ด ๋‚˜๋ฌด๊ฐ€ ์žˆ๋˜ ์นธ์˜ ์–‘๋ถ„์œผ๋กœ ์ถ”๊ฐ€๋œ๋‹ค.
  • ๊ฐ€์„
    • ๋‚˜๋ฌด๊ฐ€ 8 ๋ฐฉํ–ฅ์œผ๋กœ ๋ฒˆ์‹์„ ํ•˜๋ฉฐ, ๋ฒˆ์‹ ๊ฐ€๋Šฅํ•œ ๋‚˜๋ฌด๋Š” ๋‚˜์ด๊ฐ€ 5์˜ ๋ฐฐ์ˆ˜์—ฌ์•ผ ํ•œ๋‹ค.
    • NxN ํฌ๊ธฐ๋ฅผ ๋ฒ—์–ด๋‚  ๊ฒฝ์šฐ ๋ฒˆ์‹์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
  • ๊ฒจ์šธ
    • ๋กœ๋ด‡์ด ๋Œ์•„๋‹ค๋‹ˆ๋ฉฐ, ์ดˆ๊ธฐ์— ์ฃผ์–ด์ง„ ๊ฐ’์— ๋”ฐ๋ผ ์–‘๋ถ„์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

 ์ฆ‰ ๋ด„์— ๋‚˜์ด๊ฐ€ ์–ด๋ฆฐ ๋‚˜๋ฌด ๋ถ€ํ„ฐ ์–‘๋ถ„์„ ์„ญ์ทจํ•˜๊ณ , ๋” ์ด์ƒ ์–‘๋ถ„์„ ์„ญ์ทจํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ๋‚˜๋จธ์ง€๋Š” ์ฃฝ์€ ๋‚˜๋ฌด๊ฐ€ ๋˜๋ฏ€๋กœ ์—ฌ๋ฆ„์˜ ๋กœ์ง์„ ์‹คํ–‰ํ•˜๋ฉด ๋œ๋‹ค. ์ดํ›„์— ๊ฐ€์„, ๊ฒจ์šธ์˜ ๋กœ์ง์€ ๊ฐ„๋‹จํ•˜๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


def spring_summer():
    for x in range(n):
        for y in range(n):
            if not tree[x][y]:
                continue

            # ์–ด๋ฆฐ ๋‚˜๋ฌด ๋ถ€ํ„ฐ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌ.
            tree[x][y].sort()

            for idx, cur_age in enumerate(tree[x][y]):
                # ๋ด„
                if cur_age <= graph[x][y]:
                    graph[x][y] -= cur_age
                    tree[x][y][idx] += 1
                # ์—ฌ๋ฆ„
                else:
                    # ์ฃฝ์€ ๋‚˜๋ฌด์˜ ์–‘๋ถ„์„ ์ €์žฅํ•จ.
                    graph[x][y] += (sum([(die // 2) for die in tree[x][y][idx:]]))
                    # ๋’ค์˜ ๋‚˜๋ฌด๋“ค์€ ์ฃฝ์€ ๋‚˜๋ฌด ์ด๋ฏ€๋กœ ๊ฐ’์„ ๋‚ ๋ฆผ.
                    tree[x][y] = tree[x][y][:idx]
                    break


def fall():
    for x in range(n):
        for y in range(n):
            cnt = 0
            if not tree[x][y]:
                continue

            for cur_age in tree[x][y]:
                # 5์˜ ๋ฐฐ์ˆ˜์ผ ๊ฒฝ์šฐ๋งŒ
                if cur_age % 5 == 0:
                    cnt += 1
            if cnt:
                for dx, dy in dirs:
                    next_x, next_y = x + dx, y + dy
                    if 0 <= next_x < n and 0 <= next_y < n:
                        for _ in range(cnt):
                            tree[next_x][next_y].append(1)


def winter():
    for x in range(n):
        for y in range(n):
            graph[x][y] += nutrient[x][y]


if __name__ == '__main__':
    dirs = ((-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1))
    n, m, k = map(int, stdin.readline().split())
    # ๊ฒจ์šธ์— ์ถ”๊ฐ€ํ•  ์–‘๋ถ„ ๊ฐ’.
    nutrient = [list(map(int, stdin.readline().split())) for _ in range(n)]
    # ๋ชจ๋“  ์นธ์— ์–‘๋ถ„์€ 5์”ฉ ๋“ค์–ด ์žˆ์Œ
    graph = [[5] * n for _ in range(n)]
    tree = [[[] for _ in range(n)] for _ in range(n)]

    for _ in range(m):
        i, j, age = map(int, stdin.readline().split())
        tree[i - 1][j - 1].append(age)

    # k๋งŒํผ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ฉฐ, ๋ด„, ์—ฌ๋ฆ„, ๊ฐ€์„, ๊ฒจ์šธ์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ ํ•˜๋ฉด ๋จ.
    for _ in range(k):
        spring_summer()
        fall()
        winter()

    answer = 0
    for i in range(n):
        for j in range(n):
            answer += len(tree[i][j])

    print(answer)

 ํŒŒ์ด์ฌ์œผ๋กœ๋Š” ํ•ด๋‹น ์ฝ”๋“œ๊ฐ€ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ PyPy3๋กœ ์ œ์ถœํ•˜์˜€๋‹ค. ์•„๋งˆ `sort`์™€ `indexing`ํ•˜๋Š” ์˜ค๋ฒ„ํ—ค๋“œ๋กœ ์ธํ•ด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

 

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