POJ 3984 Maze problem

Source: Internet
Author: User

Do this problem actually I want to see the way of printing path, water, look at the code of others on the internet, the print path is basically to save the parent node, and then

Print out its coordinates, I wrote a recursive, from behind to find the starting point, the process is a little different, not saved before, but continue up and down around the

Look, estimate is because this time a bit more, although in this problem can not see the difference ... Inadvertently saw the blog, I do this problem nearly a year later than her ...

Code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> int dist[10][10];
int visit[10][10];
int gra[10][10];
const int Dx[4] = { -1,1,0,0};
const int Dy[4] = {0,0,-1,1};
using namespace Std;
struct node {int x, y;};
	void BFs (int x,int y) {int i;
	queue<node>que;
	memset (visit,0,sizeof (visit));
	memset (dist,0,sizeof (Dist));
	Node N1;
	n1.x = x;
	N1.y = y;
	Dist[x][y] = 0;
	Que.push (N1);
	Visit[x][y] = 1;
		while (!que.empty ()) {node N1 = Que.front ();
		Que.pop ();
			if (n1.x = = 5) && (n1.y = = 5)) {//printf ("%d\n", Dist[n1.x][n1.y]);
		return;
			} for (i=0; i<4; i++) {int xx = n1.x + dx[i];
			int yy = N1.y + dy[i]; if (!visit[xx][yy] && (gra[xx][yy] = = 0) && (xx >= 1) && (xx <= 5) && (yy >= 1) &am
				p;& (yy <= 5)) {Visit[xx][yy] = 1;
				DIST[XX][YY] = dist[n1.x][n1.y] + 1;
				Node N2;
				n2.x = Xx,n2.y = yy;
			Que.push (N2); }}}} void Print_path(Node N1,node n2)
	{int i;
	if (n2.x = = n1.x) && (n1.y = = n2.y)) return;
		for (i=0; i<4; i++) {int xx = n2.x + dx[i];
		int yy = n2.y + dy[i];
			if ((dist[n2.x][n2.y] = = Dist[xx][yy] + 1) && Visit[xx][yy]) {node N3;
			n3.x =xx,n3.y =yy;
			Print_path (N1,N3);
		printf ("(%d,%d) \ n", n3.x-1,n3.y-1);
}} return;
	} int main () {int i,j;
	for (I=1; i<=5; i++) for (j=1; j<=5; j + +) scanf ("%d", &gra[i][j]);	
	BFS (a);
	Node n1,n2;
	n1.x = 1, n1.y = 1;
	n2.x = 5, n2.y = 5;
	Print_path (N1,N2);
	printf ("(4, 4)");
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.