2022年4月27日 星期三

數字龍捲風

 d1 = '''

3 4 2 1 4
4 2 3 8 9
2 1 9 5 6
4 2 3 7 8
1 2 6 4 3
'''
d = [[j for j in i.split()] for i in d1.strip().split('\n')]

def go():
    global turn,ctr,r,c
    turn = turn % 4
    if turn ==0:
        c-=1
    elif turn==1:
        r-=1
    elif turn==2:
        c+=1
    elif turn ==3:
        r+=1  
    print(d[r][c],end='')
    ctr += 1
    if ctr>=n**2: exit()
turn = 0
n = 5

r,c =n//2,n//2
ctr = 0
print(d[r][c],end='')
ctr += 1
if n==1:exit()

for i in range(1,6):
    for j in range(i):
        go()
    turn +=1
    for j in range(i):
        go()
    turn +=1

沒有留言:

張貼留言