1. 數字龍捲風
    1. 題目
    2. 解題

數字龍捲風

數字龍捲風

APCS歷屆

題目

又來寫APCS歷屆了,看一下題目吧?
pic
簡單來說就是他給你一張地圖,然後會順時針跑,他會告訴你第一步怎麼走,然後你要將走過的都印出來。

解題

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 = "")

我的解法就是先看一下下一步要怎麼走,就開始移動,改座標,並將數字存進ans,但要注意就是它有可能開始遶第二圈的時候會走兩次一樣的下一步,所以我們就用before來記錄有沒有走過以及另外做判斷如果走過就把他的下一步改去上一步。