poj-2251 广搜

http://poj.org/problem?id=2251

#include<stdio.h>  
#include<iostream>  
#include<math.h>  
#include<stdlib.h>  
#include<ctype.h>  
#include<algorithm>  
#include<vector>  
#include<string.h>  
#include<queue>  
#include<stack>  
#include<set>   
#include<sstream>  
#include<time.h>  
#include<utility>  
#include<malloc.h>  
#include<stdexcept>  
#include<iomanip>  
#include<iterator>  

using namespace std;

char map[40][40][40];
int vis[40][40][40];
int L, R, C;

struct node
{
	int z, x, y;
	int c;
};

int dir[6][3] = { { 1, 0, 0 }, { -1, 0, 0 }, { 0, -1, 0 }, { 0, 1, 0 }, { 0, 0, -1 }, { 0, 0, 1 } };

int BFS(int si, int sj, int sk)
{
	memset(vis, 0, sizeof(vis));

	queue<node> q;
	node cur, next;

	cur.z = si, cur.x = sj, cur.y = sk, cur.c = 0;

	vis[si][sj][sk] = 1;
	
	q.push(cur);

	while (!q.empty())
	{
		cur = q.front();
		q.pop();
		for (int i = 0; i < 6; i++)
		{
			next.z = cur.z + dir[i][0];
			next.x = cur.x + dir[i][1];
			next.y = cur.y + dir[i][2];
			next.c = cur.c + 1;
			if (next.z<1 || next.z>L || next.x<1 || next.x>R || next.y<1 || next.y>C || map[next.z][next.x][next.y] == '#')
				continue;
			if (map[next.z][next.x][next.y] == 'E')
				return next.c;
			if (!vis[next.z][next.x][next.y])
			{
				vis[next.z][next.x][next.y] = 1;
				q.push(next);
			}
		}
	}
	return 0;
}

int main()
{
	int si, sj, sk;
	while (cin >> L >> R >> C)
	{
		if (L == 0 && R == 0 && C == 0)
			break;

		for (int i = 1; i <= L; i++)
			for (int j = 1; j <= R; j++)
				for (int k = 1; k <= C; k++)
				{
					cin >> map[i][j][k];
					if (map[i][j][k] == 'S')
					{
						si = i; sj = j; sk = k;
					}
				}
		int ans = BFS(si, sj, sk);
		if (ans == 0)
			printf("Trapped!\n");
		else
			printf("Escaped in %d minute(s).\n", ans);

	}
	return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值