poj 2251 Dungeon Master( bfs )
2024-10-19 02:27:15
题目:http://poj.org/problem?id=2251
简单三维 bfs不解释, 1A, 上代码
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<iomanip>
#include<cmath>
#include<map>
#include<vector>
#include<algorithm>
using namespace std; int a,b,c;
int vis[][][],G[][][];
int dx[]={,,,,-,};
int dy[]={,,,-,,};
int dz[]={,-,,,,};
struct node
{
int l,r,c,step;
}e,s,pos,next;
int bfs(int l,int r,int c)
{
int i;
queue<node>q;
next.l=l; next.r=r; next.c=c; next.step=;
vis[l][r][c]=;
q.push(next);
while(!q.empty())
{
pos=q.front();
q.pop();
for(i=; i<; i++)
{
next.l=pos.l+dx[i]; next.r=pos.r+dy[i];
next.c=pos.c+dz[i]; next.step=pos.step+;
if(!vis[next.l][next.r][next.c]&&G[next.l][next.r][next.c])
{
vis[next.l][next.r][next.c]=;
q.push(next);
}
if(next.l==s.l&&next.r==s.r&&next.c==s.c)
return next.step;
}
}
return -;
}
int main()
{
int i,j,k,x;
char ch;
while(cin>>a>>b>>c&&(a!=||b!=||c!=))
{
memset(G,,sizeof(G));
memset(vis,,sizeof(vis));
for(i=; i<=a; i++)
for(j=; j<=b; j++)
for(k=; k<=c; k++)
{
cin>>ch;
if(ch=='.')
G[i][j][k]=;
if(ch=='E')
{
e.l=i; e.r=j; e.c=k;
G[i][j][k]=;
}
if(ch=='S')
{
s.l=i; s.r=j; s.c=k;
G[i][j][k]=;
}
}
x=bfs(e.l,e.r,e.c);
if(x==-)
cout<<"Trapped!"<<endl;
else
printf("Escaped in %d minute(s).\n",x);
}
return ;
}
最新文章
- 安装cocoapods
- TODO:Golang Linux进程退出说明
- [python]初试页面抓取——抓取沪深股市交易龙虎榜数据
- tensorflow学习笔记五:mnist实例--卷积神经网络(CNN)
- activiti 里面各个方法理解
- 使用OCR来帮助LR实现认证码识别
- linq 多表分组查询统计
- 怎样创建FTP服务器
- 关于Control flow
- erlang 基础知识
- Linux学习之lsof命令
- SQL中的Update、delete与inner join 联合使用
- Swift的属性,方法,下标脚本以及继承
- Kettle 5.0源码编译
- JDK6和JDK7中String的substring()方法及其差异
- Spring中对象和属性的注入方式
- 我的Lambda的学习笔记
- MAC下 mySQL及workbench安装
- Tomcat 配置详解/优化方案(转)
- tf 版本更新 记录