• 注册
    • 今日签到
    • 连续签到

    暂没有数据

    暂没有数据

    • 查看作者
    • Maze III

      BFS:

      def haspath(maze):
          queue=[(0,0)] #start point
          distance = {}
          distance[queue[0]] = 0
          direction =[(1,0),(0,1),(-1,0),(0,-1)]
          while queue:
              #print(queue)
              curr_position = queue.pop(0)
              for x,y in direction:
                  i = curr_position[0] + x
                  j = curr_position[1] + y
                  if i >= 0 and i<len(maze) and j>=0 and j<len(maze[0]):
                      d=distance[curr_position]+1
                      if (i,j) not in distance.keys():
                          distance[(i,j)] = d
                      else:
                          distance[(i,j)]=min(distance[(i,j)],d)
                          #print(distance[(i,j)])
                      if maze[i][j]==9:
                          return distance[(i,j)]
                          #return True
                      if maze[i][j]==1:
                          queue.append((i,j))
                          maze[curr_position[0]][curr_position[1]]=3
          return False
      maze=[[1,0,1]
           ,[1,0,1]
           ,[1,1,1]
           ,[1,1,1]
           ,[1,1,9]]
      print(haspath(maze))

      DFS 



      (halfway)def
              start = [,]
              current = start
              visit = {}
              direction =[(,),(,),(-,),(,-)]
              result = prev = dfs(maze, start, current,visit,prev ):
      
      
      
                  maze[current[]][current[]] == (current[],current[]) visit:
                      prev == :
                          visit[(current[],current[])] = :
                          (visit)
                          visit[(current[],current[])] = visit[prev[],prev[]]+old_current = current
                      x, y direction:
      
      
                          i = old_current[] + x
                          j = old_current[] + y
      
      
                          i >=j>=i < (maze) j <(maze[]) :
                              prev = current
                              current = [i,j]
      
                              (current[],current[]) visit:
                                  dfs(maze, start,current,visit,prev)
                              :
                      maze[current[]][current[]] == :
      
                      visit[(prev[],prev[])]
      
      
              dfs(maze, start, current,visit,prev )
      maze=[[,,]
           ,[,,]
           ,[,,]
           ,[,,]
           ,[,,]]
      (haspath(maze))

      纽约州·法拉盛
    • 0
    • 0
    • 0
    • 206