BFS类题目,从简单开始

区分用广搜还是深搜最大的区别主要在于一个解决的主要是走几步,一个解决的主要是有几种方法;
即:
BFS:几步路(一般是说找最优解!!!)
DFS:几种方法
首先解决BFS类问题一定要了解queue的用法,它贯穿整个BFS类题目
开始做题!

P1162 填涂颜色

题目描述
由数字0组成的方阵中,有一任意形状闭合圈,闭合圈由数字1构成,围圈时只走上下左右4个方向。现要求把闭合圈内的所有空间都填写成2.例如:6×6的方阵(n=6),涂色前和涂色后的方阵如下:

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

0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1
输入格式
每组测试数据第一行一个整数n(1≤n≤30)

接下来n行,由0和1组成的n×n的方阵。

方阵内只有一个闭合圈,圈内至少有一个0。

//感谢黄小U饮品指出本题数据和数据格式不一样. 已修改(输入格式)

输出格式
已经填好数字2的完整方阵。

输入输出样例
输入
6
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 0 0 1
1 1 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1
输出
0 0 0 0 0 0
0 0 1 1 1 1
0 1 1 2 2 1
1 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1

这题由于不知道方阵中哪个位置为0,也就是无法判断在方阵中起点应该从哪开始,所以我们应该在方阵的外面加一圈,从方阵外面进入开始寻找

#include <bits/stdc++.h>
using namespace std;
int a[35][35],vis[35][35];
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右 

int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){//注意这里是从1开始!!! 
		for(int j=1;j<=n;j++){
			cin>>a[i][j];
		}
	}
	memset(vis,0,sizeof(vis));//标记走过的位置 
	queue<int> x;queue<int> y;
	vis[0][0]=1;
	x.push(0),y.push(0);//由于不能确定输入的方阵中哪里的位置为1,也就是不能确定起点应该从哪里开始,所以我们就在方阵的外面加上一圈,从方阵外面开始出发寻找 
	while(!x.empty() && !y.empty()){
		for(int i=0;i<4;i++){
			int xx=x.front()+to[i][0],yy=y.front()+to[i][1];
			if(xx>=0&&yy>=0&&xx<=n+1&&yy<=n+1&&a[xx][yy]==0&&!vis[xx][yy]){//判断的时候注意我们在原方阵外面加了一圈 
				x.push(xx),y.push(yy);
				vis[xx][yy]=1;//标记走过的位置 
			}
		}
		x.pop(),y.pop();//将第一个数据移除列队 
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(a[i][j]==0&&vis[i][j]==0){//需要同时满足方阵内的数据为0且没有走过这个位置 
				cout<<"2 ";
			}
			else cout<<a[i][j]<<' ';
		}
		cout<<endl;
	}
    return 0;
}

P1443 马的遍历

题目
题目描述
有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步

输入格式
一行四个数据,棋盘的大小和马的坐标

输出格式
一个n*m的矩阵,代表马到达某个点最少要走几步(左对齐,宽5格,不能到达则输出-1)

输入输出样例
输入
3 3 1 1
输出
0 3 2
3 -1 1
2 1 4
比较复杂的写法:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
int ans[410][410],vis[410][410];
int to[8][2]={-1,-2 ,1,-2 ,-2,-1 ,2,-1 ,-2,1 ,2,1 ,-1,2 ,1,2};
int n,m,sx,sy;
typedef struct node{
	int x,y,t;
}node;
int main(){
	cin>>n>>m>>sx>>sy;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			ans[i][j]=INF;
		}
	}
	ans[sx][sy]=0;
	memset(vis,0,sizeof(vis));
	queue<node>a;
	node b;
	b.x =sx,b.y =sy,b.t =0;
	a.push(b);
	vis[sx][sy]=1;
	while(!a.empty()){
		for(int i=0;i<8;i++){
			int xx=a.front().x+to[i][0],yy=a.front().y+to[i][1];
			if(xx>0&&yy>0&&xx<=n&&yy<=m&&!vis[xx][yy]){
				vis[xx][yy]=1;
				a.push((node){xx,yy,a.front().t+1});
				ans[xx][yy]=a.front().t+1;
			} 
		}
		a.pop();
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(vis[i][j]==1)
			cout<<left<<setw(5)<<ans[i][j];
			else cout<<left<<setw(5)<<"-1";
		}
		cout<<endl;
	}
    return 0;
}

我们发现其实vis[]可以直接变成记录ans[]的数字,也就是它既可以记录走过的位置又可以同时记录下答案,具体写法:

