list1 = [1, 1, 1, 2, 1, 1, 2, 4, 4, 3,
2, 1, 1, 1, 1, 1, 1, 1, 4, 2,
1, 2, 3, 1, 4, 2, 1, 1, 2, 4,
2, 1, 1, 1, 1, 1, 2, 1, 4, 1,
1, 1, 1, 3, 2, 2, 3, 1, 2, 3,
3, 2, 4, 1, 1, 1, 1, 1, 1, 4,
2, 1, 1, 1, 1, 2, 2, 1, 4, 1,
1, 2, 1, 3, 2, 1, 2, 4, 1, 4,
1, 2, 1, 1, 1, 1, 4, 2, 2, 2,
2, 1, 2, 1, 2, 2, 3, 4, 1, 1]
flag = -1 #为了记录第0个格子设置为-1
direction = 1 #默认向右走,+1
dex = [] #记录走过的格子
n = 0 #步数
#从哪边来的专向后该去那边
s2 = {1:10, 10:-1, -1:-10, -10:1}
s4 = {-10:-1, -1:10, 10:1, 1:-10}
while flag != 99:
n += 1
#右转
if list1[flag] == 2:
direction = s2.get(direction)
#掉头
elif list1[flag] == 3:
direction = 0-direction
#左转
elif list1[flag] == 4:
direction = s4.get(direction)
#直走
flag += direction
print('现在位置是:',flag)
dex.append(flag)
#去重
dex = list(set(dex))
print('一共走了%d步'%n)
#求和
print('走过的格子的数字相加:',sum([list1[i] for i in dex]))