[BFS] Maze (C ++)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/liuzich/article/details/99960789

Description [title]

A maze of R rows and C columns consisting of a grid, the grid has some obstacles, we can not walk; lattice is some open space, you can go.

Given a maze, seeking the lower right corner from the top left corner went requires a minimum number of steps to go (to ensure that the data will be able to come). Only in the horizontal or vertical direction to go, you can not go sideways.

[Enter]

The first two lines are integers, R and C, representative of the length and width of the maze. (1≤ R, C ≤ 40)
followed by the R lines of C characters, representative of the maze.
Space lattice with a '' said obstacle lattice is represented by '#'.
The top left and bottom right corner of the maze are '.'.

[Output]

Output from the lower right corner of the upper left corner went at least to go through many steps (at least that is how many space lattice to go through). To calculate the number of steps, including start and end points.

[Sample input]

5 5
…###
#…
#.#.#
#.#.#
#.#…

[Sample Output]

9

【source】

No

Submit statistical information submitted records

I evaluate this question: typing problem.
This problem does have a simple, data range and small, DFS can also do, but also can open a large array of points,

Anyway, not money

Here Insert Picture DescriptionAnnotated code:

#include <bits/stdc++.h>		//C++的万能头文件 
using namespace std;		//名空间 
int n,m,p[10008],q[10008],ans[108][108];		//不要钱的数组开大点
char a[108][108];		//用于存地图 
int xx[5]= {0,1,-1,0,0};		//方向x
int yy[5]= {0,0,0,1,-1};		//方向y
void bfs() {
	a[1][1]='#';		//设初始点为不可走
	ans[1][1]=1;		//初始点一步可以到
	int head=1;			//这个不用说
	int tail=1;			//这个也不用说
	p[1]=1;		//把第一个位置的方向x加入队列
	q[1]=1;		//把第一个位置的方向y加入队列
	while(head<=tail) {	//BFS扩展
		for(int i=1; i<=4; i++) {	//四个方向搜索
			int h=p[head]+xx[i];
			int l=q[head]+yy[i];
			if(a[h][l]!='#'&&h>=1&&l>=1&&h<=n&&l<=m) {
				a[h][l]='#';		//走过了,以防再走,所以设为不可走
				//cout<<h<<" "<<l<<endl;	//跟踪数据(检查时用)
				//system("pause");			//跟踪数据(检查时用)
				tail++;		//有几个神奇的小朋友要进队列了
				p[tail]=h;		//神奇的小朋友1号,(方向x)
				q[tail]=l;		//神奇的小朋友2号,(方向y)
				ans[h][l]=ans[p[head]][q[head]]+1;		//把这个点的步数设为上一个点的步数+1
				if(h==n&&l==m) {	//判断是否到达终点
					cout<<ans[h][l]<<endl;		//输出结果
					return ;		//结束函数(易错点)
				}
			}
		}
		head++;		//头结点++(易错点)
	}
}
int main() {
	cin>>n>>m;		//输入长和宽
	/*下面是输入*/
	/*———————————————————*/
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=m; j++) {
			cin>>a[i][j];
		}
	}
	/*———————————————————*/
	/*上面是输入*/ 
	bfs();

	return 0;
}

No comment Code:

#include <bits/stdc++.h>
using namespace std;
int n,m,p[10008],q[10008],ans[108][108];
char a[108][108];
int xx[5]={0,1,-1,0,0};
int yy[5]={0,0,0,1,-1};
void bfs()
{
	a[1][1]='#';
	ans[1][1]=1;
	int head=1;
	int tail=1;
	p[1]=1;
	q[1]=1;
	while(head<=tail)
	{
		for(int i=1;i<=4;i++)
		{
			int h=p[head]+xx[i];
			int l=q[head]+yy[i];
			if(a[h][l]!='#'&&h>=1&&l>=1&&h<=n&&l<=m)
			{
				a[h][l]='#';
				tail++;
				p[tail]=h;
				q[tail]=l;
				ans[h][l]=ans[p[head]][q[head]]+1;
				if(h==n&&l==m)
				{
					cout<<ans[h][l]<<endl;
					return ;
				}
			}
		}
		head++;
	}
}
int main() {
	cin>>n>>m;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cin>>a[i][j];
		}
	}
	bfs();

	return 0;
}

Guess you like

Origin blog.csdn.net/liuzich/article/details/99960789