#include <bits/stdc++.h>
using namespace std;
int vis[410][410];
int to[8][2]={-1,-2 ,1,-2 ,-2,-1 ,2,-1 ,-2,1 ,2,1 ,-1,2 ,1,2};
int n,m,sx,sy;
typedef struct node{
	int x,y,t;
}node;
int main(){
	cin>>n>>m>>sx>>sy;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			vis[i][j]=-1;
		}
	}
	vis[sx][sy]=0;
	queue<node>a;
	node b;
	b.x =sx,b.y =sy,b.t =0;
	a.push(b);
	while(!a.empty()){
		for(int i=0;i<8;i++){
			int xx=a.front().x+to[i][0],yy=a.front().y+to[i][1];
			if(xx>0&&yy>0&&xx<=n&&yy<=m&&vis[xx][yy]==-1){
				vis[xx][yy]=a.front().t+1;
				a.push((node){xx,yy,a.front().t+1});
			} 
		}
		a.pop();
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cout<<left<<setw(5)<<vis[i][j];
		}
		cout<<endl;
	}
    return 0;
}

结构体数据存入queue的两种方法:
方法一:
定义一个结构体b,将b赋值然后再将b的值给a

	queue<node>a;
	node b;
	b.x =sx,b.y =sy,b.t =0;
	a.push(b);

方法二:
强制转换

a.push((node){xx,yy,a.front().t+1});

左对齐的两种方法:
方法一:c++写法

cout<<left<<setw(5)<<vis[i][j];

方法二:C写法

printf("%-5d",vis[i][j]);

负数表示左对齐,正数表示右对齐

P1135 奇怪的电梯

题目
题目描述
呵呵,有一天我做了一个梦,梦见了一种很奇怪的电梯。大楼的每一层楼都可以停电梯,而且第i层楼(1≤i≤N)上有一个数字Ki(0≤Ki≤N)。电梯只有四个按钮:开,关,上,下。上下的层数等于当前楼层上的那个数字。当然,如果不能满足要求,相应的按钮就会失灵。例如:3, 3 ,1 ,2 ,5代表Ki(K1=3,K2=3,…),从1楼开始。在1楼,按“上”可以到4楼,按“下”是不起作用的,因为没有−2楼。那么,从A楼到B楼至少要按几次按钮呢?

输入格式
共二行。

第一行为3个用空格隔开的正整数,表示N,A,B(1≤N≤200,1≤A,B≤N)N,A,B(1≤N≤200,1≤A,B≤N)。
第二行为N个用空格隔开的非负整数,表示Ki 。

输出格式
一行,即最少按键次数,若无法到达,则输出−1。

输入输出样例
输入
5 1 5
3 3 1 2 5
输出
3

#include <bits/stdc++.h>
using namespace std;
int n,a,b;
int c[210],vis[210]={0};
int main(){
	cin>>n>>a>>b;
	for(int i=1;i<=n;i++){
		cin>>c[i];
	}
	queue<int>f;
	queue<int>step;
	f.push(a),step.push(0);
	vis[a]=1;
	while(!f.empty()){
		if(f.front()==b){
			cout<<step.front();
			return 0;
		}
		int d=f.front()+c[f.front()];
		if(d<=n&&!vis[d]){
			f.push(d),step.push(step.front()+1);
			vis[d]=1;
		}
		int e=f.front()-c[f.front()];
		if(e>0&&!vis[e]){
			f.push(e),step.push(step.front()+1);
			vis[e]=1;
		}
		f.pop();step.pop();
	}
	cout<<"-1";
    return 0;
}

4001:抓住那头牛

题目
这题和上面那题类似,不做过多解释

描述
农夫知道一头牛的位置,想要抓住它。农夫和牛都位于数轴上,农夫起始位于点N(0<=N<=100000),牛位于点K(0<=K<=100000)。农夫有两种移动方式:

1、从X移动到X-1或X+1,每次移动花费一分钟
2、从X移动到2*X,每次移动花费一分钟

假设牛没有意识到农夫的行动,站在原地不动。农夫最少要花多少时间才能抓住牛?

输入
两个整数,N和K
输出
一个整数,农夫抓到牛所要花费的最小分钟数
样例输入
5 17
样例输出
4

#include <bits/stdc++.h>
#define maxx 100000
using namespace std;
int n,k,vis[100010]={0}; 

