亡命逃窜 nyoj 523
2016-05-14 16:39
330 查看
bfs http://acm.nyist.net/JudgeOnline/problem.php?pid=523[code] #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
int k,a,b,c,t;
struct node{
int x,y,z;
int step;
node(){
step=0;
}
};
int p[51][51][51];
int vis[52][52][52];
int check(int x,int y,int z){
if(x>=a||y>=b||z>=c||x<0||y<0||z<0){
return 0;
}
if(p[x][y][z]==1){
return 0;
}
if(vis[x][y][z]==1){
return 0;
}
vis[x][y][z]=1;
return 1;
}
int bfs(){
vis[0][0][0]=1;
queue<node>q;
struct node t;
t.x=0;t.y=0;t.z=0;t.step=0;
q.push(t);
while(!q.empty()){
struct node temp=q.front();
q.pop();
int xx=temp.x;int yy=temp.y;int zz=temp.z;
if(xx==a-1&&yy==b-1&&zz==c-1){
return temp.step;
}
if(check(xx+1,yy,zz)){
t.x=xx+1;t.y=yy;t.z=zz;
t.step=temp.step+1;
q.push(t);
}
if(check(xx,yy+1,zz)){
t.x=xx;t.y=yy+1;t.z=zz;
t.step=temp.step+1;
q.push(t);
}
if(check(xx,yy,zz+1)){
t.x=xx;t.y=yy;t.z=zz+1;
t.step=temp.step+1;
q.push(t);
}
if(check(xx-1,yy,zz)){
t.x=xx-1;t.y=yy;t.z=zz;
t.step=temp.step+1;
q.push(t);
}
if(check(xx,yy-1,zz)){
t.x=xx;t.y=yy-1;t.z=zz;
t.step=temp.step+1;
q.push(t);
}
if(check(xx,yy,zz-1)){
t.x=xx;t.y=yy;t.z=zz-1;
t.step=temp.step+1;
q.push(t);
}
}
return -1;
}
int main(){
int temp;
scanf("%d",&k);
while(k--){
scanf("%d%d%d%d",&a,&b,&c,&t);
memset(vis,0,sizeof(vis));
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
for(int l=0;l<c;l++){
scanf("%d",&p[i][j][l]);
}
}
}
int flag=bfs();
if((flag>t)||(flag==-1)){
printf("-1\n");
}
else{
printf("%d\n",flag);
}
}
}
相关文章推荐
- 作业八 单元测试
- Android中的Canvas
- ExpandableListView
- Problem C: Mine Sweeping Game (华中农业预赛)
- js图片转base64编码压缩上传
- 虚拟地址,虚拟地址空间, 交换分区
- 系统参数的设计,通用方法提取,页面处理map字符串并显示系统参数(增强版)
- 线程
- Android中操作数据的集中方式---文件,SQLite,ContentProvider
- 常用效果集绵
- Ubuntu on Windows 10工作方式全解析
- PHP5升级到PHP7注意点(windows环境)
- 纯手工编写可执行程序
- LINUX 运维命令
- 练习: C#---函数(100以内7有关的数、99乘法表、100以内奇数和、硬币组合)
- 在WebLogic新建针对Oracle数据库的JNDI数据源
- 1003 Problem C
- UbuntuBSD 官方网站正式上线
- 抽象类
- Debug和Release区别