Improving the maze of Xueba by test algorithm
Resource constraints
Time limit: 1.0s memory limit: 256.0MB
Problem description
Xueba robbed everyone of their homework. In order to help the students get back their homework, the monitor decided to go to Xueba for a duel. But Xueba lives in a castle in order not to be disturbed. Outside the castle is a two-dimensional lattice maze. To enter the castle, you must first pass through the maze. Because the monitor still has his sister to accompany him, he can't make a mistake to cut firewood. In order to save time, he got the map of maze from the informant and prepared to calculate the shortest route in advance. But now he is explaining this to his sister, so he entrusts you to help him find the shortest route.
Input format
The first line, two integers n, m, is the length and width of the maze.
Next N lines, the number of m in each line, with no interval between them, is one of 0 or 1. 0 indicates that this lattice can pass, 1 indicates that it cannot. Let's say you are now at the maze coordinates (1,1), i.e. the upper left corner, and the exit of the maze is at (n,m). Each time you move, you can only move up, down, left and right to another grid that can pass through. Each move is one step. Data guarantee (1,1), (n,m) can be passed.
Output format
One number in the first row is the minimum number of steps K required.
The second line is K characters, each character ∈ {U,D,L,R}, which represents up and down respectively. If there are multiple shortest paths of the same length, select the one with the smallest dictionary order under this representation method.
sample input
Input Sample 1:
3 3
001
100
110
Input Sample 2:
3 3
000
000
000
sample output
Output Sample 1:
4
RDRD
Output Sample 2:
4
DDRR
Data size and engagement
20% of the data meet the following requirements: 1 < = n, m < = 10
50% of the data meet the following requirements: 1 < = n, m < = 50
100% of the data meet the following requirements: 1 < = n, m < = 500.
""" @Author:Lixiang @Blog(Personal blog address): https://lixiang007.top/ @WeChat: 18845312866 """ import math import string import sys from itertools import permutations class Node: def __init__(self, x, y, w): self.x = x #Record abscissa self.y = y #Record ordinates self.w = w #Record path def __str__(self): return self.w #Output path def up(node): return Node(node.x, node.y - 1, node.w+"U") #Situation on def down(node): return Node(node.x, node.y + 1, node.w+"D") ##Situation under def left(node): return Node(node.x - 1, node.y, node.w+"L") ##Left situation def right(node): return Node(node.x + 1, node.y, node.w+"R") #Right case if __name__ == '__main__': m, n = map(int, input().split()) visited = set() #Record visited points queue = [] map_int = [[0] * (m + 1)] for i in range(1, n+1): map_int.append([0]*(m+1)) nums = input() nums = "0" + nums for j in range(0, m+1): map_int[i][j] = ord(nums[j]) - 48 #ord to ascII node = Node(1, 1, "") #Set the starting point queue.append(node) while len(queue) != 0: moveNode = queue[0] #Set the current move point to moveNode queue.pop(0) moveStr = str(moveNode.x) + " "+ str(moveNode.y) #Used to record whether the current coordinate has passed if moveStr not in visited: visited.add(moveStr) if moveNode.x == m and moveNode.y == n: #Output and exit the cycle if the end point is reached print(len(moveNode.__str__())) #Step number print(moveNode) #Print path break if moveNode.y < n: #First, the order is below if map_int[moveNode.y + 1][moveNode.x] == 0: queue.append(down(moveNode)) if moveNode.x > 1: #The second order is left if map_int[moveNode.y][moveNode.x - 1] == 0: queue.append(left(moveNode)) if moveNode.x < m: #The third order is right if map_int[moveNode.y][moveNode.x + 1] == 0: queue.append(right(moveNode)) if moveNode.y > 1: #The last order is up if map_int[moveNode.y - 1][moveNode.x] == 0: queue.append(up(moveNode))
Don't run the example and change it yourself!