int main(){
	cin>>n>>k;
	queue<int>man;
	queue<int>step;
	man.push(n);
	step.push(0);
	vis[n]=1;
	while(!man.empty()){
		if(man.front()==k){
			cout<<step.front()<<endl;
			return 0;
		}
		int a=man.front()+1;
		if(a<=maxx&&!vis[a]){
			man.push(a);
			step.push(step.front()+1);
			vis[a]=1;
		}
		int b=man.front()-1;
		if(b>=0&&!vis[b]){
			man.push(b);
			step.push(step.front()+1);
			vis[b]=1;
		}
		if(2*(man.front())<=maxx&&!vis[2*(man.front())]){
			man.push(2*(man.front()));
			step.push(step.front()+1);
			vis[2*(man.front())]=1;
		}
		man.pop();
		step.pop();
	}
    return 0;
}

H - Frog

题目
Problem Description
A little frog named Fog is on his way home. The path’s length is N (1 <= N <= 100), and there are many insects along the way. Suppose the
original coordinate of Fog is 0. Fog can stay still or jump forward T units, A <= T <= B. Fog will eat up all the insects wherever he stays, but he will
get tired after K jumps and can not jump any more. The number of insects (always less than 10000) in each position of the path is given.
How many insects can Fog eat at most?
Note that Fog can only jump within the range [0, N), and whenever he jumps, his coordinate increases.

Input
The input consists of several test cases.
The first line contains an integer T indicating the number of test cases.
For each test case:
The first line contains four integers N, A, B(1 <= A <= B <= N), K (K >= 1).
The next line contains N integers, describing the number of insects in each position of the path.

Output
each test case:
Output one line containing an integer - the maximal number of insects that Fog can eat.

Sample Input
1
4 1 2 2
1 2 3 4

Sample Output
8
注意这题青蛙起始位置应该也是有虫子吃的不然我怎么也想不到样例中它能吃到8个虫子fw想了好久才发现这个问题理解题目很重要吖
因为它叫我们找最优解所以第一时间就想bfs了。前面做的一题A - Tempter of the Bone这种一开始我也想用bfs,but一直wa 后面发现它不是叫我们找最优解!!!一定要注意细节!!!

#include <bits/stdc++.h>

using namespace std;
int t,n,a,b,k,ans,road[110];
int vis[500][500];//vis[i][j]=x 青蛙在i时走了j次吃了x只昆虫 (一个小优化,相同位置只有后来的比前者大才行)
typedef struct{
	int pos,eat,step;//位置,吃的量,步数
}node;
int main(){
	cin>>t;
	while(t--){
		ans=0;
		memset(vis,0,sizeof(vis));
		memset(road,0,sizeof(road));
		cin>>n>>a>>b>>k;
		for(int i=0;i<n;i++){
			cin>>road[i];
		}
		queue<node>flog;
		vis[0][0]=road[0];
		flog.push((node){0,road[0],0});
		while(!flog.empty()){
			node x=flog.front();flog.pop();//注意要及时pop!!!!不然无限循环会疯的 !!!jm!!!
			if(x.pos==(n-1)||x.step==k){
				ans=max(ans,x.eat );
				continue;
			}
			for(int i=a;i<=b;i++){
				int sp=x.pos+i;
				int se=x.eat+road[sp];
				int ss=x.step+1;
				if(se>vis[sp][ss]&&ss<=k&&sp<n){
					flog.push((node){sp,se,ss});
					vis[sp][ss]=se;
				}
			}
		}
		cout<<ans<<endl;
	}
    return 0;
}

P1141 01迷宫

题目
题目描述
有一个仅由数字0与1组成的n×n格迷宫。若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上。

你的任务是:对于给定的迷宫,询问从某一格开始能移动到多少个格子(包含自身)。

输入格式
第1行为两个正整数n,m。

下面n行,每行n个字符,字符只可能是0或者1,字符之间没有空格。

接下来m行,每行2个用空格分隔的正整数i,j对应了迷宫中第i行第j列的一个格子,询问从这一格开始能移动到多少格。

输出格式
m行,对于每个询问输出相应答案。

输入输出样例
输入
2 2
01
10
1 1
2 2
输出
4
4
说明/提示
所有格子互相可达。

对于20%的数据,n≤10;

对于40%的数据,n≤50;

对于50%的数据,m≤5;

对于60%的数据,n≤100,m≤100;

对于100%的数据,n≤1000,m≤100000。

注意chess[][]一定要弄成bool类型!!!!不然有个数据会RE!!!ex
RE数据,欢迎前来尝试

