結果

問題 No.401 数字の渦巻き
ユーザー sue_charo
提出日時 2017-08-07 00:01:07
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 1,967 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-10-11 23:00:40
合計ジャッジ時間 2,282 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())
def ILI(): return list(map(int, input().split()))


def read():
    N = II()
    return (N,)


def solve(N):
    ans = [[None] * N for __ in range(N)] # [y][x]

    limit_right = N
    limit_left = -1
    limit_top = 0
    limit_bottom = N

    now_x = 0
    now_y = 0
    count = 1
    state = 0 # 0 : right, 1: down, 2: left, 3: up
    while count != (N ** 2 + 1):
        if state == 0:
            if now_x + 1 == limit_right:
                limit_right = now_x
                state = (state + 1) % 4
            else:
                ans[now_y][now_x] = count
                count += 1
                now_x += 1
        elif state == 1:
            if now_y + 1 == limit_bottom:
                limit_bottom = now_y
                state = (state + 1) % 4
            else:
                ans[now_y][now_x] = count
                count += 1
                now_y += 1
        elif state == 2:
            if now_x - 1 == limit_left:
                limit_left = now_x
                state = (state + 1) % 4
            else:
                ans[now_y][now_x] = count
                count += 1
                now_x -= 1
        elif state == 3:
            if now_y - 1 == limit_top:
                limit_top = now_y
                state = (state + 1) % 4
            else:
                ans[now_y][now_x] = count
                count += 1
                now_y -= 1

    row_ans = []
    for row in ans:
        for i in range(N):
            if len(str(row[i])) == 1:
                row[i] = "00{}".format(row[i])
            if len(str(row[i])) == 2:
                row[i] = "0{}".format(row[i])
            if len(str(row[i])) == 3:
                row[i] = "{}".format(row[i])
        row_ans.append(" ".join(row))

    ans = "\n".join(row_ans)

    return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()
0