Python 如何在给定固定移动次数的情况下检查指针是否可以遍历可能的路径

Python 如何在给定固定移动次数的情况下检查指针是否可以遍历可能的路径,python,algorithm,graph-theory,breadth-first-search,Python,Algorithm,Graph Theory,Breadth First Search,我正在尝试在Hackerrank上解决此问题: 我有以下意见: ........#....#..#..#....#...#..#.#.#.#.#.#..#..... ..#..#..#.#....#..#.....#......#..##...........##. .........#.###.##...#.....##......###............# ....##........#..#.#.#......#...#.##.......##..... .............

我正在尝试在Hackerrank上解决此问题:

我有以下意见:

........#....#..#..#....#...#..#.#.#.#.#.#..#.....
..#..#..#.#....#..#.....#......#..##...........##.
.........#.###.##...#.....##......###............#
....##........#..#.#.#......#...#.##.......##.....
.................###...#.#...#......#.#.#.#.#...#.
.........#.....#...........##....#.#.#.##.....##..
.....#.##............#....#......#..#..#...##.....
.#.......###....#.#..##.##.#...##...#........#...#
..#.##..##..........#..........##.....##..........
#.#..##......#.#.#..##.###...#.........###..#...#.
.#..#..............#...#........#..#...#....#..#..
##..#..#........#....#........#...#.#......#.....#
#.#.......#.#..#...###..#..#.##...#.##.......#...#
#.#...#...#.....#....#......##.#.#.........#....#.
.#..........#......##..#....#....#.#.#..#..###....
#.#............#.##..#.##.##......###......#..#..#
.#..#.##...#.#......................#........#....
.....#....#.#..........##.#.#................#....
##............#.#......####...#.........#..##..#..
....#..##..##...#.........##..##....#..#.##...#...
.#........#...#..#...........#.###.....##.....##..
.......#..#..##...#..###.....#..##.........#......
...#......#..#...........###...............#......
...##.###.#.#....#...#..#.#.#....#....#.##.#...#..
..........#.......#..#..#...###....##.....#..#....
.............##.##.#.......#.#....#.......#..#..#.
.......#........#.....#....##...#...#.#...#.#.##..
.....#..#..#........#..#.....#...#.##.#....#...#..
....................#.#...#....###...###...##...#.
##.#.....##.....#..#.#.#...........#.#.##...#..#.#
#...........#....#.##...#.#.....#...#.....#.#.....
..#..##...#........#.##..#.....##.......#...#.#.#.
......#....#...##...........#..#.......#.##.......
......#..#..#.###..........#...#...........#..#...
....#.#..#..#.#.#...#.......#...#.##......#.......
....#.......#..#........#...#.#...#......#.......#
.#....##...#.#..#....#.#.##........#..#.#.........
#....#.......#..##......##...............#..#.##..
...#..##.......#.....#....#...#.#......#..##..###.
.....#...#...#...#...#...#..##...#..#.............
....##......#...#..#...#...#.#....#.....#..#.##...
...##.......#..##.....#........#.#....#...#.......
..#...#....#...#.###......#................#......
...#...###...#..##...###.....................#....
.....#....#....#...#.#.#.##....##......#....##....
...#.###...##.........#..........#.##.#.....#.....
##..#...#.........#.......#......##...........####
...###.#..........#.....#####........#..#.#.#...#.
...#..#.....#..##.##.#.....##...#...#.#.....#...##
.##.......#.##....#............#..................
#.....#.........#.#.........#..###....##...##.....
#....#.....#...#.....#.##...##...####........#....
#...........#..#...#........#.##..##..#...#.#.....
..#.#................#......###..##.#.#...##...#..
.#.#....#..#............#....#......#............#
..#..#...#.#.#...#...........#.......##.#...#.#...
#..........#.....#.....#......#.......#.#...##....
.......#...........#...........#....#............#
...####.#.....#.##.....#.......##.#..#......#.....
.#..#.....#..#......#.............#.#.#..##...#...
..#.#.#.........#...#..#.......#................##
.#..##.#.#...#.............#..#..........#..#...#.
....#........#......#...###..#.#..................
#..#..#.....#.#.#...##....##........#........#....
.....#.#.......##.......#.....#........#..##..#...
#.#.##........#..##.#..#.#...#........#.#......#..
....#.#.#.......#.##.##...##...#..#.###...#.#.#...
.....##.#....#........#....#.#........#.#.#.....#.
.....#..##..#.#....#.......#...#.#.###.........#.#
#.....#.##..#.......###.........#..##..#......##..
70行,最大时间为2244