#include <bits/stdc++.h>
using namespace std;
int n,m,num=0,cnt=0;//把每一块联通区域都用统一的num标记,cnt记录区域一共有多大 
int to[4][2]={0,1,0,-1,1,0,-1,0};
int vis[1010][1010]={0},ans[100010];
bool chess[1010][1010];//这题好讨厌,如果这个是int类型的就RE 
queue<int>x;
queue<int>y;
void bfs(int a,int b){
	x.push(a);
	y.push(b);
	vis[a][b]=num;
	while(!x.empty()){
		for(int i=0;i<4;i++){
			int xx=x.front()+to[i][0];
			int yy=y.front()+to[i][1];
			if(xx>0&&yy>0&&xx<=n&&yy<=n&&chess[x.front()][y.front()]!=chess[xx][yy]&&!vis[xx][yy]){
				vis[xx][yy]=num;
				x.push(xx);
				y.push(yy);
			}
		}
		x.pop();
		y.pop();
		cnt++;
	}
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			char v;//这里小心它的输入之间是没有空格的!!!没有空格!!!没有空格!!! 
			cin>>v;
			if(v=='1') chess[i][j]=1;
			else chess[i][j]=0;
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(!vis[i][j]){
				num++; 
				bfs(i,j);
				ans[num]=cnt;//记录每块num相同的区域的大小 
				cnt=0;
			}
		}
	}
	for(int i=0;i<m;i++){
		int xx,yy;
		scanf("%d%d",&xx,&yy);
		cout<<ans[vis[xx][yy]]<<endl;
	}
    return 0;
}

6044:鸣人和佐助

题目
描述
佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?
已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置。地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸的手下才能到这些位置。鸣人有一定数量的查克拉,每一个单位的查克拉可以打败一个大蛇丸的手下。假设鸣人可以往上下左右四个方向移动,每移动一个距离需要花费1个单位时间,打败大蛇丸的手下不需要时间。如果鸣人查克拉消耗完了,则只可以走到没有大蛇丸手下的位置,不可以再移动到有大蛇丸手下的位置。佐助在此期间不移动,大蛇丸的手下也不移动。请问,鸣人要追上佐助最少需要花费多少时间?

输入
输入的第一行包含三个整数:M,N,T。代表M行N列的地图和鸣人初始的查克拉数量T。0 < M,N < 200,0 ≤ T < 10
后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。
输出
输出包含一个整数R,代表鸣人追上佐助最少需要花费的时间。如果鸣人无法追上佐助,则输出-1。
样例输入
样例输入1

4 4 1
#@##
**##
###+
****

样例输入2

4 4 2
#@##
**##
###+
****

样例输出
样例输出1
6

样例输出2
4

#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
int n,m,c;//M行N列的地图和鸣人初始的查克拉数量T
char mapp[210][210];
int vis[210][210];
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
typedef struct{
	int x;
	int y;
	int step;
	int t;
}node;
int main(){
	int ans=inf;
	queue<node>a;
	cin>>m>>n>>c;
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			cin>>mapp[i][j];
			vis[i][j]=-1;//初始化为-1这样当c为0的时候后面的步骤依旧可以运行 
			if(mapp[i][j]=='@'){
				a.push((node){i,j,0,c});
				vis[i][j]=c;
			}
		}
	}
	while(!a.empty()){
		node b=a.front();
		if(mapp[b.x][b.y]=='+'){
			ans=b.step ;
			break;
		}
		for(int i=0;i<4;i++){
			int xx=b.x+to[i][0];
			int yy=b.y+to[i][1];
			if(xx>=0&&yy>=0&&xx<m&&yy<n&&b.t >vis[xx][yy]){//先判断边界条件,b.t >vis[xx][yy]这一步是在找最优解(剪枝),如果 vis[xx][yy]大于b.t就没必要再接下去了因为vis[xx][yy]已经比b.t更优了 
				if(mapp[xx][yy]=='#'&&b.t >0){
					a.push((node){xx,yy,b.step+1,b.t-1});
					vis[xx][yy]=b.t-1;
				}
				else if(mapp[xx][yy]=='*'||mapp[xx][yy]=='+'){
					a.push((node){xx,yy,b.step+1,b.t});
					vis[xx][yy]=b.t;
				}
			}
		}
		a.pop();
	}
	if(ans!=inf) cout<<ans<<endl;
	else cout<<"-1"<<endl;
    return 0;
}

Dungeon Master(三维)

传送门
Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?
Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input

3 4 5
S…
.###.
.##…
###.#

##.##
##…

#.###
####E

1 3 3
S##
#E#

0 0 0
Sample Output

