HDU 1072 Nightmare (时间重置)BFS

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
 

[cpp]  view plain  copy
  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<queue>  
  4. #include<cstring>  
  5. using namespace std;  
  6. /* 
  7.  
  8.     1.到达出口时没有时间了,挂 
  9.     2.到达重置时间处没有时间了,挂 
  10.     3.时间重置装置可以多次使用 
  11.     4.总时间是6分钟,能不能走出迷宫?  
  12.      
  13.     0--墙 
  14.     1--路 
  15.     2--起点  
  16.     3--出口 
  17.     4--重置装置  
  18.      
  19.     关于重置装置可以多次使用,每个点可以多次走  
  20.     用数组br[]记录上次到达某点剩余的时间 
  21.     如果这次到达这个点能剩更多时间就走否则不走  
  22. */  
  23. const int N = 8;  
  24. const int M = 8;  
  25. char mp[N+2][M+2];  
  26. int n,m;  
  27. int br[N+2][M+2];  
  28. struct Node  
  29. {  
  30.     int x,y;  
  31.     int t;//剩下的时间   
  32.     int step;// 走了的步数   
  33. };  
  34. int dx[4] = {0,0,1,-1};  
  35. int dy[4] = {1,-1,0,0};  
  36. int bfs(int sx,int sy)  
  37. {  
  38.     memset(br,0,sizeof br);  
  39.     Node h;  
  40.     h.x = sx,h.y = sy,h.t = 6,h.step = 0;  
  41.     br[h.x][h.y] = 6;  
  42.     queue<Node>q;  
  43.     while (!q.empty()) q.pop();  
  44.     q.push(h);  
  45.     while (!q.empty())  
  46.     {  
  47.         h = q.front();  
  48.         q.pop();  
  49.         for (int i = 0;i < 4;++i)  
  50.         {  
  51.             Node nx;  
  52.             nx.t = h.t - 1;  
  53.             nx.x = h.x + dx[i];  
  54.             nx.y = h.y + dy[i];  
  55.             nx.step = h.step + 1;  
  56.             if (nx.x>=1&&nx.y>=1&&nx.x<=n&&nx.y<=m)  
  57.             {  
  58.                   
  59.                 if (mp[nx.x][nx.y] == 4)  
  60.                 {  
  61.                     if (6 > br[nx.x][nx.y])  
  62.                     {  
  63.                         nx.t = 6;  
  64.                         q.push(nx);   
  65.                         br[nx.x][nx.y] = 6;  
  66.                     }  
  67.                 }  
  68.                 else if (mp[nx.x][nx.y] == 1)  
  69.                 {  
  70.                     if (nx.t == 1) continue;// =-= 没机会了  下一步走到哪都是死  
  71.                     if (br[nx.x][nx.y] < nx.t)  
  72.                     {  
  73.                         q.push(nx);  
  74.                         br[nx.x][nx.y] = nx.t;  
  75.                     }  
  76.                 }  
  77.                 else if (mp[nx.x][nx.y] == 3)  
  78.                 {  
  79.                     return nx.step;  
  80.                 }  
  81.             }  
  82.         }  
  83.     }  
  84.     return -1;  
  85. }  
  86. int main()  
  87. {  
  88.     int T;  
  89.     scanf("%d",&T);  
  90.     while (T--)  
  91.     {  
  92.         int sx,sy;  
  93.         scanf("%d%d",&n,&m);  
  94.         for (int i = 1;i <= n;++i)  
  95.         {  
  96.             for (int j = 1;j <= m;++j)  
  97.             {  
  98.                 scanf("%d",&mp[i][j]);  
  99.                 if (mp[i][j] == 2)  
  100.                 {  
  101.                     sx = i,sy = j;  
  102.                 }  
  103.             }  
  104.         }  
  105.         cout<<bfs(sx,sy)<<endl;  
  106.     }  
  107.     return 0;  
  108. }  

你可能感兴趣的:(HDU 1072 Nightmare (时间重置)BFS)