NYOJ 353 3D dungeon 【bfs】

题意:给你一个高L长R宽C的图形,每一个坐标都可以视为一个方格,你一次可以向上,下,左,右,前,后任一方向移动一个方格, 但是不能向有#标记的方格移动。

问:从S出发能不能到达E,如果能请输出最少的移动次数。

策略:简单的深搜。

注意:因为是求最少的移动次数,所以要从所有能到达的中选出最少的。

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
char map[35][35][35];
int ans, l, r, c;
const int dirx[6] = {1, -1, 0, 0, 0, 0};
const int diry[6] = {0, 0, 1, -1, 0, 0};
const int dirz[6] = {0, 0, 0, 0, 1, -1};
struct node{
	int x, y, z;
	int step;
};
node st, en;
queue<node > q;
char s[10000];
bool vis[35][35][35];

int limit(node s){
	return (s.x<l&&s.x>=0&&s.y>=0&&s.y<r&&s.z>=0&&s.z<c&&map[s.x][s.y][s.z] != '#');
}

int match(node s){
	if(s.x==en.x&&s.y==en.y&&s.z==en.z) return 1;
	else return 0;
}
void bfs(){
	memset(vis, 0, sizeof(vis));
	ans = INF;  //初始化最大
	int i;
	q.push(st);
	//map[st.x][st.y][st.z] = '#';
	vis[st.x][st.y][st.z] = 1;
	while(!q.empty()){
		node temp = q.front();
		
		for(i = 0; i < 6; i ++){
			node temp2;
			temp2.x = temp.x+dirx[i];
			temp2.y = temp.y+diry[i];
			temp2.z = temp.z+dirz[i];
			temp2.step = temp.step+1;
			//printf("%d %d %d %d..", temp2.x, temp2.y, temp2.z, temp2.step);
			if(limit(temp2)&&!vis[temp2.x][temp2.y][temp2.z]){
				if(match(temp2)){
					ans = ans<temp2.step?ans:temp2.step;  //要bfs完全部的
				}
				else{
					q.push(temp2);
					vis[temp2.x][temp2.y][temp2.z] = 1;
				}
			}
		}
	//	if(ans) break;
		q.pop();
	}
	if(ans == INF) printf("Trapped!\n");
	else printf("Escaped in %d minute(s).\n", ans);
}
int main(){
	int i, j, k;
	while(scanf("%d%d%d", &l, &r, &c), l||r||c){
		memset(map, 0, sizeof(map));
		for(i = 0; i < l; i ++){
			for(j = 0; j < r; j ++){
				scanf("%s", map[i][j]);
				for(k = 0; k <c; k ++){
					if(map[i][j][k] == 'S'){
						st.x = i; st.y = j; st.z = k; st.step = 0;
					}
					if(map[i][j][k] == 'E'){
						en.x =i; en.y = j; en.z = k; en.step = 0;
					}
				}
			}
			gets(s);
		}
		bfs();
		
	}
	return 0;
}         


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值