zoukankan      html  css  js  c++  java
  • hdu 1253:胜利大逃亡(基础广搜BFS)

    胜利大逃亡

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 23779    Accepted Submission(s): 9125


    Problem 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
     
    Author
    Ignatius.L
     
    Recommend
    Ignatius.L   |   We have carefully selected several similar problems for you:  1372 1242 1072 1240 1312 

     
      BFS广搜基础题。
      这道题挺有新意,把二维地图的搜索转变成了三维地图的搜索,不过换汤不换药,思路是一样的。直接拿过来BFS的模板,改动了一下,提交就过了。但是效率不高,1000+MS,有大神路过希望贴个更快的代码,感激不尽!^_^
      参考代码
     1 #include <iostream>
     2 #include <string.h>
     3 #include <queue> 
     4 using namespace std;
     5 int a[55][55][55];    //记录地图
     6 int isv[55][55][55];    //记录访问过没有 
     7 int dx[6] = {0,1,0,-1,0,0};
     8 int dy[6] = {1,0,-1,0,0,0}; 
     9 int dz[6] = {0,0,0,0,-1,1};
    10 int A,B,C,T;
    11 struct NODE{
    12     int x;
    13     int y;
    14     int z;
    15     int step;
    16 };
    17 bool judge(int x,int y,int z,int step)
    18 {
    19     if( x<1 || y<1 || z<1 || x>A || y>B || z>C )    //出界 
    20         return 1;
    21     if( isv[x][y][z] )    //走过 
    22         return 1;
    23     if( a[x][y][z]==1 )    //遇到墙 
    24         return 1;
    25     if(step+1>T)    //如果走这一步超过了时间,不能走
    26         return 1;
    27     return 0;
    28 }
    29 int bfs(int x,int y,int z)    //返回到达终点的时间 
    30 {
    31     memset(isv,0,sizeof(isv));
    32     queue <NODE> q;
    33     NODE cur,next;
    34     cur.x = x;
    35     cur.y = y;
    36     cur.z = z;
    37     cur.step = 0;
    38     isv[1][1][1] = true;
    39     q.push(cur);    //第一个节点入队 
    40     while(!q.empty()){
    41         cur = q.front();
    42         q.pop();    //队首出队
    43         if( cur.x==A && cur.y==B && cur.z==C){
    44             return cur.step;
    45         }
    46         for(int i=0;i<6;i++){
    47             int nx = cur.x + dx[i];
    48             int ny = cur.y + dy[i];
    49             int nz = cur.z + dz[i];
    50             if(judge(nx,ny,nz,cur.step))    //判定    
    51                 continue;
    52             //可以走
    53             next.x = nx;
    54             next.y = ny;
    55             next.z = nz;
    56             isv[nx][ny][nz] = true;    //记录访问过 
    57             next.step = cur.step + 1; 
    58             q.push(next);
    59         } 
    60     }
    61     return 0;
    62 }
    63 int main()
    64 {
    65     int n;
    66     cin>>n;
    67     while(n--){
    68         scanf("%d%d%d%d",&A,&B,&C,&T);
    69         int i,j,k;
    70         for(i=1;i<=A;i++)
    71             for(j=1;j<=B;j++)
    72                 for(k=1;k<=C;k++)
    73                     scanf("%d",&a[i][j][k]);
    74         int step = bfs(1,1,1);
    75         if(step)    //到达终点
    76             printf("%d
    ",step);
    77         else
    78             printf("-1
    ");
    79     }
    80     return 0;
    81 }
    Run ID Submit Time Judge Status Pro.ID Exe.Time Exe.Memory Code Len. Language Author
    10416745 2014-03-28 09:53:41 Accepted 1253 1140MS 1652K 1877 B C++ freecode

    Freecode : www.cnblogs.com/yym2013

  • 相关阅读:
    .NET CORE技术路线图
    .Net Core之Configuration
    30+程序员
    Source Insight无限试用期修改方法
    WALT(Window Assisted Load Tracking)学习
    使用Mac的Remote Desktop Manager连接ubuntu16.04 & Win10的远程桌面
    进程调度函数scheduler_tick()的触发原理:周期PERIODIC定时器
    Linux、Android系统调用从上层到底层的调用路径浅析
    Ftrace的部分使用方法
    CPU efficiency测量标准:DMIPS
  • 原文地址:https://www.cnblogs.com/yym2013/p/3630029.html
Copyright © 2011-2022 走看看