- def fun123(x):
- move = [0,1]
- postion,footprint = [0,0],[[0,0]]
- result = x[postion[0]][postion[1]]
- while not postion == [9,9]:
- if x[postion[0]][postion[1]] == 1:
- postion[0] += move[0]
- postion[1] += move[1]
- if postion not in footprint:
- result += x[postion[0]][postion[1]]
- footprint.append([postion[0],postion[1]])
- continue
- if x[postion[0]][postion[1]] == 3:
- move[0],move[1] = move[0]*-1,move[1]*-1
- postion[0] += move[0]
- postion[1] += move[1]
- if postion not in footprint:
- result += x[postion[0]][postion[1]]
- footprint.append([postion[0],postion[1]])
- continue
- if x[postion[0]][postion[1]] == 2:
- rotating = [[0,1],[1,0],[0,-1],[-1,0],[0,1]]
- move = rotating[rotating.index(move)+1]
- postion[0] += move[0]
- postion[1] += move[1]
- if postion not in footprint:
- result += x[postion[0]][postion[1]]
- footprint.append([postion[0],postion[1]])
- continue
- if x[postion[0]][postion[1]] == 4:
- rotating = [[0,1],[-1,0],[0,-1],[1,0],[0,1]]
- move = rotating[rotating.index(move)+1]
- postion[0] += move[0]
- postion[1] += move[1]
- if postion not in footprint:
- result += x[postion[0]][postion[1]]
- footprint.append([postion[0],postion[1]])
- continue
- return result
- if __name__ == '__main__':
- print(fun123([[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]]))
复制代码 |