这是我的策略,但它适用于某些测试用例:

import math
import collections

def reachTheEnd(grid, maxTime):
    
    # Write your code here
    grid = [i for i in grid]
    yes = 'Yes'
    no = 'No'
    counter = 0
    
    for i in grid:
        counter += i.count('.')
    
    if maxTime <= counter:
        return yes
        
    elif i != i[::-1]:
        return no 
    else:
        return no
    print(counter)
导入数学
导入集合
def reachTheEnd(网格,最大时间):
#在这里编写代码
grid=[i代表网格中的i]
是=‘是’
否='否'
计数器=0
对于网格中的i:
计数器+=i.count('.'))

如果maxTime广度优先搜索的思想是:(1)不访问同一节点两次,(2)持续维护一个未访问的节点列表,按时间片划分。最终,您将访问所有可访问的节点,并且对于您确实访问的任何节点,您都将以尽可能少的步骤访问它

在下面的算法中,我们创建一个缓存以满足(1)和一个队列以满足(2)。在每个时间片中,我们检查队列的每个元素,并将队列完全替换为一个新队列,该队列由在该时间片中发现的元素组成。每当我们发现一个新元素时,我们都会将它与第一次发现它的时间片一起添加到缓存中——因此,这一定是到达该新元素的最快路径

如果我们在到达目的地之前没有时间或没有新的细胞进行探索,那么我们就失败了。否则,我们就会成功。我们只需对退出条件进行编码,并在退出
while
循环时检查是否访问了目的地,就可以检查这一点

def reachTheEnd(grid, maxTime):
    # mark what the coordinates of the destination are
    destination = (len(grid) - 1, len(grid[-1]) - 1)
    # initialize
    #   - a step counter
    #   - a cache of visited cells on the grid
    #   - a queue of not-yet-visited cells that are adjacent to visited cells
    counter = 0
    cache = {(0, 0): 0}
    queue = [(0, 0)]
    # perform breadth-first search on the current queue, moving forward one
    # timeslice. On each timeslice, we take one 'step' forward in any direction
    # towards a newly-accessible tile on the grid.
    # our 'exit' conditions are
    #   - we run out of time
    #   - there is no path to the end of the maze
    #   - we reach the end
    while counter < maxTime and len(queue) > 0 and destination not in cache:
        counter += 1
        new_queue = []
        for (x, y) in queue:
            # check adding to path in all directions (up, down, left, right)
            # If the step is
            #   - not out-of-bounds
            #   - not a wall
            #   - not already visited
            # then add it to the cache with the current step count, as this is
            # is the most quickly we can reach it. Also add it to the queue of 
            # cells to investigate on the next timeslice.
            for (dx, dy) in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                (nx, ny) = (x + dx, y + dy)
                if 0 <= nx < len(grid) \
                        and 0 <= ny < len(grid[nx]) \
                        and grid[nx][ny] == '.' \
                        and (nx, ny) not in cache:
                    cache[(nx, ny)] = counter
                    new_queue.append((nx, ny))
        queue = new_queue
    # by the time the loop exits, either we've failed to reach the end, 
    # or the end is in the cache with the quickest possible path to it.
    if destination in cache:
        return "Yes"
    else:
        return "No"
def reachTheEnd(网格,最大时间):
#标记目的地的坐标
目的地=(len(网格)-1,len(网格[-1])-1)
#初始化
#-步进计数器
#-网格上已访问单元的缓存
#-与访问的小区相邻的尚未访问的小区队列
计数器=0
缓存={(0,0):0}
队列=[(0,0)]
#对当前队列执行广度优先搜索,向前移动一个队列
#时间片。在每个时间片上,我们朝着任何方向前进一步
#朝向网格上新可访问的瓷砖。
#我们的“退出”条件是
#-我们没时间了
#-没有通往迷宫尽头的路
#-我们到达终点
当计数器0且目标不在缓存中时:
计数器+=1
新队列=[]
对于队列中的(x,y):
#选中添加到所有方向(上、下、左、右)的路径
#如果步骤是
#-不出界
#-不是墙
#-尚未访问
#然后使用当前步数将其添加到缓存中,如下所示
#是我们能最快到达的。还将其添加到
#要在下一个时间片上调查的单元格。
对于[(-1,0)、(1,0)、(0,-1)、(0,1)]中的(dx,dy):
(nx,ny)=(x+dx,y+dy)

