[コードツリー]施工旋風(python)


🔗 質問リンク


https://www.codetree.ai/frequent-problems/heros-of-storm/description
https://www.acmicpc.net/problem/17144(李準-スモッグこんにちは!)

👩🏻‍💻 コード#コード#

import sys

n, m, t = map(int, sys.stdin.readline().rstrip().split())
maps = [list(map(int, sys.stdin.readline().rstrip().split())) for _ in range(n)]
answer = 0
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]


def spread():
    next_maps = [[0] * m for _ in range(n)]

    for i in range(n):
        for j in range(m):
            if maps[i][j] == -1:
                continue

            mod = maps[i][j] // 5
            cnt = 0

            for k in range(4):
                nx = i + dx[k]
                ny = j + dy[k]
                if 0 <= nx < n and 0 <= ny < m and maps[nx][ny] != -1:
                    next_maps[nx][ny] += mod
                    cnt += 1

            next_maps[i][j] += maps[i][j] - mod * cnt

    for i in range(n):
        for j in range(m):
            if maps[i][j] != -1:
                maps[i][j] = next_maps[i][j]


def rotate_top(start):
    for i in range(start - 1, 0, -1):
        maps[i][0] = maps[i - 1][0]
    for j in range(m - 1):
        maps[0][j] = maps[0][j + 1]
    for i in range(0, start):
        maps[i][m - 1] = maps[i + 1][m - 1]
    for j in range(m - 1, 0, -1):
        if j == 1:
            maps[start][j] = 0
        else:
            maps[start][j] = maps[start][j - 1]


def rotate_bottom(start):
    for i in range(start + 1, n - 1):
        maps[i][0] = maps[i + 1][0]
    for j in range(m - 1):
        maps[n - 1][j] = maps[n - 1][j + 1]
    for i in range(n - 1, start, -1):
        maps[i][m - 1] = maps[i - 1][m - 1]
    for j in range(m - 1, 0, -1):
        if j == 1:
            maps[start][j] = 0
        else:
            maps[start][j] = maps[start][j - 1]


tornado = 0
for row in range(n):
    if maps[row][0] == -1:
        tornado = row
        break

for i in range(t):
    spread()
    rotate_top(tornado)
    rotate_bottom(tornado + 1)

for i in range(n):
    for j in range(m):
        if maps[i][j] != -1:
            answer += maps[i][j]
print(answer)

📝 整理する

  • 施工旋風の位置を探す
  • 計算
  • 拡散{{けいさん:
  • かくさん}}
    2-1. (0,0)~(n−1,m−1)への拡散結果をnext mapsに保存する
    2-2. next mapsをmaps
  • に保存
    上下回転
  • 施工旋風の位置を除いた残りの答えに
  • を加える

    💡覚えておきたい


    rotate top,rotate bottomではエラーの範囲が設定されており,エラーの部分を見つけるのに長い時間がかかった.
    範囲を設定するときはよく確認しましょう.