結果

問題 No.401 数字の渦巻き
ユーザー kichirb3kichirb3
提出日時 2018-03-08 23:21:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 1,842 bytes
コンパイル時間 107 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-15 12:22:15
合計ジャッジ時間 2,425 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,752 KB
testcase_01 AC 36 ms
10,752 KB
testcase_02 AC 36 ms
10,752 KB
testcase_03 AC 35 ms
10,752 KB
testcase_04 AC 35 ms
10,880 KB
testcase_05 AC 37 ms
10,880 KB
testcase_06 AC 36 ms
10,752 KB
testcase_07 AC 37 ms
10,752 KB
testcase_08 AC 36 ms
10,880 KB
testcase_09 AC 35 ms
10,752 KB
testcase_10 AC 35 ms
10,880 KB
testcase_11 AC 36 ms
10,752 KB
testcase_12 AC 37 ms
10,880 KB
testcase_13 AC 37 ms
10,880 KB
testcase_14 AC 35 ms
10,752 KB
testcase_15 AC 36 ms
10,880 KB
testcase_16 AC 38 ms
10,880 KB
testcase_17 AC 35 ms
10,880 KB
testcase_18 AC 36 ms
10,880 KB
testcase_19 AC 35 ms
10,880 KB
testcase_20 AC 38 ms
10,752 KB
testcase_21 AC 38 ms
10,752 KB
testcase_22 AC 35 ms
10,752 KB
testcase_23 AC 34 ms
10,880 KB
testcase_24 AC 34 ms
11,008 KB
testcase_25 AC 34 ms
11,008 KB
testcase_26 AC 36 ms
10,880 KB
testcase_27 AC 38 ms
11,008 KB
testcase_28 AC 36 ms
10,880 KB
testcase_29 AC 37 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
"""
No.401 数字の渦巻き
https://yukicoder.me/problems/no/401

"""
import sys
from sys import stdin
from itertools import cycle
input = stdin.readline


def next_repeat(n):
    """
    渦巻きを書く際のある方向への移動回数。
    最初は右方向にnマス連続で移動する。次は下方向にn-1マス移動する。その次は左方向にn-1マス移動する。
    これ以降は2回方向が変わるごとに移動量が-1されていく。
    """
    yield n
    while True:
        n -= 1
        if n < 0:
            raise ValueError
        yield n
        yield n


def next_pos(n):
    """
    次に数字を埋める渦巻きのx, y座標を返す    
    """
    x, y = -1, 0                #  次に数字を塗る座標。最初に移動した後に0, 0になるようにx=-1からスタート
    d = cycle([(1, 0), (0, 1), (-1, 0), (0, -1)]) #  渦巻きを書く際の方向、右・下・左・上の順で x, yの変化量
    c = next_repeat(n)          #  渦巻きの一辺の長さ

    while True:
        dx, dy = d.__next__()
        count = c.__next__()
        for _ in range(count):
            nx = x + dx
            ny = y + dy
            yield nx, ny
            x = nx
            y = ny


def guruguru(N):
    """
    数字の渦巻きをつくる    
    :param N: 一辺のサイズ
    :return: 完成した渦巻 
    """
    ans = [[''] * N for _ in range(N)]
    p = next_pos(N)
    for i in range(1, N*N+1):
        x, y = p.__next__()
        ans[y][x] = '{:03d}'.format(i) #  そのままプリントできるように0パディングした文字列で塗る
    return ans


def main(args):
    N = int(input())
    ans = guruguru(N)
    for row in ans:
        print(*row, sep=' ')


if __name__ == '__main__':
    main(sys.argv[1:])
0