zoukankan      html  css  js  c++  java
  • hdu 1072 Nightmare(bfs)

    Nightmare

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 8683    Accepted Submission(s): 4172


    Problem 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 before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.

    Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.

    Here are some rules:
    1. We can assume the labyrinth is a 2 array.
    2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
    3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
    4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
    5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
    6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.
     
    Input
    The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
    Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
    There are five integers which indicate the different type of area in the labyrinth:
    0: The area is a wall, Ignatius should not walk on it.
    1: The area contains nothing, Ignatius can walk on it.
    2: Ignatius' start position, Ignatius starts his escape from this position.
    3: The exit of the labyrinth, Ignatius' target position.
    4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.
     
    Output
    For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.
     
    Sample Input
    3 3 3 2 1 1 1 1 0 1 1 3 4 8 2 1 1 0 1 1 1 0 1 0 4 1 1 0 4 1 1 0 0 0 0 0 0 1 1 1 1 4 1 1 1 3 5 8 1 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 4 1 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1
     
    Sample Output
    4 -1 13
     
    bfs
    存三维
    前两维空间维度,第三维是剩余时间...
    将同一位置不同的剩余时间看做不同的平面...之前做过一个类似的.
    还有就是.reset bomb的机器最多也只有n*m个
    那么最多访问 n*m次的(n*m) (实际肯定小于那么大)
    加一个步数控制...
    不然可能到TLE掉.
      1 /*************************************************************************
      2     > File Name: code/hdu/1072.cpp
      3     > Author: 111qqz
      4     > Email: rkz2013@126.com 
      5     > Created Time: 2015年10月03日 星期六 23时23分40秒
      6  ************************************************************************/
      7 
      8 #include<iostream>
      9 #include<iomanip>
     10 #include<cstdio>
     11 #include<algorithm>
     12 #include<cmath>
     13 #include<cstring>
     14 #include<string>
     15 #include<map>
     16 #include<set>
     17 #include<queue>
     18 #include<vector>
     19 #include<stack>
     20 #include<cctype>
     21                  
     22 #define yn hez111qqz
     23 #define j1 cute111qqz
     24 #define ms(a,x) memset(a,x,sizeof(a))
     25 using namespace std;
     26 const int dx4[4]={1,0,0,-1};
     27 const int dy4[4]={0,-1,1,0};
     28 typedef long long LL;
     29 typedef double DB;
     30 const int inf = 0x3f3f3f3f;
     31 const int N=10;
     32 int a[N][N];
     33 bool vis[N][N][N];
     34 int n,m;
     35 struct node{
     36     int x,y;
     37     int t;
     38     int d;
     39     bool ok()
     40     {
     41     if (x>=0&&y>=0&&x<n&&y<m&&!vis[x][y][t]&&a[x][y]>0&&t>0) return true;
     42     return false;
     43     }
     44 
     45     bool escape()
     46     {
     47     if (a[x][y]==3)
     48     return true;
     49     return false;
     50     }
     51     bool rbomb()
     52     {
     53     if (a[x][y]==4) return true;
     54     return false;
     55     }
     56     void mark()
     57     {
     58     vis[x][y][t] = true;
     59     }
     60 }s;
     61 
     62 bool bfs()
     63 {
     64     queue<node>q;
     65     q.push(s);
     66     int step = 0;
     67     while (!q.empty()&&step<n*m*n*m)
     68     {
     69     step++;
     70     node pre = q.front();q.pop();
     71 //    cout<<"x:"<<pre.x<<" y:"<<" a[x][y]:"<<a[pre.x][pre.y]<<" t:"<<pre.t<<" d:"<<pre.d<<endl;
     72     if (pre.escape())
     73     {
     74         printf("%d
    ",pre.d);
     75         return true;
     76     }
     77     
     78     for ( int  i = 0 ; i < 4 ; i++)
     79     {
     80         node next;
     81         next.x = pre.x + dx4[i];
     82         next.y = pre.y + dy4[i];
     83         next.d = pre.d + 1;
     84         next.t = pre.t - 1;
     85         if (!next.ok()) continue;
     86         if (next.rbomb())
     87         {
     88         next.t = 6;
     89         }
     90         next.mark();
     91         q.push(next);
     92 
     93 
     94     }
     95     }
     96     return false;
     97 
     98 }
     99 int main()
    100 {
    101   #ifndef  ONLINE_JUDGE 
    102    freopen("in.txt","r",stdin);
    103   #endif
    104 
    105    int T;
    106    scanf("%d",&T);
    107    while (T--)
    108     {
    109     ms(vis,false);
    110     scanf("%d %d",&n,&m);
    111     for ( int i = 0  ; i < n;  i++)
    112         for ( int j = 0 ; j < m ; j++)
    113         {
    114         scanf("%d",&a[i][j]);
    115         if (a[i][j]==2)
    116         {
    117             s.x = i;
    118             s.y = j;
    119             s.d = 0;
    120             s.t = 6;
    121             vis[s.x][s.y][s.t] = true;
    122         }
    123         }
    124     if(!bfs())
    125     {
    126         puts("-1");
    127     }
    128         
    129     }
    130   
    131    
    132  #ifndef ONLINE_JUDGE  
    133   fclose(stdin);
    134   #endif
    135     return 0;
    136 }
    View Code
     
  • 相关阅读:
    鲜牛奶与纯牛奶的区别 All In One
    Rough Notation Animation All In One
    java基础编程String及相关
    JDBC1
    java基础数据类型
    java基础编程
    JDBC3
    MYSQL1
    JDBC2
    JSP和Servlet的相同点和不同点,他们之间的联系
  • 原文地址:https://www.cnblogs.com/111qqz/p/4854757.html
Copyright © 2011-2022 走看看