如果0广度优先搜索的思想是:(1)不访问同一个节点两次,(2)持续维护一个未访问的节点列表,按时间片划分。最终,您将访问所有可访问的节点,并且对于您确实访问的任何节点,您都将以尽可能少的步骤访问它

在下面的算法中,我们创建一个缓存以满足(1)和一个队列以满足(2)。在每个时间片中,我们检查队列的每个元素,并将队列完全替换为一个新队列,该队列由在该时间片中发现的元素组成。每当我们发现一个新元素时,我们都会将它与第一次发现它的时间片一起添加到缓存中——因此,这一定是到达该新元素的最快路径

如果我们在到达目的地之前没有时间或没有新的细胞进行探索,那么我们就失败了。否则,我们就会成功。我们只需对退出条件进行编码,并在退出
while
循环时检查是否访问了目的地,就可以检查这一点

def reachTheEnd(grid, maxTime):
    # mark what the coordinates of the destination are
    destination = (len(grid) - 1, len(grid[-1]) - 1)
    # initialize
    #   - a step counter
    #   - a cache of visited cells on the grid
    #   - a queue of not-yet-visited cells that are adjacent to visited cells
    counter = 0
    cache = {(0, 0): 0}
    queue = [(0, 0)]
    # perform breadth-first search on the current queue, moving forward one
    # timeslice. On each timeslice, we take one 'step' forward in any direction
    # towards a newly-accessible tile on the grid.
    # our 'exit' conditions are
    #   - we run out of time
    #   - there is no path to the end of the maze
    #   - we reach the end
    while counter < maxTime and len(queue) > 0 and destination not in cache:
        counter += 1
        new_queue = []
        for (x, y) in queue:
            # check adding to path in all directions (up, down, left, right)
            # If the step is
            #   - not out-of-bounds
            #   - not a wall
            #   - not already visited
            # then add it to the cache with the current step count, as this is
            # is the most quickly we can reach it. Also add it to the queue of 
            # cells to investigate on the next timeslice.
            for (dx, dy) in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
                (nx, ny) = (x + dx, y + dy)
                if 0 <= nx < len(grid) \
                        and 0 <= ny < len(grid[nx]) \
                        and grid[nx][ny] == '.' \
                        and (nx, ny) not in cache:
                    cache[(nx, ny)] = counter
                    new_queue.append((nx, ny))
        queue = new_queue
    # by the time the loop exits, either we've failed to reach the end, 
    # or the end is in the cache with the quickest possible path to it.
    if destination in cache:
        return "Yes"
    else:
        return "No"
def reachTheEnd(网格,最大时间):
#标记目的地的坐标
目的地=(len(网格)-1,len(网格[-1])-1)
#初始化
#-步进计数器
#-网格上已访问单元的缓存
#-与访问的小区相邻的尚未访问的小区队列
计数器=0
缓存={(0,0):0}
队列=[(0,0)]
#对当前队列执行广度优先搜索,向前移动一个队列
#时间片。在每个时间片上,我们朝着任何方向前进一步
#朝向网格上新可访问的瓷砖。
#我们的“退出”条件是
#-我们没时间了
#-没有通往迷宫尽头的路
#-我们到达终点
当计数器0且目标不在缓存中时:
计数器+=1
新队列=[]
对于队列中的(x,y):
#选中添加到所有方向(上、下、左、右)的路径
#如果步骤是
#-不出界
#-不是墙
#-尚未访问
#然后使用当前步数将其添加到缓存中,如下所示
#是我们能最快到达的。还将其添加到
#要在下一个时间片上调查的单元格。
对于[(-1,0)、(1,0)、(0,-1)、(0,1)]中的(dx,dy):
(nx,ny)=(x+dx,y+dy)

如果0非常好的实现非常好的实现