hdoj 1072 Nightmare

Posted ZefengYao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdoj 1072 Nightmare相关的知识,希望对你有一定的参考价值。

Nightmare

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


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
 

 

Author
Ignatius.L
 
题意:从起点走到终点是否可能,中途编号为4的点可以重置炸弹时间为6min,所有的点是可以重复走的。
思路:起点,终点,以及编号为4的点都找出来,并一个一个进行bfs搜索,查找这些点的点与点之间距离,没有通路标记为INF,之后一次floyd即可
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<vector>
#include<set>
#include<algorithm>
#include<queue>
#include<string>
#include<cstring>
#include<map>
using namespace std;
#define N_MAX 100+2
#define INF 0x3f3f3f3f
typedef long long ll;
int n,m,Map[N_MAX][N_MAX],level[N_MAX][N_MAX],Time[N_MAX][N_MAX];
int d[N_MAX][N_MAX];
bool vis[N_MAX][N_MAX],flag=0;
int dir_x[4]={0,0,-1,1};
int dir_y[4]={-1,1,0,0};
vector<pair<int,int> >P;
void bfs(int xx,int yy){
  queue<pair<int,int> >que;
  que.push(make_pair(xx,yy));
 level[xx][yy]=0; Time[xx][yy]=6;vis[xx][yy]=true;
  while(!que.empty()){
    pair<int,int> p=que.front();que.pop();
    int x=p.first,y=p.second;
    if(Time[x][y]==0)continue;
    for(int i=0;i<4;i++){
        int X=x+dir_x[i],Y=y+dir_y[i];
        if(X>=0&&X<n&&Y>=0&&Y<m&&Map[X][Y]!=0&&!vis[X][Y]){
                vis[X][Y]=true;
            Time[X][Y]=Time[x][y]-1;
            if(Time[X][Y])level[X][Y]=level[x][y]+1;
            if(Map[X][Y]==4&&Time[X][Y])Time[X][Y]=6;
            que.push(make_pair(X,Y));
        }
    }
  }
}
void floyd(){
    int n=P.size();
for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
        for(int k=0;k<n;k++){
            d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
        }
    }
}
}

int main() {
    int t;scanf("%d",&t);
   while(t--){
       P.clear();
    memset(d,0,sizeof(d));
     scanf("%d%d",&n,&m);
     for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
        scanf("%d",&Map[i][j]);
        flag=0;int point,end_point;
        for(int i=0;i<n;i++){
         for(int j=0;j<m;j++){
            if(Map[i][j]==2){point=P.size(); P.push_back(make_pair(i,j));}
            if(Map[i][j]==3){end_point=P.size();P.push_back(make_pair(i,j));}
            if(Map[i][j]==4){P.push_back(make_pair(i,j));}
         }
        }
        for(int i=0;i<P.size();i++){
        memset(level,INF,sizeof(level));
        memset(Time,INF,sizeof(Time));
        memset(vis,0,sizeof(vis));
            int a=P[i].first,b=P[i].second;
            bfs(a,b);
            for(int j=0;j<P.size();j++){
                int c=P[j].first,D=P[j].second;
                d[i][j]=level[c][D];
            }
        }
        floyd();
        if(d[point][end_point]!=INF)printf("%d\n",d[point][end_point]);
        else puts("-1");
   }
    return 0;
}

 

以上是关于hdoj 1072 Nightmare的主要内容,如果未能解决你的问题,请参考以下文章

HDU-1072 Nightmare

hdu--1072--Nightmare(bfs回溯)

HDU 1072 [Nightmare] BFS

Nightmare HDU1072

HDOJ3085 Nightmare II 双向BFS

nodejs爬虫笔记---利用nightmare模拟点击下一页