hdu 1253 3维迷宫 在规定时间内能否出迷宫 (3维BFS)

题意:有一个人要在魔王回来之前逃出城堡。1表示墙,0表示路。魔王将在T分钟后回到城堡 起点可以是墙,但是人能走出。而终点也可以是墙,那自然就走不出了,但是要判断。

剪枝:如果终点是门或者从起点到终点的最短时间都大于t ,直接输出 -1。

Sample Input
1
3 3 4 20 //a b c T
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

 1 # include <cstdio>
 2 # include <cmath>
 3 # include <iostream>
 4 # include <cstring>
 5 # include <algorithm>
 6 # include <queue>
 7 using namespace std ;
 8
 9 int a, b, c, T;
10 int map[51][51][51] ;
11 int tur[6][3] = {-1, 0, 0, 1, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1} ;
12 struct node
13 {
14     int x,y,z,step;
15 };
16 void bfs()
17 {
18     queue<node> q ;
19     node begin={0,0,0,0};
20     map[0][0][0] = 1 ;
21     q.push(begin) ;
22     while(!q.empty())
23     {
24         node p = q.front() ;
25         q.pop() ;
26         for(int i=0; i<6; i++)
27         {
28             node temp = p ;
29             temp.x += tur[i][0] ;
30             temp.y += tur[i][1] ;
31             temp.z += tur[i][2] ;
32             if(temp.x==a-1&&temp.y==b-1&&temp.z==c-1&&temp.step<=T)
33             {
34                 printf("%d\n", temp.step+1) ;
35                 return ;
36             }
37
38             if(temp.x>=0&&temp.x<a&&temp.y>=0&&temp.y<b&&temp.z>=0&&temp.z<c&&!map[temp.x][temp.y][temp.z])
39             {
40                 map[temp.x][temp.y][temp.z] = 1 ;
41                 temp.step++ ;
42                 q.push(temp) ;
43             }
44         }
45     }
46     printf("-1\n") ;
47     return ;
48 }
49 int main()
50 {
51     int t ;
52     scanf("%d", &t) ;
53     while(t--){
54         scanf("%d%d%d%d", &a, &b, &c, &T) ;
55         for(int i=0; i<a; i++)
56             for(int j=0; j<b; j++)
57                 for(int k=0; k<c; k++)
58                     scanf("%d", &map[i][j][k]) ;
59        if(map[a-1][b-1][c-1]||a+b+c-3>T)
60         {
61             printf("-1\n") ;
62             continue ;
63         }
64         if(a==1&&b==1&&c==1)
65         {
66             printf("0\n") ;
67             continue ;
68         }
69         bfs() ;
70     }
71     return 0 ;
72 }

时间: 2024-08-06 03:40:05

hdu 1253 3维迷宫 在规定时间内能否出迷宫 (3维BFS)的相关文章

[ACM] hdu 1253 胜利大逃亡 (三维BFS)

胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出

HDU 1253 胜利大逃亡 NYOJ 523【BFS】

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24608    Accepted Submission(s): 9427 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

HDU 1253:胜利大逃亡(简单三维BFS)

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24937    Accepted Submission(s): 9535 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

HDU 1253 (简单三维广搜) 胜利大逃亡

奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz 而且我也优化过了的啊,尼玛还是一直爆! 先把代码贴上睡觉去了,明天再来弄 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 #include <cmath> 7 using namespace std; 8 9 struct Poin

HDU 1253 胜利大逃亡(BFS)

#include <iostream> #include <cstdlib> #include <cstdio> #include <queue> #include <cstring> using namespace std; struct node{ int x,y,z,step; }; int ma[51][51][51]; int A,B,C,T; int mv[6][3] = {{1,0,0},{0,1,0},{0,0,1},{-1,0,

hdu 1267 下沙的沙子有几粒?(二维递推题)

题意:就是给你m个H和n个D,然后从左开始数H的累积个数总是不比D的累计数少的排列有多少种举一个测试案例吧:3个H和1个D总共有3种排列,依次是:H D H H,H H D H,H H  H D三种排列,亲~意思应该懂了吧?!呵呵... 思路:递推公式为:a[m][n]=a[m-1][n]+a[m][n-1];然后当n=0的时候无论m取何值都是1,递推公式怎么推来的呢?我现在说下我的思路吧!假设3个H和2个D是由2个H和2个D还有3个H一个D推来的,2个H和2个D总共有H D H D,H H D

hdu 1253

#include<cstdio> #include<iostream> #include<queue> #include<cstring> using namespace std; int s[55][55][55]; int used[55][55][55]; int bzx[55]={1,-1,0,0,0,0}; int bzy[55]={0,0,1,-1,0,0}; int bzz[55]={0,0,0,0,1,-1}; int a,b,c,t,fla

hdu 1253 胜利大逃亡(简单题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目大意:在所给的时间能顺利离开城堡. 1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <cstring> 5 using namespace std; 6 int map[55][55][55],visit[55][55][55],a,b,c,T; 7 int

生成带内嵌图片的二维码

在博问上看到有同学在问如何实现一个带内嵌图片的二维码,所以准备记录下来,供同学们参考. 1.首先准备一个用于内嵌的图片. 2.既然生成二维码码,那肯定需要将什么样的内容生成二维码,这里我用http://www.baidu.com作为生成二维码的字符串 private string QcodeSource { get { return "http://www.baidu.com"; } } 3.我们来看看根据QcodeSource生成二维码的方法,这里返回Byte[].PS:这里用了 G