Labyrinth with portal

The original text is on my blog: http://leetcode.liboer.top
Title Description
Given a maze, the number of steps required to find the fastest path from the starting point to the key point. Suppose the maze is as follows, assuming that the coordinates of the upper left corner are (0, 0), and the coordinates of the lower right corner are (3, 2)
1 0 -1 1
-2 0 -1 -3
2 2 0 0
-2 is the starting point of the maze, and the coordinates are ( 0, 1)
-3 is the end of the maze. The coordinates are (3, 1).
-1 represents obstacles. Cannot walk.
1 and 2 represent portals. Portals are marked by positive integers and only appear in pairs. Standing on a portal, it can be teleported to another portal location with the same number in just one step: 1 can only be teleported to 1, 2 can only be teleported to 2. Standing on the portal can also choose not to teleport.
There are several ways to move from the start point to the end point, for example:
(0,1)->(1,1)->(1,2)->(2,2)->(3,2)->(3 , 1), a total of 5 steps
or
(0,1)->(0, 0) -transmission> (3,0)->(3,1), a total of 3 steps. 3 steps are the minimum required after inspection The number of steps, the final result returns 3.
Input description
Each line of input is an integer separated by a space. The
first line gives the length and width of the maze map, which are both positive integers
. Each number in each subsequent line represents a grid of the maze.
-2 represents the starting point, and -3 Indicates the end point, -1 indicates an impassable obstacle, 0 indicates a passable road, and a positive integer greater than 0 indicates a portal, and it is guaranteed to appear in pairs. On a portal, it can be transmitted to another in one step. The location of the digital portal].
Output description
The minimum number of steps required to output is able to go from the starting point to the ending point.
Output -1 if there is no way to go from the start point to the end point.
Test case
input
4 3
1 0 -1 1
-2 0 -1 -3
2 2 0 0
output
3

The data is preprocessed during data input, and the
key points have been annotated

class Node:
    def __init__(self, x, y, v):
        self.x = x
        self.y = y
        self.v = v


def dfs(point, depth):
    global minstep
    f = 0
    if point.v == -3:
        # 到达终点
        if depth < minstep:
            minstep = depth
        return depth
    vis[point.x][point.y] = 1
    for i in range(4):
        # 上下左右递归
        x = point.x + direction[i][0]
        y = point.y + direction[i][1]
        if x < 0 or x >= n or y < 0 or y >= m:
            # 越界
            continue
        if vis[x][y] == 1 or arr_map[x][y] == -1:
            # 已访问过或障碍物
            continue
        node = Node(x, y, arr_map[x][y])
        vis[x][y] = 1
        if node.v > 0:
            for s in send:
                if s.v == node.v:
                    if s.x != node.x or s.y != node.y:
                        # 传送门
                        f = dfs(s, depth+2)
                        # 迭代完事回来之后要重新从这里出发,要把访问标识是为未访问
                        vis[x][y] = 0
        else:
            f = dfs(node, depth+1)
            # 迭代完事回来之后要重新从这里出发,要把访问标识是为未访问
            vis[x][y] = 0
    return f


m, n = list(map(int, input().split()))
minstep = float('inf')
arr_map = []
vis = [[0 for i in range(m)] for i in range(n)]
send = []
direction = [[-1, 0], [1, 0], [0, -1], [0, 1]]  # 上下左右
for i in range(n):
    line = list(map(int, input().split()))
    arr_map.append(line)
    for j in range(m):
        if line[j] == -2:
            # 起点
            start = Node(i, j, -2)
        if line[j] == -3:
            # 终点
            end = Node(i, j, -3)
        if line[j] > 0:
            # 传送门
            p = Node(i, j, line[j])
            send.append(p)

dfs(start, 0)
if minstep == float('inf'):
    print(-1)
else:
    print(minstep)


"""
4 3
1 0 -1 1
-2 0 -1 -3
2 2 0 0

4 3
0 1 -1 0
-2 0 -1 -3
2 1 0 2

4 3
0 1 -1 0
-2 0 -1 -3
0 1 -1 0

"""

Guess you like

Origin blog.csdn.net/qq_31910669/article/details/115289493