zoukankan      html  css  js  c++  java
  • HDU 1253.胜利大逃亡

    胜利大逃亡
    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,思路上比较明确,类似BFS从一维拓展成二维,将BFS从二维拓展成三维即可
     
    不过该题数据量较大,需要进行各种优化
     
    当达到时间限制后,就不必再进行BFS了,应该直接输出-1
    当找到答案后,就不必进行BFS了,应该直接输出答案
     
    不过第一次尽管这样处理还是TLE,于是又优化了下读入
     1 int read_int() {
     2     char c;
     3     int ans = 0;
     4     while(c = getchar(),!(c >= '0' && c <= '9'));
     5     while(c >= '0' && c <= '9') {
     6         ans *= 10;
     7         ans += (int)c - '0';
     8         c = getchar();
     9     }
    10     return ans;
    11 }

    不过第一次写的有点小问题,导致WA。

    看了下问题出在优化输入上,修改了下后AC

      1 /*
      2 By:OhYee
      3 Github:OhYee
      4 Email:oyohyee@oyohyee.com
      5 Blog:http://www.cnblogs.com/ohyee/
      6 
      7 かしこいかわいい?
      8 エリーチカ!
      9 要写出来Хорошо的代码哦~
     10 */
     11 
     12 #include <cstdio>
     13 #include <algorithm>
     14 #include <cstring>
     15 #include <cmath>
     16 #include <string>
     17 #include <iostream>
     18 #include <vector>
     19 #include <list>
     20 #include <queue>
     21 #include <stack>
     22 #include <map>
     23 using namespace std;
     24 
     25 //DEBUG MODE
     26 #define debug 0
     27 
     28 //循环
     29 #define REP(n) for(int o=0;o<n;o++)
     30 
     31 const int maxn = 55;
     32 int a,b,c;
     33 int t;
     34 int dis[maxn][maxn][maxn];
     35 int Map[maxn][maxn][maxn];
     36 
     37 const int delta[6][3] = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
     38 
     39 struct point {
     40     int x,y,z;
     41     point() {
     42         x = y = z = -1;
     43     }
     44     point(int a,int b,int c) {
     45         x = a;
     46         y = b;
     47         z = c;
     48     }
     49 };
     50 
     51 //优化输入
     52 int read_int() {
     53     char c;
     54     int ans = 0;
     55     while(c = getchar(),!(c >= '0' && c <= '9'));
     56     while(c >= '0' && c <= '9') {
     57         ans *= 10;
     58         ans += (int)c - '0';
     59         c = getchar();
     60     }
     61     return ans;
     62 }
     63 
     64 
     65 int BFS(point s,point v) {
     66     memset(dis,-1,sizeof(dis));
     67     queue<point> Q;
     68 
     69     Q.push(s);
     70     dis[s.x][s.y][s.z] = 0;
     71 
     72     while(!Q.empty()) {
     73         int x = Q.front().x;
     74         int y = Q.front().y;
     75         int z = Q.front().z;
     76         Q.pop();
     77 
     78         //时间已到
     79         if(dis[x][y][z] >= t)
     80             return -1;
     81 
     82         REP(6) {
     83             int xx = x + delta[o][0];
     84             int yy = y + delta[o][1];
     85             int zz = z + delta[o][2];
     86 
     87             //非法路径
     88             if(xx < 0 || xx >= a || yy < 0 || yy >= b || zz < 0 || zz >= c)
     89                 continue;
     90             //
     91             if(Map[xx][yy][zz] == 1)
     92                 continue;
     93 
     94             //尚未访问过
     95             if(dis[xx][yy][zz] == -1) {
     96                 dis[xx][yy][zz] = dis[x][y][z] + 1;
     97                 //到达终点
     98                 if(xx == v.x && yy == v.y && zz == v.z)
     99                     return dis[xx][yy][zz];
    100                 Q.push(point(xx,yy,zz));
    101             }
    102         }
    103     }
    104     return -1;
    105 }
    106 
    107 void Do() {
    108     //scanf("%d%d%d%d",&a,&b,&c,&t);
    109     a = read_int();
    110     b = read_int();
    111     c = read_int();
    112     t = read_int();
    113 
    114 
    115     for(int k = 0;k < a;k++)//
    116         for(int i = 0;i < b;i++)//
    117             for(int j = 0;j < c;j++)//118                 //scanf("
    %c
    ",&Map[k][i][j]);
    119                 Map[k][i][j] = read_int();
    120 
    121     #if debug
    122     for(int k = 0;k < a;k++) {
    123         for(int i = 0;i < b;i++) {
    124             for(int j = 0;j < c;j++)
    125                 printf("%d",Map[k][i][j]);
    126             printf("
    ");
    127         }
    128         printf("
    ");
    129     }
    130     #endif
    131 
    132     if(a*b*c == 1)
    133         printf("0
    ");
    134     else
    135         printf("%d
    ",BFS(point(0,0,0),point(a - 1,b - 1,c - 1)));
    136 }
    137 
    138 int main() {
    139     int T;
    140     T = read_int();
    141     //scanf("%d",&T);
    142     while(T--)
    143         Do();
    144     return 0;
    145 }
  • 相关阅读:
    常用图书下载
    模式另类说明
    windows进程中的内存结构
    Windows API学习手记
    20060318工作记录
    X3全局变量及公共函数所在的命名空间说明
    PHP 后台定时循环刷新某个页面 屏蔽apache意外停止
    php随机生成指定长度的字符串 可以固定数字 字母 混合
    以div代替frameset,用css实现仿框架布局
    核中汇编写的字符串函数代码分析
  • 原文地址:https://www.cnblogs.com/ohyee/p/5421606.html
Copyright © 2011-2022 走看看