Waterloo Network algorithm to improve Couba maze __ACM

Source: Internet
Author: User
Algorithm to improve Couba maze
Time limit: 1.0s memory limit: 256.0MB

Problem description
Couba robbed everyone's homework, monitor to help students find homework, decided to find Couba duel. But Couba in order not to disturb others, live in a castle, the castle outside is a two-dimensional lattice maze, to enter the castle must first through the maze. Because the monitor also has the sister to accompany, the knife is not mistaken for firewood, he in order to save time, from the informant to get to the maze map, ready to calculate the shortest route in advance. But now he is explaining the matter to her sister, so he commissioned you to help him find a shortest route.
Input format
The first line is two integers n, m, the length of the maze.
The next n rows, the number of M per line, no interval between the numbers, is one of 0 or 1. 0 means this lattice can be passed, 1 means not. Suppose you are now in the maze coordinates (1,1) place, that is, the upper left corner, the Labyrinth exits in (n,m). Each move can only be moved to the top and bottom 4 directions to another one can pass the grid, each move to calculate one step. Data assurance (1,1), (n,m) can be passed.
Output format
The first line of a number is the minimum number of steps required K.
The second line of K characters, each character ∈{u,d,l,r}, respectively, representing up and down. 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 convention
20% of the data is satisfied: 1<=n,m<=10
50% of the data is satisfied: 1<=n,m<=50

100% of the data is satisfied: 1<=n,m<=500.


This is the classic breadth-first search example, the title added to the print path, you have to increase the father of the array record Father node, in addition to the most Dictionary order to print the path at each node in the ' D ', ' L ', ' R ', ' U ' sequential traversal

Did not write the annotation, the beginner BFS looks may be laborious, the thought is not difficult



# include <stdio.h> # include <string.h> # include <iostream> # include <queue> using namespace St
D
int n, m;
Char ans[100000];
int x[4]={-1, 0, 0, 1};
int y[4]={0, 1,-1, 0};
Queue<int> Q;
int vis[510][510];
int direct[510][510];
int g[510][510];
int d[510][510];
int father[510][510];
	int main () {int I, j, K, R, C;
	Char s[510];
	int no;
	Memset (g,-1, sizeof (g));
	scanf ("%d%d", &n, &m);
	GetChar ();
		For (I=1 i<=n; i++) {scanf ("%s", s);
		For (j=0 j<=m-1; j + +) {g[i][j+1]=s[j]-' 0 ';
	}} d[1][1]=0;
	no=0;
	Q.push (no);
	memset (Vis, 0, sizeof (VIS));
	Vis[1][1]=1;
	memset (Direct,-1, sizeof (direct));
		while (!q.empty ()) {No=q.front ();
		Q.pop ();
		r=no/m+1;c=no%m+1;
					For (i=3 i>=0; i--) {if (g[r+x[i]][c+y[i]]==0) {if (vis[r+x[i]][c+y[i]]==0) {vis[r+x[i]][c+y[i]]=1;
					d[r+x[i]][c+y[i]]=d[r][c]+1;
					Father[r+x[i]][c+y[i]]=no;
					Direct[r+x[i]][c+y[i]]=i;
				Q.push ((r+x[i]-1) *m+c+y[i]-1); } else{if (d[r+x[i]][c+y[i]]>d[r][c]+1) {d[r+x[i]][c+y[i]]=d[r][c]+1;
						Father[r+x[i]][c+y[i]]=no;
						Direct[r+x[i]][c+y[i]]=i;
					Q.push ((r+x[i]-1) *m+c+y[i]-1);
	{}}}} R=n, c=m;
	int count=0; while (!) (
		R==1&&c==1)) {if (direct[r][c]==0) {ans[count++]= ' U ';
		else if (direct[r][c]==1) {ans[count++]= ' R ';
		else if (direct[r][c]==2) {ans[count++]= ' L ';
		} else{ans[count++]= ' D ';
		} No=father[r][c];
	r=no/m+1;c=no%m+1;
	printf ("%d\n", D[n][m]);
	For (i=count-1 i>=0; i--) {printf ("%c", Ans[i]);
return 0; }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.