HDU 2102 A 计划(bfs)

刚开始没注意到起点已经给定(0,0,0),还自己通过S去找到起点的坐标;

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn=10+2;
struct node{
	int x,y,z;
	node(int x=0,int y=0,int z=0):x(x),y(y),z(z){}
}ne;
char g[maxn][maxn][maxn];
int d[maxn][maxn][maxn];
int N,M,T;
int dir[][2]={{1,0},{-1,0},{0,-1},{0,1}};  //上下左右 
bool isValid(node &nd){
	return nd.x>=0&&nd.x<N && nd.y>=0&&nd.y<M;
}
void bfs(){
	memset(d,0,sizeof(d));
	queue<node>q;
	q.push(node(0,0,0));
	while(!q.empty()){
		node u=q.front();
		q.pop();
		if(u.x==ne.x && u.y==ne.y && u.z==ne.z){
			if(d[u.x][u.y][u.z]>T){
			printf("NO\n");return ; 
		}
			printf("YES\n");
			return ;
		}
		for(int i=0;i<4;i++){
				node v=node(dir[i][0]+u.x,dir[i][1]+u.y,u.z);
				if(isValid(v) && !d[v.x][v.y][v.z] && g[v.x][v.y][v.z]!='*')
				{
				if(g[v.x][v.y][v.z]!='#')q.push(v);        //如果是传输机不加入队列 
				d[v.x][v.y][v.z]=d[u.x][u.y][u.z]+1;
				
				if(g[v.x][v.y][v.z]=='#'){
				int t;
				if(u.z==0)t=1;
				else t=0;
				node v2=node(v.x,v.y,t);
				
				if(g[v2.x][v2.y][v2.z]!='*' && g[v2.x][v2.y][v2.z]!='#' && !d[v2.x][v2.y][v2.z]){
					q.push(v2);
					d[v2.x][v2.y][v2.z]=d[v.x][v.y][v.z];
				}
			}
		  }
		}
	}
	printf("NO\n");
}
int main(){
	int C;
	scanf("%d",&C);
	while(C--){
		scanf("%d%d%d",&N,&M,&T);
		getchar();
	for(int k=0;k<2;k++){
		for(int i=0;i<N;i++){
			for(int j=0;j<M;j++){
			scanf("%c",&g[i][j][k]);
			if(g[i][j][k]=='P')ne.x=i,ne.y=j,ne.z=k;
			}
			getchar();
		}
		if(k==0)getchar();
	}
		bfs();
	}
	return 0;
} 


在网上找了几组测试数据,可以试下:

6  
5 5 14
S*#*.
.#...
.....
****.
...#.


..*.P
#.*..
***..
...*.
*.#..
5 5 13
S*#*.
.#...
.....
****.
...#.


..*.P
#.*..
***..
...*.
*.#..
5 5 13
S*#*.
.#...
.....
****.
...#.


..*.P
#.*..
***..
...*.
*.##.
5 5 8
S*#*#
.#**.
.....
*****
...#.


..*.P
#.*..
***..
...*.
*.##.
1 4 10
.#.#


*.#P
1 4 2
.#.#


*.#P

格式自己再调下,复制上来有点不对

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏油

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值