2020/05/11

[zerojudge] f012: 奶油機器人

# 蘭頓螞蟻  Langton's ant python asnewchien@gmail.com

from sys import *
dot = {(0,0): 0} # 某點的顏色  0:white, 1:black
x, y = 0, 0      # position
direction = 0    # 方向  0:up, 1:right, 2:down, 3:left
r = [(0,0)]      # 某一步的座標

rule = ({(0,0):(1,0,1,1), (0,1):(0,-1,2,1),(0,2):(-1,0,3,1),(0,3):(0,1,0,1),
         (1,0):(-1,0,3,0),(1,1):(0,1,0,0), (1,2):(1,0,1,0), (1,3):(0,-1,2,0)})
# key of rule   : (color, direction)
# value of rule : (deltaX, deltaY, newDirection, newColor)

for i in range(1, 10104):
    op = (x, y)       # 原來的點座標
    oc = dot[op]      # 原來的點顏色
    dx, dy, direction, nc = rule[(oc,direction)]
    dot[(x,y)] = nc   # 給原來的點一個新顏色
    x += dx
    y += dy
    if(not dot.__contains__((x,y))): dot[(x,y)] = 0
    # 下一個點如果沒有拜訪過,設定為白色
    r.append((x,y))

'''
我們現在要以 r[10000] 為基準點, 循環節為 104
我先做了一個測試,
發現 r[10000]=(-16, 10), r[10104]=(-18, 8), r[10208]=(-20, 6)
每個循環的起點對應上一個起點的  deltaX = -2, deltaY = -2
以下我要對 r[10000] ~ r[10103] 建立對 r[10000] 的相對位置
'''
# md = [(0, 0), (-1, 0), (-1, -1), (-2, -1), (-2, -2), (-3, -2), (-3, -1), (-2, -1), (-2, -2), (-1, -2), (-1, -3), (0, -3), (0, -2), (-1, -2), (-1, -3), (-2, -3), (-2, -2), (-1, -2), (-1, -3), (0, -3), (0, -4), (-1, -4), (-1, -5), (-2, -5), (-2, -4), (-1, -4), (-1, -5), (0, -5), (0, -4), (-1, -4), (-1, -5), (-2, -5), (-2, -6), (-3, -6), (-3, -5), (-2, -5), (-2, -6), (-1, -6), (-1, -7), (-2, -7), (-2, -6), (-1, -6), (-1, -5), (-2, -5), (-2, -6), (-1, -6), (-1, -7), (0, -7), (0, -8), (-1, -8), (-1, -7), (0, -7), (0, -6), (1, -6), (1, -5), (0, -5), (0, -4), (1, -4), (1, -3), (0, -3), (0, -4), (1, -4), (1, -5), (0, -5), (0, -6), (1, -6), (1, -7), (0, -7), (0, -6), (1, -6), (1, -5), (0, -5), (0, -4), (1, -4), (1, -3), (2, -3), (2, -2), (1, -2), (1, -1), (0, -1), (0, -2), (-1, -2), (-1, -3), (-2, -3), (-2, -4), (-1, -4), (-1, -5), (-2, -5), (-2, -4), (-1, -4), (-1, -3), (-2, -3), (-2, -2), (-3, -2), (-3, -3), (-4, -3), (-4, -2), (-3, -2), (-3, -3), (-2, -3), (-2, -2), (-1, -2), (-1, -3), (-2, -3), (-2, -2)]

md = []
baseX, baseY = r[10000]
for i in range(104):
    x, y = r[10000+i]
    md.append((x-baseX, y-baseY))

for s in stdin:
    n = int(s)
    try: 
        # n <= 10000
        print('({},{})'.format(*r[n]))
    except:
        # n > 10000
        v, u = divmod(n-10000, 104)
        tx, ty = baseX+(-2)*v, baseY+(-2)*v
        tx += md[u][0]
        ty += md[u][1]
        print('({},{})'.format(tx,ty))

2020/05/02

[zerojudge] e998: S形矩陣

from sys import *

A = list(map(str, range(1, 10001)))
# 一次建好型態為 string 的表
opt = '' # 最後一次印
for s in stdin:
    n = int(s)
    for i in range(n):
        if(i%2==0): opt += ' '.join(A[i*n:i*n+n]) + ' \n'
        else: opt += ' '.join(A[i*n:i*n+n][::-1]) + ' \n'
        # i 為奇數時取出值,先反轉再 join
        # i 為偶數時,直接 join
    opt += '\n'
stdout.write(opt)