hdoj1253
来源:互联网 发布:网络言情作家排行榜 编辑:程序博客网 时间:2024/06/14 11:27
一题简直模板的
BFS,只是三维遍历而已。
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string.h>
#include <math.h>
#include<stdlib.h>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
int A,B,C,t;
int dxy[7][3]=
{
0,-1,0,
0,1,0,
1,0,0,
-1,0,0,
0,0,1,
0,0,-1
};
int a[55][55][55];
int flag[55][55][55];
#include <iostream>
#include <sstream>
#include <string.h>
#include <math.h>
#include<stdlib.h>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
int A,B,C,t;
int dxy[7][3]=
{
0,-1,0,
0,1,0,
1,0,0,
-1,0,0,
0,0,1,
0,0,-1
};
int a[55][55][55];
int flag[55][55][55];
struct asd
{
int x,y,z;
int step;
} now,ne;
{
int x,y,z;
int step;
} now,ne;
queue<asd>q;
void bfs()
{
int i;
memset(flag,0,sizeof(flag));
while(!q.empty())
q.pop();
now.x=0;
now.y=0;
now.z=0;
now.step=0;
flag[0][0][0]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.step+1>t)
break;
for(i=0; i<6; i++)
{
int dx=now.x+dxy[i][0];
int dy=now.y+dxy[i][1];
int dz=now.z+dxy[i][2];
if(dx==A-1&&dy==B-1&&dz==C-1&&now.step+1<=t)
{
printf("%d\n",now.step+1);
return;
}
if(dx<0||dy<0||dz<0||dz>=C||dy>=B||dx>=A||flag[dx][dy][dz]||a[dx][dy][dz])
continue;
flag[dx][dy][dz]=1;
ne.x=dx;
ne.y=dy;
ne.z=dz;
ne.step=now.step+1;
q.push(ne);
}
}
printf("-1\n");
}
{
int i;
memset(flag,0,sizeof(flag));
while(!q.empty())
q.pop();
now.x=0;
now.y=0;
now.z=0;
now.step=0;
flag[0][0][0]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.step+1>t)
break;
for(i=0; i<6; i++)
{
int dx=now.x+dxy[i][0];
int dy=now.y+dxy[i][1];
int dz=now.z+dxy[i][2];
if(dx==A-1&&dy==B-1&&dz==C-1&&now.step+1<=t)
{
printf("%d\n",now.step+1);
return;
}
if(dx<0||dy<0||dz<0||dz>=C||dy>=B||dx>=A||flag[dx][dy][dz]||a[dx][dy][dz])
continue;
flag[dx][dy][dz]=1;
ne.x=dx;
ne.y=dy;
ne.z=dz;
ne.step=now.step+1;
q.push(ne);
}
}
printf("-1\n");
}
int main()
{
int i,j,k,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&A,&B,&C,&t);
for(i=0; i<A; i++)
{
for(j=0; j<B; j++)
{
for(k=0; k<C; k++)
scanf("%d",&a[i][j][k]);
}
}
bfs();
}
return 0;
}
{
int i,j,k,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d",&A,&B,&C,&t);
for(i=0; i<A; i++)
{
for(j=0; j<B; j++)
{
for(k=0; k<C; k++)
scanf("%d",&a[i][j][k]);
}
}
bfs();
}
return 0;
}
0 0
- hdoj1253
- hdoj1253
- HDOJ1253三维搜索
- hdoj1253 胜利大逃亡
- 简单bfs-hdoj1253
- HDOJ1253 胜利大逃亡 BFS
- 宽搜入门代码模板详解 HDOJ1253
- Activity知识梳理
- error : cannot open source file "SDKDDKVer.h"
- HTTP协议详解
- BFS Vs DFS (Level Order Traversal)
- Visual Studio常用快捷键
- hdoj1253
- Java虚拟机类加载机制浅谈
- CodeForces 626 D. Jerry's Protest(概率)
- FZU 2030 括号问题 (DFS)
- JVM 参数设置
- 算法导论—Boyer-Moore(BM)算法
- [易语言] 【源码】百宝云之注册/充值/开卡管理系统
- ElasticSearch 个人笔记-ElasticSearch 的安装
- 5个月iOS开发入门总结(C++转行iOS)