【APCS】m932. 2. 蜜蜂觀察 - Python 解

詳細資訊:https://zerojudge.tw/ShowProblem?problemid=m932

身為一個高中程式小白,上次在APCS考場中遇到這題,我瞬間懵了一下,雖說最後有解出來,但似乎我的答案並不是 AC。

註:這篇文章其實很早就寫完了,只不過現在才發XD。這篇文只是純粹因為解出我不會的題目,感到欣喜若狂,所以藉此分享。

以下是程式碼:

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
import sys

m, n, k = map(int, sys.stdin.readline().split())

mm, nn = m-1, n-1

mat = [list(input()) for _ in range(m)]

step = [int(i) for i in input().split()]

result = []

x, y = mm, 0

for i in range(k):
if step[i] == 0:
if x - 1 >= 0:
x -= 1
result += mat[x][y]
else:
result += mat[x][y]
elif step[i] == 1:
if y + 1 <= nn:
y += 1
result += mat[x][y]
else:
result += mat[x][y]
elif step[i] == 2:
if x + 1 <= mm and y + 1 <= nn:
x += 1
y += 1
result += mat[x][y]
else:
result += mat[x][y]
elif step[i] == 3:
if x + 1 <= mm:
x += 1
result += mat[x][y]
else:
result += mat[x][y]
elif step[i] == 4:
if y - 1 >= 0:
y -= 1
result += mat[x][y]
else:
result += mat[x][y]
elif step[i] == 5:
if x - 1 >= 0 and y - 1 >= 0:
x -= 1
y -= 1
result += mat[x][y]
else:
result += mat[x][y]

print(''.join(result))
print(len(set(result)))

雖然說並不是最簡潔的程式碼,也不是效率最高的,但能真正解出來真的讓我非常開心XD。

這題主要是在考「字元處理」、「邊界檢查」、「二維陣列」,算是每年 APCS 第二題的典例。