Escaped in 11 minute(s).
Trapped!
大致翻译:
你被困在一个三维地牢,需要找到最快的出路!地牢是由单位立方体组成的,这些立方体可以用石头填充,也可以不用石头填充。把一个单位向北、向南、东、西、向上或向下移动需要一分钟。你不能斜行,迷宫四周都是坚固的岩石。

有可能逃跑吗?如果是,需要多长时间?

输入

输入由许多地下城组成。每个地牢描述都以一行包含三个整数L、R和C(大小都限制在30以内)的行开始。

L是组成地牢的层数。

R和C是构成每层平面的行和列的数量。

然后,后面会有L个R行块,每个都包含C字符。每个角色描述一个地牢单元。充满岩石的单元格用“#”表示,空单元格用“.”表示。您的起始位置用“S”表示,出口用字母“E”表示。每一级后面都有一个空行。对于L、R和C,输入以三个零终止。

输出

每个迷宫产生一行输出。如果可以到达出口,打印一行表格

在x分钟内逃脱。

其中x被替换为逃逸所需的最短时间。

如果无法逃脱,请打印该行

被困!
注意:建立三维空间的输入操作和三维空间的上下前后左右的走向

//#include <bits/stdc++.h>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-6
const int ma=1e4+10;
typedef long long ll;
typedef unsigned long long ull; 
char a[50][50][50];
int vis[50][50][50];//记录走过的位置
int to[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
typedef struct node{
	int x;int y;int z;int step;
}node;
int l,r,c,el,er,ec,ans; 
int main()
{
	while(~scanf("%d%d%d",&l,&r,&c)&&r&&l&&c){
		queue<node>q;ans=0;//多组输入记得初始化
		memset(vis,0,sizeof(vis));
		for(int i=0;i<l;i++){
			for(int j=0;j<r;j++){
				scanf("%s",a[i][j]);
				for(int k=0;k<c;k++){
					if(a[i][j][k]=='S'){
						node n;
						n.x=i, n.y=j, n.z=k, n.step=1;//注意起始位置也算一次
						q.push(n);
						vis[i][j][k]=1;//我觉得这个有无问题不大
					}
					if(a[i][j][k]=='E'){
						el=i,er=j,ec=k;
					}
				}
			}
		}
		while(!q.empty()){
			node n=q.front() ;q.pop();
			int x,y,z;
			for(int i=0;i<6;i++){
				x=n.x+to[i][0] ;
				y=n.y+to[i][1] ;
				z=n.z+to[i][2] ;
				if(x==el&&y==er&&z==ec){
					ans=n.step ;//若找到则记录并结束循环
					break;
				}
				if(x>=0&&x<l&&y>=0&&y<r&&z>=0&&z<c&&!vis[x][y][z]&&a[x][y][z]=='.'){
					vis[x][y][z]=1;
					node m;
					m.x=x, m.y=y, m.z=z, m.step=n.step+1;
					q.push(m);
				}
			}
		}
		if(ans)printf("Escaped in %d minute(s).\n",ans);
		else printf("Trapped!\n");
	}
	return 0;
}

Jelly

LINK

题目:

在这里插入图片描述
和前面一样是一个三维

代码:


#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=1e2+10;
struct node{
	int x,y,z,step;
};
char a[N][N][N];int vis[N][N][N];
int to[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
queue<node>q;
int main() {
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			for(int k=1;k<=n;k++){
				cin>>a[i][j][k];
			}
		}
	}
	node x;x.x=1,x.y=1,x.z=1,x.step=1;
	q.push(x);int xx,yy,zz,ans=INT_MAX;
	if(n==1){
		cout<<0<<endl;
		return 0;
	}else if(a[n][n][n]!='.'){
		cout<<"-1"<<endl;return 0;
	}
	while(!q.empty()){
		x=q.front();q.pop();
		if(x.x==n&&x.y==n&&x.z==n){//(和上一题的区别)注意判断不要写在下面的for循环里!!!
			ans=x.step;
            break;
		}
		for(int i=0;i<6;i++){
			xx=x.x+to[i][0],yy=x.y+to[i][1];zz=x.z+to[i][2];
			if(xx>0&&xx<=n&&yy>0&&yy<=n&&zz>0&&zz<=n&&!vis[xx][yy][zz]&&a[xx][yy][zz]=='.'){
				vis[xx][yy][zz]=1;
				node y;y.x=xx,y.y=yy,y.z=zz,y.step=x.step+1;
				q.push(y);
			}
		}
	}
	if(ans!=INT_MAX)cout<<ans<<endl;
	else cout<<"-1"<<endl;
	return 0;
}



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值