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 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

此题重点是有些点可以重复走,但是重置时间点不需要从新走,如果走到该点tim小于以前的或者走到该点路程更短走该地可以重复走。

此题可以用BFS来写,下次补充BFS代码

 #include <iostream>
#include <cstdio>
#include <cstring>
#define size 10
#define Max 10000
using namespace std;
int dx[]={,,,-},dy[]={,-,,};
int map[size][size];
int ti[size][size];
int st[size][size];
int n,m;
int tim,len;
bool flag=;
int Minlen=Max;
int xs,ys;
bool flag0;
int nx,ny;
int dfs(int x,int y,int tim,int len)
{
int i,j;
if(map[x][y]==||tim<=||x<||x>=n||y<||y>=m||len>=Minlen)
return ;
if(map[x][y]==&&len<Minlen)
{
flag=;
Minlen=len;
return ;
}
if(map[x][y]==)
tim=;
if(tim>ti[x][y]||len<st[x][y])
{
ti[x][y]=tim;
st[x][y]=len;
for(i=;i<;i++)
{
nx=x+dx[i];
ny=y+dy[i];
dfs(nx,ny,tim-,len+);
}
}
return ;
}
int main()
{
int T;
int i,j;
freopen("in.txt","r",stdin);
cin>>T;
while(T--)
{
cin>>n>>m;
for(i=;i<n;i++)
for(j=;j<m;j++)
{
cin>>map[i][j];
if(map[i][j]==)
{
xs=i;
ys=j;
}
}
len=;
tim=;
Minlen=Max;
flag=;
memset(ti,,sizeof(ti));
for(i=;i<size;i++)
for(j=;j<size;j++)
st[i][j]=;
dfs(xs,ys,tim,len);
if(!flag) cout<<-<<endl;
else cout<<Minlen<<endl;
}
}

Nightmare(DFS)的更多相关文章

  1. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  2. LeetCode Subsets (DFS)

    题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...

  3. HDU 2553 N皇后问题(dfs)

    N皇后问题 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在 ...

  4. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  5. 【算法导论】图的深度优先搜索遍历(DFS)

    关于图的存储在上一篇文章中已经讲述,在这里不在赘述.下面我们介绍图的深度优先搜索遍历(DFS). 深度优先搜索遍历实在访问了顶点vi后,访问vi的一个邻接点vj:访问vj之后,又访问vj的一个邻接点, ...

  6. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  7. HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)

    HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  8. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  9. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

随机推荐

  1. File类详解

    一.File类: File类时io包中唯一代表磁盘文件本身的对象.File类定义了一些与平台无关的方法来操作文件,可以通过调用File类中的方法,实现创建.删除.重命名文件等. File类的对象主要用 ...

  2. ftp二进制与ascii传输方式区别

    ASCII 和BINARY模式区别:    用HTML 和文本编写的文件必须用ASCII模式上传,用BINARY模式上传会破坏文件,导致文件执行出错.    BINARY模式用来传送可执行文件,压缩文 ...

  3. BIT_COUNT()和BIT_OR()

    在学习MySQL手册时,看到根据天数计算访问量时,出现了BIT_COUNT()和BIT_OR()两个函数来处理天数计算的问题 所使用的表格信息如下: mysql> select year,mon ...

  4. WPF InkCanvas MouseDown及MouseLeftButtonDown事件不触发的代替事件

    PreviewMouseDown事件可以触发  再通过e.LeftButton 的状态判断是否按钮被按下 特此备忘

  5. 使用opencv传中文文件崩溃

    这个问题经过我的调试发现:   程序是在 while (*at && !isdigit(*at)) at++;   这个语句时crash的,但是跟进去是isdigit的问题,因为变量a ...

  6. mysql----用户root被删除或忘记root密码的解决方案

    修改文件my.cnf,可用VIM打开,如:sudo vim /etc/my.cnf 在[mysqld]下加上一行: skip-grant-tables 保存文件,然后重启mysqld程序:sudo s ...

  7. STL适配器的初步理解

    c++中的适配器有三种:容器适配器,迭代器适配器,函数适配器.下面一一介绍: 1.容器适配器:因为这些容器都是基于其他标准容器实现的所以叫做容器的适配器,具体的有stack,queue,priorit ...

  8. Find the Celebrity 解答

    Question Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there ma ...

  9. c++中可以对类中私有成员中的静态变量初始化吗?

    转载http://www.cnblogs.com/carbs/archive/2012/04/04/2431992.html 问题:我看的书上写的对私有部分的访问可以是公共部分的成员函数,也可以是友员 ...

  10. shell中eval命令妙用——变量嵌套替换

    eval命令妙用--变量嵌套替换 eval命令在Linux下的应用非常广泛,在写脚本的时候遇到一个变量嵌套的问题,用eval迎刃而解,略试不爽啊. var1="hello" i=1 ...