Huawei OD machine test-robot walking the maze

 Question description

The robot walks a maze, given the x and y of the maze (x*y maze) and there are obstacles in the maze, input k means there are k obstacles, and the coordinates of the obstacles will be input one by one. The robot starts from
0,0 The position goes to the position of x, y and can only go in the direction of increasing x, y, and cannot go back.
As shown in the code class comments, # represents the square that can go, 0 represents obstacles, and the robot starts from 0, 0 The location can only go down or forward to the exit.
There will be unreachable squares and trap squares. The unreachable squares are the first three in the fourth row, and the robot cannot go to the square on the walking path. , such as the last two trap squares in the first row, you cannot reach the end point after walking into them.
Requirements: Output the number of traps and unreachable squares.

1. The room is composed of X*Y squares, for example, the picture below is 6*4 in size. Each one is described by coordinates (x, y)
2. The robot starts from the grid (,) and can only go east or north. The entrance and exit are fixed as the northeast corner of the room, as shown in the grid (5,3) ). The use case ensures that the robot can walk from the entrance to the exit.
3. Some squares in the room are walls, such as (4,1) where the robot cannot pass.
4. Some places cannot go to the exit once they arrive, such as the square marked B, which is called a trap square
5. Some places cannot be reached by robots, such as the square marked A, called unreachable The reachable squares and the unreachable squares do not include the location of the wall. 6. In the example picture below, there are 2 trap squares and 3 unreachable squares.
7. Please implement the path planning Q function for the robot: given the size of the room and the location of the walls, please calculate how many trap squares and unreachable squares there are.

Code

# coding:utf-8
"""
@Date   :2023/7/22
@Title  :机器人走迷宫
@discript:https://dream.blog.csdn.net/article/details/128986089
"""


def robotWalkMaze(x, y, obs):
    dp = [['#'] * y for _ in range(x)]

    # 把墙壁坐标对应的结果标记为0
    for ob in obs:
        i, j = ob
        dp[i][j] = 0

    def dfs(x_, y_):
        if x_ == x - 1 and y_ == y - 1:  # 如果坐标等于出口位置,返回路线可用,标记1
            dp[x_][y_] = 1
            return 1
        elif x_ >= x or y_ >= y or dp[x_][y_] == 0:  # 如果坐标大于等于边界,或者dp中标记为0,即墙壁,这路线标记为-1,不可用
            return -1
        elif dp[x_][y_] != '#':  # 如果当前位置不等于#,即已经被标记过,返回该标记即可
            return dp[x_][y_]
        else:  # 按照深度优先算法先向下走,再向右走
            down = dfs(x_ + 1, y_)
            right = dfs(x_, y_ + 1)
            if down == -1 and right == -1:  # 如果当前位置标记为向下和向右都标记为-1,即说明该位置是陷阱方块
                dp[x_][y_] = -1
            else:
                dp[x_][y_] = max(down, right)  # 位置信息取向下或者向右最大值,其实就是只要有1就ok
            return dp[x_][y_]

    dfs(0, 0)
    r1 = sum(line.count(-1) for line in dp)
    r2 = sum(line.count('#') for line in dp)  # 位置标记没被更新,说明是不可达的方块
    return r1, r2


x, y = map(int, input('X,Y:').split())
obss = []

for _ in range(int(input('N:'))):
    obj = tuple(map(int, input('location:').split(' ')))
    obss.append(obj)

c1, c2 = robotWalkMaze(x, y, obss)
print(c1, c2)

Guess you like

Origin blog.csdn.net/SD_JZZ/article/details/132666722