A - Maze

topic:

Dongdong has a map and wants to find the sister paper through the map. The map shows that 0 means you can walk, 1 means you can't walk, the upper left corner is the entrance, and the lower right corner is the sister paper. These two positions are guaranteed to be 0. Now that you know the map, it is not difficult for Dongdong to find sister paper. Please write a program to write the shortest route for Dongdong to find sister paper.

Input:

The input is a 5 × 5 two-dimensional array, consisting of only two digits 0 and 1, representing a normal matrix map.

Output:

Several lines are output, representing the coordinates of the shortest path from the upper left corner to the lower right corner, in the format shown in the sample. The data is guaranteed to have a unique solution.

Sample Input:

0 1 0 0 0
0 1 0 1 0
0 1 0 1 0
0 0 0 1 0
0 1 0 1 0

Sample Output:

(0, 0)
(1, 0)
(2, 0)
(3, 0)
(3, 1)
(3, 2)
(2, 2)
(1, 2)
(0, 2)
(0, 3)
(0, 4)
(1, 4)
(2, 4)
(3, 4)
(4, 4)

Hint:

The coordinates (x, y) indicate the x-th row and y-column, the row and column numbers start from 0, and the upper left corner is used as the origin. Also note that there should be a space after the comma separating the coordinates in the output.

Ideas:

The problem is to find a path from the origin (0,0) to the end point (5,5) in a plane rectangular coordinate system.
A point is represented by the structure point, two constant arrays of dx and dy indicate up, down, left, and right directions. Use the bfs method to create a queue que. When que is not empty, take the head element, Determine whether it reaches the end point, if it is reached, it is ready to output, if not, press the point around the point into the que, and mark the vis array according to the coordinates of the point, the mark value is the mark value of the previous point in vis Add one and do so until you reach the end. When outputting, start from the end point to find a feasible path back in the vis array, and then output in reverse order to get the path from the start point to the end point.

Code:

#include <iostream>
#include <queue>
#include <string.h>
#include <vector> 

using namespace std;

struct point
{
	int x,y;
	
	point()
	{
	}
	point(int a,int b)
	{
		x=a;
		y=b;
	}
};

int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int vis[5][5];//标记最小到达需要几步 

void bfs(int maze[5][5])
{
	memset(vis,-1,sizeof(vis));
	queue<point> que;
	que.push(point(0,0));
	vis[0][0]=0;
	while(!que.empty())
	{
		point p=que.front();
		que.pop();
		if(p.x==4&&p.y==4)
			break;
		for(int i=0;i<4;i++)
		{
			int x=p.x+dx[i];
			int y=p.y+dy[i];
			if(0<=x&&x<=4&&0<=y&&y<=4&&maze[x][y]!=1&&vis[x][y]==-1)
			{
				vis[x][y]=vis[p.x][p.y]+1;
				que.push(point(x,y));		
			}
		}
	}
}

void print()
{
	vector<point> v;
	point now(4,4);
	v.push_back(point(4,4));
	while(1)
	{
		if(now.x==0&&now.y==0)
			break;
		for(int i=0;i<4;i++)
		{
			int x=now.x+dx[i];
			int y=now.y+dy[i];
			if(0<=x&&x<=4&&0<=y&&y<=4&&vis[x][y]==vis[now.x][now.y]-1)
			{
				v.push_back(point(x,y));
				now.x=x;
				now.y=y;
				break;		
			}
		}
	}
	vector<point>::reverse_iterator it=v.rbegin();
	printf("(%d, %d)",it->x,it->y);
	it++;
	for(;it!=v.rend();it++)
		printf("\n(%d, %d)",it->x,it->y); 
}

int main()
{
	int maze[5][5];
	for(int i=0;i<5;i++)
		for(int j=0;j<5;j++)
			cin>>maze[i][j];
	bfs(maze);
	print();
	return 0;
}
Published 32 original articles · praised 0 · visits 700

Guess you like

Origin blog.csdn.net/qq_43814559/article/details/104660624