zoukankan      html  css  js  c++  java
  • hdu-1072

    http://acm.hdu.edu.cn/showproblem.php?pid=1072

    Nightmare

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


    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

    1、不需要全部标记,因为走过的地方可能还会走

    2、注意炸弹时间的改变,到“4”的位置时间记为0

    3、注意入队的时间大于6的就没有意义了

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 using namespace std;
     6 int g[110][110], n, m;
     7 int d[4][2]={1,0,0,1,-1,0,0,-1};
     8 struct node{
     9     int x, y, z, time;
    10     bool is;
    11     bool operator < (const node &a) const{
    12         return z>a.z;
    13     }
    14 };
    15 bool ok(node a){
    16     return a.x>=0&&a.x<n&&a.y>=0&&a.y<m;
    17 }
    18 priority_queue<node> q;
    19 int main(){
    20     int T, i, j;
    21     node s, e;
    22     scanf("%d",&T);
    23     while(T--){
    24         while(!q.empty())
    25             q.pop();
    26         scanf("%d%d",&n,&m);
    27         for(i=0; i<n; i++){
    28             for(j=0; j<m; j++){
    29                 scanf("%d",&g[i][j]);
    30                 if(g[i][j]==2){
    31                     s.x = i;
    32                     s.y = j;
    33                     s.z = 0;
    34                     s.time = 0;
    35                     s.is = false;
    36                 }
    37                 else if(g[i][j]==3){
    38                     e.x = i;
    39                     e.y = j;
    40                 }
    41             }
    42         }
    43         q.push(s);
    44         bool flag = false;
    45         node t, b;
    46         while(!q.empty()){
    47             t=q.top();
    48             q.pop();
    49             //cout<<" "<<t.x<<" "<<t.y<<endl;
    50             if(t.x==e.x&&t.y==e.y&&t.time<6){
    51                 flag = true;
    52                 printf("%d
    ",t.z);
    53                 break;
    54             }
    55             for(i=0; i<4; i++){
    56                 b.x=t.x+d[i][0];
    57                 b.y=t.y+d[i][1];
    58                 if(ok(b)&&g[b.x][b.y]==4){
    59                      b.z = t.z+1;
    60                      if(t.time+1==6){
    61                         continue;
    62                      }
    63                      else{
    64                         b.time = 0;
    65                         t.is = true;
    66                         q.push(b);
    67                         g[t.x][t.y] = 1;
    68                         g[b.x][b.y] = 0;
    69                      }
    70                 }
    71                 else if(ok(b)&&(g[b.x][b.y]==1||g[b.x][b.y]==3)){
    72                     if(t.is){
    73                         b.z = t.z+1;
    74                         b.time=t.time+1;
    75                         if(b.time<6){
    76                             q.push(b);
    77                         }
    78                     }
    79                     //printf("%d %d
    ",b.x,b.y);
    80                     else{
    81                         b.z = t.z+1;
    82                         b.time=t.time+1;
    83                         if(b.time<6){
    84                             q.push(b);
    85                             g[b.x][b.y]=0;
    86                         }
    87                     }
    88                 }
    89             }
    90         }
    91         if(!flag)
    92             puts("-1");
    93     }
    94     return 0;
    95 }
     
  • 相关阅读:
    idea注释模板配置
    component-scan中base-package包含通配符
    mysql查询datetime大于下午3点的数据
    Linux c 开发-4 使用QT远程调试Linux程序
    Linux c 开发-3 配置ubuntu子系统桌面环境
    Linux c 开发-2 配置Vs2019
    Linux c 开发-1 Ubuntu子系统18.04开启SSH
    STM32 例程-5 Proteus使用串口2
    STM32 例程-4 Proteus下串口发送数据
    STM32 例程-3 Proteus下单按键试验
  • 原文地址:https://www.cnblogs.com/wudi-accept/p/5303367.html
Copyright © 2011-2022 走看看