Program Job week02-A title

A-Maze
title
Here Insert Picture DescriptionSample

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)

Here Insert Picture Description

Problem-solving ideas

This is a classic maze problem (four connectivity), you can use the search path issue dfs can also be used bfs. The purpose of this question is to find the shortest path to find the required sister paper, layer by layer bfs search to find the end of the first path is the shortest, if dfs then choose to find all solutions, so here I am using the bfs method.

Mp defined memory map array, for determining whether an array VIS nodes are accessed, the initialization represents the beginning not accessed to all zeros. Node coordinate information storage node definition of the structure, the size is 5 * 5. Then define a type of queue node, recording the node bfs.

int mp[5][5];
int vis[5][5];
struct node
{
	int x, y;
};
node pre[5][5];
queue<node> q;

BFS bfs () :
this is an ordinary breadth-first search

First starting point (0, 0) into the queue, and marked, i.e. vis [0] [0] = 1.
Then enters a loop, and referred to as the first team taken p, then the node pop

Four nodes of the search node can be reached, where the search uses a cyclic manner, so that the coordinate plus 1, or 0 or 1. Because only four directions up and down, so to add a constraint to increase the amount of x, y of the and either 1 or -1. This can eliminate the case of four directions and diagonally to stay put

When traversing these four nodes, to judge whether a valid point, determines whether the coordinates of the map beyond the range, and whether the point is accessed, i.e. whether the value of a vis.

If the node is accessible, it is added to the queue, and marked as a vis, a pre recorded before adding a new node (corresponding to Dad qwq), in order to later output path


```cpp
for(int i=-1;i<2;i++)
		{
			for(int j=-1;j<2;j++)
			{
				node nn;
				nn.x = p.x+i;
				nn.y = p.y+j;
				if(i+j==1||i+j==-1)
				{
					if((nn.x<0||nn.x>4||nn.y<0||nn.y>4)||vis[nn.x][nn.y]==1||mp[nn.x][nn.y]==1)
					continue;
					q.push(nn);
					vis[nn.x][nn.y] = 1;
					pre[nn.x][nn.y] = p;
				}
			}
		}

Loop until the queue is empty (in fact, think of this topic can be added in a judgment at the end of the cycle, if the end point found (4,4) to empty the queue to stop the search because the first time I met with ... The end is the shortest path to write a qwq when we do not think)

Track output path ()

Because the record prior to a node in the wide search, when the output paths are usually two ways: First, push forward from the end, the way there is an array of nodes, and finally reverse output; the other is Recursion the method, beginning from the end stop on one of the nodes recursively until the starting point is a recursive function parameters, return to the previous output. Both can be, here I use recursion.


```cpp
void track(node pp)
{
	if(pp.x==0&&pp.y==0)
	{
		cout<<"(0, 0)"<<endl;
		return;
	}
	track(pre[pp.x][pp.y]);
	cout<<'('<<pp.x<<", "<<pp.y<<')'<<endl;
}

The complete code


```cpp
#include <iostream>
#include <queue>
using namespace std;

int mp[5][5];
int vis[5][5];
struct node
{
	int x, y;
};
node pre[5][5];
queue<node> q;

void bfs()
{
	node p;
	p.x = 0;	p.y = 0;
	q.push(p);	
	vis[0][0] = 1;
	while(!q.empty())
	{
		p = q.front();
		q.pop();
		for(int i=-1;i<2;i++)
		{
			for(int j=-1;j<2;j++)
			{
				node nn;
				nn.x = p.x+i;
				nn.y = p.y+j;
				if(i+j==1||i+j==-1)
				{
					if((nn.x<0||nn.x>4||nn.y<0||nn.y>4)||vis[nn.x][nn.y]==1||mp[nn.x][nn.y]==1)
					continue;
					q.push(nn);
					vis[nn.x][nn.y] = 1;
					pre[nn.x][nn.y] = p;
				}
			}
		}
	}
}
void track(node pp)
{
	if(pp.x==0&&pp.y==0)
	{
		cout<<"(0, 0)"<<endl;
		return;
	}
	track(pre[pp.x][pp.y]);
	cout<<'('<<pp.x<<", "<<pp.y<<')'<<endl;
}
int main()
{
	for(int i=0;i<5;i++)
	{
		for(int j=0;j<5;j++)
		{
			cin>>mp[i][j];
			vis[i][j] = 0;
		}
	}
	bfs();
	node end;
	end.x = 4;	end.y = 4;
	track(end);
	return 0;
}
Published an original article · won praise 0 · Views 11

Guess you like

Origin blog.csdn.net/wakeupshely/article/details/104605364