dir0=[[0,1],[1,0],[0,-1],[-1,0]] step = 999999999 tempStep = 0 tempValue = 0 def grid_input(N): grid = [[]for i in range(N)] for i in range(N): line = input().split('') for j in range(len(line)): grid[i].append(int[line[j]]) return grid def M_find_C(grid,row,col,e_row,e_col): global step global candy global tempValue global tempStep global dir0 tempStepValue = 0 if row<0 or col<0 or row>len(grid) or col>len(grid) or grid[row][col]==-1: return None if row == e_row and col == e_col: if tempStep+1 < step: step = tempStep+1 candy = tempValue elif tempStep+1 == step and tempValue > candy: candy = tempValue return None if grid[row][col] >= 0: tempStepValue = grid[row][col] tempValue += tempStepValue tempStep+=1 grid[row][col] = -1 for i in range(0,4): M_find_C(grid,dir0[i][0]+row,dir0[i][1]+col,e_row,e_col) if grid[row][col]>=0: tempValue -=tempStepValue tempStep -=1 grid[row][col] = tempStepValue def M_C_games(N,grid): s_row = 0 s_col = 0 e_row = 0 e_col = 0 for i in range(0,N): for j in range(0,N): if grid [i][j] == -3: s_row = i s_col = j elif grid[i][j] == -2: e_row = i e_col = j M_find_C(grid,s_row,s_col,e_row,e_col) return candy if __name__ == "__main__": N = int(input()) M_C_games(N,grid_input(N)) print(candy)
2021-1-2,笔记