胜利大逃亡

 

 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 int map[51][51][51];
 5 int flag[51][51][51];
 6 int A,B,C,T;
 7 int dir[][3]={{0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0}};
 8 struct node
 9 {
10     int x,y,z;
11     int time;
12 };
13 void bfs()
14 {
15     int i;
16     flag[0][0][0]=1;
17     node p,temp;
18     p.time =p.x =p.y=p.z=0;
19     queue<node>q;
20     q.push(p);
21     while(!q.empty())
22     {
23         p=q.front();
24         q.pop();
25         for(i=0;i<6;i++)
26         {
27             temp=p;
28             temp.x+=dir[i][0];
29             temp.y+=dir[i][1];
30             temp.z+=dir[i][2];
31             temp.time=p.time+1;
32             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]==1)
33                 continue;
34             if(temp.x==A-1&&temp.y==B-1&&temp.z==C-1)
35             {
36                 if(temp.time>T)
37                     printf("-1\n");
38                 else
39                     printf("%d\n",temp.time);
40                 return;
41             }
42             if(map[temp.x][temp.y][temp.z]==0&&flag[temp.x][temp.y][temp.z]==0)
43             {
44                 q.push(temp);
45                 flag[temp.x][temp.y][temp.z]=1;
46             }
47         }
48     }
49     printf("-1\n");
50 }
51 int main()
52 {
53     int K;
54     scanf("%d",&K);
55     int i,j,k;
56     while(K--)
57     {
58         scanf("%d %d %d %d",&A,&B,&C,&T);
59         int canwalk=0;
60         memset(flag,0,sizeof(flag));
61         for(i=0;i<A;i++)
62         {
63             for(j=0;j<B;j++)
64             {
65                 for(k=0;k<C;k++)
66                 {
67                     scanf("%d",&map[i][j][k]);
68                     if(map[i][j][k]==0)
69                         canwalk++;
70                 }
71             }
72         }
73         if(A*B*C==1)
74             printf("0\n");
75         else if(canwalk<A+B+C+1)
76             printf("-1\n");
77         else
78             bfs();
79     }
80     return 0;
81 }


这是我第一个搜索题,当然也是第一个广搜题,刚开始的时候真不知道怎么开始,还好,有大家的帮忙,自己开始对队列有了一点点的熟悉,可是,自己懂得真的太少了,真的很需要努力啊!

posted @ 2012-06-23 23:22  欧阳生朵  阅读(167)  评论(0编辑  收藏  举报