HDU - 1253 胜利大逃亡(bfs+queue的使用)
2017-12-07 21:24
399 查看
Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.
魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.
Input
输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块……),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙.(如果对输入描述不清楚,可以参考Sample Input中的迷宫描述,它表示的就是上图中的迷宫)
特别注意:本题的测试数据非常大,请使用scanf输入,我不能保证使用cin能不超时.在本OJ上请使用Visual C++提交.
Output
对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.
Sample Input
1
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0
Sample Output
11
解题思路:
剪枝:预判断当前时间加上剩余最短时间是否已经超过了限制时间t若超过则剪掉
PS:同样的代码不同的时间提交时间竟然会差很多= =
AC代码:
数组模拟队列:
以后还是多学习用STL的队列来实现吧
两种剪枝:
第一种直接判断当前时间时候超时
第二种预判当前时间与所剩最少时间的和是否超时
也可以这样写:
年轻人 多想一想 多想一想啊
2017.12.17更
(这样写岂不也是美滋滋~)
对照的就是这个
魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.
Input
输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块……),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙.(如果对输入描述不清楚,可以参考Sample Input中的迷宫描述,它表示的就是上图中的迷宫)
特别注意:本题的测试数据非常大,请使用scanf输入,我不能保证使用cin能不超时.在本OJ上请使用Visual C++提交.
Output
对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.
Sample Input
1
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0
Sample Output
11
解题思路:
剪枝:预判断当前时间加上剩余最短时间是否已经超过了限制时间t若超过则剪掉
PS:同样的代码不同的时间提交时间竟然会差很多= =
AC代码:
数组模拟队列:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int mp[55][55][55],book[55][55][55],a,b,c,t; int nx[]={1,-1,0,0,0,0}; int ny[]={0,0,1,-1,0,0}; int nz[]={0,0,0,0,1,-1}; struct node { int x,y,z,step; } Q[125010]; int bfs(int x,int y,int z,int step) { int head=1,tail=2,tx,ty,tz,tp; Q[head].x=x; Q[head].y=y; Q[head].z=z; Q[head].step=step; while(head<tail) { for(int i=0;i<6;i++) { tx=Q[head].x+nx[i]; ty=Q[head].y+ny[i]; tz=Q[head].z+nz[i]; tp=Q[head].step+1; if(tx>=0&&tx<a&&ty>=0&&ty<b&&tz>=0&&tz<c&&tp<=t&&mp[tx][ty][tz]==0&&book[tx][ty][tz]==0) { book[tx][ty][tz]=1; Q[tail].x=tx; Q[tail].y=ty; Q[tail].z=tz; Q[tail].step=tp; tail++; if(tx==a-1&&ty==b-1&&tz==c-1&&tp<=t) { return tp; } } } head++; } return -1; } int main() { int tt; scanf("%d",&tt); while(tt--) { scanf("%d%d%d%d",&a,&b,&c,&t); for(int i=0;i<a;i++) { for(int j=0;j<b;j++) { for(int k=0;k<c;k++) { scanf("%d",&mp[i][j][k]); } } } memset(book,0,sizeof(book)); book[0][0][0]=1; int sum=bfs(0,0,0,0); printf("%d\n",sum); } return 0; }
以后还是多学习用STL的队列来实现吧
两种剪枝:
第一种直接判断当前时间时候超时
#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; int mp[55][55][55],book[55][55][55],a,b,c,t; int nx[]={1,-1,0,0,0,0}; int ny[]={0,0,1,-1,0,0}; int nz[]={0,0,0,0,1,-1}; struct node { int x,y,z,step; }; node getnode(int x,int y,int z,int step) { node q; q.x=x; q.y=y; q.z=z; q.step=step; return q; } int bfs() { queue<node> q; q.push(getnode(0,0,0,0)); while(!q.empty()) { for(int i=0;i<6;i++) { int tx=q.front().x+nx[i]; int ty=q.front().y+ny[i]; int tz=q.front().z+nz[i]; if(tx>=0&&tx<a&&ty>=0&&ty<b&&tz>=0&&tz<c&&q.front().step+1<=t&&mp[tx][ty][tz]==0&&book[tx][ty][tz]==0) { book[tx][ty][tz]=1; q.push(getnode(tx,ty,tz,q.front().step+1)); if(tx==a-1&&ty==b-1&&tz==c-1&&q.front().step+1<=t) return q.back().step; } } q.pop(); } return -1; } int main() { int tt; scanf("%d",&tt); while(tt--) { scanf("%d%d%d%d",&a,&b,&c,&t); for(int i=0;i<a;i++) for(int j=0;j<b;j++) for(int k=0;k<c;k++) scanf("%d",&mp[i][j][k]); memset(book,0,sizeof(book)); book[0][0][0]=1; printf("%d\n",bfs()); } return 0; }
第二种预判当前时间与所剩最少时间的和是否超时
#include<stdio.h> #include<string.h> #include<queue> using namespace std; int mp[55][55][55],book[55][55][55],a,b,c,t; int nx[]={1,-1,0,0,0,0}; int ny[]={0,0,1,-1, f3eb 0,0}; int nz[]={0,0,0,0,1,-1}; struct node { int x,y,z,step; }; node getnode(int x,int y,int z,int step) { node q; q.x=x; q.y=y; q.z=z; q.step=step; return q; } int bfs() { queue<node> q; q.push(getnode(0,0,0,0)); while(!q.empty()) { for(int i=0;i<6;i++) { int tx=q.front().x+nx[i]; int ty=q.front().y+ny[i]; int tz=q.front().z+nz[i]; if(tx>=0&&tx<a&&ty>=0&&ty<b&&tz>=0&&tz<c&&a-1+b-1+c-1-tx-ty-tz+q.front().step+1<=t&&book[tx][ty][tz]==0&&mp[tx][ty][tz]==0) { book[tx][ty][tz]=1; q.push(getnode(tx,ty,tz,q.front().step+1)); if(tx==a-1&&ty==b-1&&tz==c-1) return q.back().step; } } q.pop(); } return -1; } int main() { int tt; scanf("%d",&tt); while(tt--) { scanf("%d%d%d%d",&a,&b,&c,&t); for(int i=0;i<a;i++) for(int j=0;j<b;j++) for(int k=0;k<c;k++) scanf("%d",&mp[i][j][k]); memset(book,0,sizeof(book)); book[0][0][0]=1; printf("%d\n",bfs()); } return 0; }
也可以这样写:
#include<stdio.h> #include<string.h> #include<queue> using namespace std; int mp[55][55][55],book[55][55][55],a,b,c,t; int nx[6][3]={0,0,1,0,1,0,1,0,0,0,0,-1,0,-1,0,-1,0,0}; struct node { int x,y,z,step; }; node getnode(int x,int y,int z,int step) { node q; q.x=x; q.y=y; q.z=z; q.step=step; return q; } int bfs() { queue<node> q; q.push(getnode(0,0,0,0)); while(!q.empty()) { for(int i=0;i<6;i++) { int tx=q.front().x+nx[i][0]; int ty=q.front().y+nx[i][1]; int tz=q.front().z+nx[i][2]; if(tx<0||tx>=a||ty<0||ty>=b||tz<0||tz>=c||book[tx][ty][tz]||mp[tx][ty][tz]) continue; if(a-1+b-1+c-1-tx-ty-tz+q.front().step+1>t) continue; book[tx][ty][tz]=1; q.push(getnode(tx,ty,tz,q.front().step+1)); if(tx==a-1&&ty==b-1&&tz==c-1) return q.back().step; } q.pop(); } return -1; } int main() { int tt; scanf("%d",&tt); while(tt--) { scanf("%d%d%d%d",&a,&b,&c,&t); for(int i=0;i<a;i++) for(int j=0;j<b;j++) for(int k=0;k<c;k++) scanf("%d",&mp[i][j][k]); memset(book,0,sizeof(book)); book[0][0][0]=1; printf("%d\n",bfs()); } return 0; }
年轻人 多想一想 多想一想啊
2017.12.17更
(这样写岂不也是美滋滋~)
#include<stdio.h> #include<queue> using namespace std; int mp[55][55][55],book[55][55][55],a,b,c,t; int nx[]={1,-1,0,0,0,0}; int ny[]={0,0,1,-1,0,0}; int nz[]={0,0,0,0,1,-1}; struct node { int x,y,z,step; } Q[125010]; node getnode(int x,int y,int z,int step) { node q; q.x=x; q.y=y; q.z=z; q.step=step; return q; } int bfs(int x,int y,int z,int step) { queue<node> q; q.push(getnode(x,y,z,step)); while(!q.empty()) { for(int i=0;i<6;i++) { int tx=q.front().x+nx[i]; int ty=q.front().y+ny[i]; int tz=q.front().z+nz[i]; int tp=q.front().step+1; if(tx>=0&&tx<a&&ty>=0&&ty<b&&tz>=0&&tz<c&&a-1+b-1+c-1-tx-ty-tz+step<=t&&book[tx][ty][tz]==0&&mp[tx][ty][tz]==0) { book[tx][ty][tz]=1; if(tx==a-1&&ty==b-1&&tz==c-1) return tp; q.push(getnode(tx,ty,tz,tp)); } } q.pop(); } return -1; } int main() { int tt; scanf("%d",&tt); while(tt--) { scanf("%d%d%d%d",&a,&b,&c,&t); for(int i=0;i<a;i++) { for(int j=0;j<b;j++) { for(int k=0;k<c;k++) { scanf("%d",&mp[i][j][k]); book[i][j][k]=0; } } } printf("%d\n",bfs(0,0,0,0)); } return 0; }
对照的就是这个
#include<stdio.h> int mp[55][55][55],book[55][55][55],a,b,c,t; int nx[]={1,-1,0,0,0,0}; int ny[]={0,0,1,-1,0,0}; int nz[]={0,0,0,0,1,-1}; struct node { int x,y,z,step; } Q[125010]; int bfs(int x,int y,int z,int step) { int tx,ty,tz,tp,head=1,tail=2; Q[head].x=x; Q[head].y=y; Q[head].z=z; Q[head].step=step; while(head<tail) { for(int i=0;i<6;i++) { tx=Q[head].x+nx[i]; ty=Q[head].y+ny[i]; tz=Q[head].z+nz[i]; tp=Q[head].step+1; if(tx>=0&&tx<a&&ty>=0&&ty<b&&tz>=0&&tz<c&&a-1+b-1+c-1-tx-ty-tz+step<=t&&book[tx][ty][tz]==0&&mp[tx][ty][tz]==0) { book[tx][ty][tz]=1; if(tx==a-1&&ty==b-1&&tz==c-1) return tp; Q[tail].x=tx; Q[tail].y=ty; Q[tail].z=tz; Q[tail].step=tp; tail++; } } head++; } return -1; } int main() { int tt; scanf("%d",&tt); while(tt--) { scanf("%d%d%d%d",&a,&b,&c,&t); for(int i=0;i<a;i++) { for(int j=0;j<b;j++) { for(int k=0;k<c;k++) { scanf("%d",&mp[i][j][k]); book[i][j][k]=0; } } } printf("%d\n",bfs(0,0,0,0)); } return 0; }
相关文章推荐
- hdu 1253胜利大逃亡 bfs
- HDU-1253 胜利大逃亡 (BFS)
- HDU 1253 胜利大逃亡( BFS )
- HDU_1253 胜利大逃亡(BFS)
- hdu 1253 胜利大逃亡(BFS)
- HDU 1253 胜利大逃亡 【BFS】
- HDU 胜利大逃亡 1253 BFS
- HDU 1253:胜利大逃亡(简单三维BFS)
- 胜利大逃亡 hdu 1253 && 亡命逃窜 (bfs)nyoj 523
- HDU-1253 胜利大逃亡 (BFS)
- HDU 1253 胜利大逃亡(三维数组+bfs)
- hdu 1253 胜利大逃亡(BFS)
- HDU 1253 胜利大逃亡(BFS)
- HDU1253 胜利大逃亡 BFS
- hdu 1253 胜利大逃亡 BFS
- HDU 1253 胜利大逃亡(BFS)
- hdu 1253:胜利大逃亡(基础广搜BFS)
- HDU 1253 (BFS) 胜利大逃亡
- HDU 1253 胜利大逃亡 (BFS,剪枝)
- HDU 1253 胜利大逃亡(简单三维BFS)