Algorithm to increase genius maze time limit: 1.0s memory limit: 256.0MBProblem description Genius robbed everyone's homework, monitor to help students find homework, decided to find genius duel. But genius in order not to disturb others, live in a castle, outside the castle is a two-dimensional lattice maze, to enter the castle must first through the maze. Because the squad leader and sister to accompany, sharpening not mistakenly chopping wood work, he to save time, from the line people got a maze map, ready to calculate the shortest route ahead. But now he is explaining the matter to his sister, so he will entrust you to find the shortest route for him. Input format the first line two integers n, m, for the length and width of the maze.
The next n lines, the number of m in each line, are not spaced between 0 or 1. 0 means that the lattice can pass, and 1 means no. Suppose you are now in the labyrinth coordinates (in the upper-left corner of the maze), the exit of the Maze (n,m). Each move can only be moved up and down in 4 directions to another pass through the grid, each move to calculate one step. Data Assurance (N,M) can be passed. Output format the first line one number is the minimum number of steps required K.
The second line is k characters, each character ∈{u,d,l,r}, respectively, indicating the upper and lower sides. If there are multiple shortest paths of the same length, select the one with the smallest dictionary order under this representation method. Sample inputs Input Sample 1:
3 3
001
100
110
Input Sample 2:
3 3
000
000
000 Sample outputs Output sample 1:
4
Rdrd
Output Sample 2:
4
DDRR data size and conventions have 20% data to meet: 1<=n,m<=10
There are 50% data to meet: 1<=n,m<=50
There are 100% of data to meet: 1<=n,m<=500.
#include <iostream>#include<algorithm>#include<cstdlib>#include<utility>#include<map>#include<cstdio>#include<queue>using namespacestd;Const intMAXN = -+Ten;Const intINF =100000000; typedef pair<int,int>P;//D (bottom), L (left), R (right), U (upper)intdir[4][2] = { {1,0}, {0, -1}, {0,1}, {-1,0}};Chardir_c[4] = {'D','L','R','U'};intRow, col;//ranksCharMAZE[MAXN][MAXN];//an array of strings that represent mazesintD[MAXN][MAXN];//an array of the shortest distances to each locationstringMin;//U,d,l,rQueue<p>que; voidinput (); BOOLJudgeintRintc);intBFS ();voidinput () {scanf ("%d%d", &row, &col); for(inti =0; i < row; i++) { for(intj =0; J < Col; J + +) {cin>>Maze[i][j]; } } //All locations Initialized for(inti =0; i < row; i++) { for(intj =0; J < Col; J + +) {D[i][j]=INF; } } }BOOLJudgeintRintc) { return(R >=0&& r < Row) && (c >=0&& C <Col)&& (maze[r][c]! ='1');//can go}intBFS () {//set the starting point to the queue and the distance of this location to 0Que.push (P (0,0)); Queue<string>path; Path.push (""); d[0][0] =0; Min=""; while(!Que.empty ()) {p P=Que.front (); Que.pop (); stringt =Path.front (); Path.pop (); if(P.first = = row-1&& P.second = = col-1) {Min= t;//because my direction is to follow the dictionary order Dlru, so this time the shortest route to form the path is to follow the smallest dictionary order route! Break; } for(inti =0; I <4; i++) { //the position after the move is (Nx,ny) intNX = P.first + dir[i][0], NY = P.second + dir[i][1]; //can go, and has not been accessed (D[nv][ny]==inf if(Judge (NX, NY) && d[nx][ny] = =INF) { //added to the queue, and the distance to that location is determined to be +1 of the distance to PQue.push (P (NX, NY)); Path.push (t+ dir_c[i]);//The data structure here is not good, I should start by combining the path and location into a structure, it will be more convenientD[nx][ny] = D[p.first][p.second] +1;//because the direction is to follow the DLRU dictionary sequence traversal, so there is no need to have any additional judgment, only need and walking routeMaze[nx][ny] ='1';//Get out of the team together! } } } returnD[row-1][col-1];}voidsolve () {input (); intres =BFS (); printf ("%d\n%s\n", Res, MIN.C_STR ());}intMain () {solve (); return 0;}
Blue Bridge Cup algorithm improves Genius's maze classic BFS problem