zoukankan      html  css  js  c++  java
  • HDU1253 胜利大逃亡 (BFS)

     

    胜利大逃亡

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


    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

    思路:bfs三维模板题。存图之后,bfs(0,0,0), 然后就走能到达的点,标记,步数+1,然后判断即可。

    比赛的时候怎么也写不出来,貌似边界弄错了(一直以为是存图的原因),后来写的时候还是不对!! check函数里面if有分号。。。。

    通过题目发现了短板,结构体构造函数也忘了。。

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <math.h>
     4 #include <string.h>
     5 #include <stdlib.h>
     6 #include <string>
     7 #include <vector>
     8 #include <set>
     9 #include <map>
    10 #include <queue>
    11 #include <algorithm>
    12 #include <sstream>
    13 #include <stack>
    14 using namespace std;
    15 #define mem(a,b) memset((a),(b),sizeof(a))
    16 #define mp make_pair
    17 #define pb push_back
    18 #define fi first
    19 #define se second
    20 #define sz(x) (int)x.size()
    21 #define all(x) x.begin(),x.end()
    22 typedef long long ll;
    23 const int inf = 0x3f3f3f3f;
    24 const ll INF =0x3f3f3f3f3f3f3f3f;
    25 const double pi = acos(-1.0);
    26 const double eps = 1e-5;
    27 const ll mod = 1e9+7;
    28 //head
    29 int a[60][60][60];
    30 bool vis[60][60][60];
    31 int des[6][3] = {0, 1, 0, 1, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, -1, 0, 0, 1};//6个方向
    32 
    33 struct node{
    34     int x, y, z;
    35     int step;
    36     node(){}
    37     node(int _x, int _y, int _z, int _step):x(_x), y(_y), z(_z), step(_step){}
    38 }temptop, f;
    39 int _, n, m, l, t;
    40 
    41 bool check(int x, int y, int z) {//该点是否满足
    42     if(x < 0 || x >= n || y < 0 || y >= m || z < 0 || z >= l)
    43         return false;
    44     if(vis[x][y][z] || a[x][y][z])
    45         return false;
    46     return true;
    47 }
    48 
    49 int bfs(int x, int y, int z) {
    50     queue<node> q;
    51     q.push(node(x, y, z, 0));//起点入队
    52     vis[x][y][z] = true;
    53     while(!q.empty()) {
    54         temptop = q.front();
    55         q.pop();
    56         if(temptop.x == n-1 && temptop.y == m-1 && temptop.z == l-1)//出口
    57             return temptop.step;
    58         for(int i = 0; i < 6; i++) {//bfs
    59             int newx = temptop.x + des[i][0];
    60             int newy = temptop.y + des[i][1];
    61             int newz = temptop.z + des[i][2];
    62             if(check(newx, newy, newz)) {//可以达到就步数+1,标记,入队
    63                 vis[newx][newy][newz] = true;
    64                 q.push(node(newx, newy, newz, temptop.step + 1));
    65             }
    66         }
    67     }
    68     return -1;//不能达到
    69 }
    70 
    71 int main() {
    72     for(scanf("%d", &_);_;_--) {
    73         mem(vis, false);
    74         scanf("%d%d%d%d", &l, &n, &m ,&t);
    75         for(int k = 0; k < l; k++) {
    76             for(int i = 0; i < n; i++) {
    77                 for(int j = 0; j < m; j++)
    78                     scanf("%d", &a[i][j][k]);
    79             }
    80         }
    81         int ans = bfs(0, 0, 0);//起点
    82         if(ans > t || ans == -1)//不满足的条件
    83             printf("-1
    ");
    84         else
    85             printf("%d
    ", ans);
    86     }
    87 }
  • 相关阅读:
    当一个模块没有默认导出
    <<平仓>>
    模态对话框
    PlanB S2F 模型
    <<深入React技术栈>> CSS Modules
    状态提升
    ol.proj.transform 坐标系转换
    HTMLVideoElement.srcObject MediaStream MediaSource Blob File
    毛玻璃特效 backdrop-filter
    Filter
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9572922.html
Copyright © 2011-2022 走看看