1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| # 規律0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2 ,3 def move(p):#移動 global nexts ans = [] ans.append(list1[p[0]][p[1]]) before = [[p[0], p[1]]] #before.append(p) while p != endp: #print(p, before) if nexts == 1 and [p[0]-1, p[1]] not in before: p[0] -= 1 nexts = 2 ans.append(list1[p[0]][p[1]]) before.append([p[0], p[1]]) elif nexts == 0 and [p[0], p[1]-1] not in before: p[1] -= 1 nexts = 1 ans.append(list1[p[0]][p[1]]) before.append([p[0], p[1]]) elif nexts == 3 and [p[0]+1, p[1]] not in before: p[0] += 1 nexts = 0 ans.append(list1[p[0]][p[1]]) before.append([p[0], p[1]]) elif nexts == 2 and [p[0], p[1]+1] not in before: p[1] += 1 nexts = 3 ans.append(list1[p[0]][p[1]]) before.append([p[0], p[1]]) else: if nexts == 0: nexts = 3 elif nexts == 1: nexts = 0 elif nexts == 3: nexts = 2 else: nexts = 1 return ans
n = int(input()) #n = 5 nexts = int(input()) #紀錄下一 #list1 = [[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]] list1 = [] for i in range(n): list1.append(list(map(int, input().split()))) p = [n // 2, n // 2] #print([p[0], p[1]]) endp = [0, 0]# 紀錄最後的地方 if nexts == 0: endp = [n-1, 0] elif nexts == 1: endp = [0, 0] elif nexts == 2: endp = [0, n-1] else: endp = [n-1, n-1] ans = move(p) print(*ans, sep = "")
|