Nightmare

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
using namespace std;
#define INF 1<<30
int t,n,m,a[10][10],sx,sy;
int step[10][10],T[10][10],minn;
int dic[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool check(int x,int y)
{
      if(x<0||x>=n||y<0||y>=m||a[x][y]==0)
            return false;
      return  true;
}
void dfs(int x,int y)
{
     if(T[x][y]<=0)
            return ;
     if(a[x][y]==3)
     {
           if(minn>step[x][y])
                  minn=step[x][y];
           return ;
     }
     for(int i=0;i<4;i++)
     {
           int xx,yy;
           xx=x+dic[i][0],yy=y+dic[i][1];
           if(check(xx,yy))
           {
                 if((step[xx][yy]<=step[x][y]+1)&&(T[xx][yy]>=T[x][y]-1))
                        continue;
                 step[xx][yy]=step[x][y]+1,T[xx][yy]=T[x][y]-1;
                 if(a[xx][yy]==4&&T[xx][yy]>0)
                 {
                       T[xx][yy]=6;
                 }
                 dfs(xx,yy);
           }
     }
}
int main()
{
      scanf("%d",&t);
      while(t--)
      {
            minn=INF;
            memset(T,0,sizeof(T));
            scanf("%d%d",&n,&m);
            for(int i=0;i<n;i++)
                  for(int j=0;j<m;j++)
                  {
                      scanf("%d",&a[i][j]);
                      if(a[i][j]==2)
                        sx=i,sy=j;
                  }
            for(int i=0;i<n;i++)
                  for(int j=0;j<m;j++)
                        step[i][j]=INF;
            step[sx][sy]=0;
            T[sx][sy]=6;
            dfs(sx,sy);
            if(minn!=INF)
                  printf("%d\n",minn);
            else
                  printf("-1\n");
      }
      return 0;
}

  

时间: 2025-01-31 06:40:15

Nightmare的相关文章

Nightmare(DFS)

Nightmare    hdu1072 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8871    Accepted Submission(s): 4270 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth

hdu 1072 Nightmare BFS,第一次刷BFS的题,感好牛逼的。。。

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7758    Accepted Submission(s): 3723 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ti

HDU 1072 (不一样的入队条件) Nightmare

之前的BFS都是需要一个标记数组,但这个题不一样,因为可能一个格子不止走一次. 那么我们就要寻找新的入队条件:left比上次经过的时候大才入队(left表示上次经过该点时剩余的时间). 为什么呢?我们重复走过一个点只有一个可能,那就是为了去取那个,所以如果取完后再回头经过这个点的时候剩余时间变多了,我们的目的就达到了. left数组初值为0 优化: 重置时间的装置最多取一次就够了,所以可以再开一个标记数组vis记录装置是否用过. 1 //#define LOCAL 2 #include <cst

POJ 1984 Navigation Nightmare 二维带权并查集

题目来源:POJ 1984 Navigation Nightmare 题意:给你一颗树 k次询问 求2点之间的曼哈顿距离 并且要在只有开始k条边的情况下 思路:按照方向 我是以左上角为根 左上角为原点 dx[i]为i点距离根的x坐标 dy[]是y坐标 这两个可以通过路径压缩求出 只不过是二维而已 #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int m

爬虫的终极形态:nightmare

爬虫的终极形态:nightmare nightmare 是一个基于 electron 的自动化库(意思是说它自带浏览器),用于实现爬虫或自动化测试.相较于传统的爬虫框架(scrapy/pyspider),或者dom操作库(cheerio/jsdom),或者基于浏览器的自动化框架(selenium/phantomjs),他的优势在于提供了一个简洁有效 的编程模型. 来看官网给出的一个对比场景: 同样是实现一个向yahoo自动提交关键词并搜索的功能 1. PhantomJS实现 PhantomJS实

nyoj 483 Nightmare【bfs+优先队列】

Nightmare 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 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 e

POJ 1984 Navigation Nightmare (数据结构-并查集)

Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4072   Accepted: 1615 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series o

HDU 1072 Nightmare(BFS)

Nightmare Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9120    Accepted Submission(s): 4389 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ti

[Node.js] Scraping Dynamic JavaScript Websites with Nightmare

Many websites have more than just simple static content. Dynamic content which is rendered by JavaScript requires browser to be able to scrape data. This video demonstrates how to use Nightmare (which is a wrapper around PhantomJS) to launch a url an