CDZSC_2015寒假新人(4)——搜索 G

Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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

相对比较简单的三围BFS搜索

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 using namespace std;
 6 int xx[]={1,-1,0,0,0,0};
 7 int yy[]={0,0,1,-1,0,0};
 8 int zz[]={0,0,0,0,1,-1};
 9 int p[55][55][55],vis[55][55][55],a,b,c;
10 struct node
11 {
12     int x;
13     int y;
14     int z;
15 };
16 int bfs(node s,node f)
17 {
18     memset(vis,0,sizeof(vis));
19     node cmp,tmp;
20     queue<node>q;
21     q.push(s);
22     while(!q.empty())
23     {
24         cmp=q.front();
25         q.pop();
26         for(int i=0; i<6; i++)
27         {
28             if(vis[cmp.x+xx[i]][cmp.y+yy[i]][cmp.z+zz[i]]==0&&cmp.x+xx[i]>=0&&cmp.x+xx[i]<a&&cmp.y+yy[i]>=0&&cmp.y+yy[i]<b&&cmp.z+zz[i]>=0&&cmp.z+zz[i]<c&&p[cmp.x+xx[i]][cmp.y+yy[i]][cmp.z+zz[i]]!=1)
29             {
30                 vis[cmp.x+xx[i]][cmp.y+yy[i]][cmp.z+zz[i]]=vis[cmp.x][cmp.y][cmp.z]+1;
31                 tmp.x=cmp.x+xx[i],tmp.y=cmp.y+yy[i],tmp.z=cmp.z+zz[i];
32                 q.push(tmp);
33             }
34         }
35     }
36     if(vis[f.x][f.y][f.z]!=0)
37     {
38         return vis[f.x][f.y][f.z];
39     }
40     else
41     {
42         return -1;
43     }
44 }
45 int main()
46 {
47 #ifdef CDZSC_OFFLINE
48     freopen("in.txt","r",stdin);
49 #endif
50     int kk,t,i,j,k,sum_time;
51     node s,f;
52     scanf("%d",&kk);
53     while(kk--)
54     {
55         scanf("%d%d%d%d",&a,&b,&c,&t);
56         for(i=0; i<a; i++)
57         {
58             for(j=0; j<b; j++)
59             {
60                 for(k=0; k<c; k++)
61                 {
62                     scanf("%d",&p[i][j][k]);
63                 }
64             }
65         }
66         s.x=0,s.y=0,s.z=0;
67         f.x=a-1,f.y=b-1,f.z=c-1;
68         sum_time=bfs(s,f);
69         if(sum_time>t||sum_time==-1)
70         {
71             printf("-1\n");
72         }
73         else
74         {
75             printf("%d\n",sum_time);
76         }
77     }
78     return 0;
79 }
80
81         
时间: 2024-10-27 03:01:23

CDZSC_2015寒假新人(4)——搜索 G的相关文章

CDZSC_2015寒假新人(4)——搜索 A

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and

CDZSC_2015寒假新人(4)——搜索 L

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description A message from humans to extraterrestrial intelligence was sent through the Arecibo radio telescope in Puerto Rico on the afternoon of Saturday November 16, 1974

CDZSC_2015寒假新人(4)——搜索 - P

Description  Parentheses Balance  You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a) if it is the empty string (b) if A and B are correct, AB is correct, (c) if A is correct, (A ) and [A ] is

CDZSC_2015寒假新人(2)——数学 - G

Description HOHO,终于从Speakless手上赢走了所有的糖果,是Gardon吃糖果时有个特殊的癖好,就是不喜欢将一样的糖果放在一起吃,喜欢先吃一种,下一次吃另一种,这样:可是Gardon不知道是否存在一种吃糖果的顺序使得他能把所有糖果都吃完?请你写个程序帮忙计算一下. Input 第一行有一个整数T,接下来T组数据,每组数据占2行,第一行是一个整数N(0<N<=1000000),第二行是N个数,表示N种糖果的数目Mi(0<Mi<=1000000). Output

CDZSC_2015寒假新人(4)——搜索 N

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Description Standard web browsers contain features to move backward and forward among the pages recently visited. One way to implement these features is to use two stacks to

CDZSC_2015寒假新人(4)——搜索 C

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description There is a strange lift.The lift can stop can at every floor as you want, and there is a number Ki(0 <= Ki <= N) on every floor.The lift have just two butto

CDZSC_2015寒假新人(4)——搜索 B

Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only

CDZSC_2015寒假新人(4)——搜索 - D

Description You're in space. You want to get home. There are asteroids. You don't want to hit them. Input Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following de

CDZSC_2015寒假新人(4)——搜索 F

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth