結果

問題 No.401 数字の渦巻き
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-24 13:20:22
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 1,092 bytes
コンパイル時間 914 ms
コンパイル使用メモリ 10,948 KB
実行使用メモリ 8,788 KB
最終ジャッジ日時 2023-08-06 12:42:33
合計ジャッジ時間 2,351 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,736 KB
testcase_01 AC 19 ms
8,628 KB
testcase_02 AC 19 ms
8,684 KB
testcase_03 AC 19 ms
8,632 KB
testcase_04 AC 20 ms
8,736 KB
testcase_05 AC 19 ms
8,608 KB
testcase_06 AC 19 ms
8,764 KB
testcase_07 AC 19 ms
8,684 KB
testcase_08 AC 19 ms
8,736 KB
testcase_09 AC 19 ms
8,652 KB
testcase_10 AC 19 ms
8,692 KB
testcase_11 AC 19 ms
8,624 KB
testcase_12 AC 19 ms
8,740 KB
testcase_13 AC 19 ms
8,680 KB
testcase_14 AC 19 ms
8,624 KB
testcase_15 AC 19 ms
8,632 KB
testcase_16 AC 19 ms
8,616 KB
testcase_17 AC 20 ms
8,780 KB
testcase_18 AC 21 ms
8,684 KB
testcase_19 AC 20 ms
8,772 KB
testcase_20 AC 20 ms
8,636 KB
testcase_21 AC 19 ms
8,788 KB
testcase_22 AC 20 ms
8,728 KB
testcase_23 AC 20 ms
8,624 KB
testcase_24 AC 20 ms
8,692 KB
testcase_25 AC 20 ms
8,780 KB
testcase_26 AC 20 ms
8,684 KB
testcase_27 AC 21 ms
8,692 KB
testcase_28 AC 20 ms
8,696 KB
testcase_29 AC 20 ms
8,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import array


UNDEF = 0


def generate_vortex(size):
    def out_of_stage(r, c):
        return r < 0 or c < 0 or r >= size or c >= size or stage[r][c] != UNDEF

    def advance(r0, c0, d):
        m = d % 4
        if m == 0:
            return (r0, c0 + 1)
        elif m == 1:
            return (r0 + 1, c0)
        elif m == 2:
            return (r0, c0 - 1)
        elif m == 3:
            return (r0 - 1, c0)
        else:
            assert False
    stage = [array.array("I", (UNDEF for _ in range(size)))
             for _ in range(size)]
    dire = 0
    stage[0][0] = 1
    row0, col0 = 0, 0
    for i in range(2, size * size + 1):
        row, col = advance(row0, col0, dire)
        while out_of_stage(row, col):
            dire += 1
            row, col = advance(row0, col0, dire)
        stage[row][col] = i
        row0, col0 = row, col
    return stage


def main():
    size = int(input())
    stage = generate_vortex(size)
    print(*(" ".join("{:03d}".format(x) for x in r) for r in stage), sep="\n")


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