HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏
Nightmare
Problem Description
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
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
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<cmath>
#include<cstring>;
#include<queue>
using namespace std;
int mp[10][10];
int vir[10][10];
int m,n,bt,escape;
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; struct node
{
int x;
int y;
int bt;
int cnt;
}; bool cheak(int i,int j)
{
if(i<0||i>=m||j<0||j>=n||mp[i][j]==0)
return 0;
return 1;
} int bfs(int si,int sj,int di,int dj)
{
queue<node>q;
node f,d;
f.x=si;
f.y=sj;
f.cnt=0;
f.bt=6;
vir[f.x][f.y]=6;
q.push(f);
while(!q.empty())
{
f=q.front();
q.pop();
if(f.bt==0)
continue;
if(f.x==di&&f.y==dj)
return f.cnt;
for(int i=0;i<4;i++)
{
d.x=f.x+dir[i][0];
d.y=f.y+dir[i][1];
d.bt=f.bt-1;
if(mp[d.x][d.y]==4&&d.bt!=0)
d.bt=6;
if(cheak(d.x,d.y)&&vir[d.x][d.y]<d.bt)
{
vir[d.x][d.y]=d.bt;
d.cnt=f.cnt+1;
q.push(d);
}
}
}
return -1;
} int main()
{
int o,si,sj,di,dj;
while(~scanf("%d",&o))
{
while(o--)
{
scanf("%d%d",&m,&n);
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&mp[i][j]);
if(mp[i][j]==2)
{
si=i;
sj=j;
}
if(mp[i][j]==3)
{
di=i;
dj=j;
}
}
}
memset(vir,0,sizeof(vir));
escape=bfs(si,sj,di,dj);
printf("%d\n",escape); } }
return 0;
}
HDU1072 Nightmare(BFS) 2016-07-24 14:02 40人阅读 评论(0) 收藏的更多相关文章
- POJ2270&&Hdu1808 Halloween treats 2017-06-29 14:29 40人阅读 评论(0) 收藏
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8565 Accepted: 3111 ...
- ZUFE2483 DO IT YOURSELF 2017-05-31 14:41 40人阅读 评论(0) 收藏
2483: DO IT YOURSELF 时间限制: 2 Sec 内存限制: 128 MB 提交: 8 解决: 3 [提交][状态][讨论版] 题目描述 有四个字符串S,T,tmp,ans,一开始 ...
- Hdu1978 How many ways 2017-01-18 14:32 40人阅读 评论(0) 收藏
How many ways Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- Hadoop常见异常及其解决方案 分类: A1_HADOOP 2014-07-09 15:02 4187人阅读 评论(0) 收藏
1.Shell$ExitCodeException 现象:运行hadoop job时出现如下异常: 14/07/09 14:42:50 INFO mapreduce.Job: Task Id : at ...
- HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏
Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...
- iOS正则表达式 分类: ios技术 2015-07-14 14:00 35人阅读 评论(0) 收藏
一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...
- Hadoop入门经典:WordCount 分类: A1_HADOOP 2014-08-20 14:43 2514人阅读 评论(0) 收藏
以下程序在hadoop1.2.1上测试成功. 本例先将源代码呈现,然后详细说明执行步骤,最后对源代码及执行过程进行分析. 一.源代码 package org.jediael.hadoopdemo.wo ...
- Hdu2181 哈密顿绕行世界问题 2017-01-18 14:46 45人阅读 评论(0) 收藏
哈密顿绕行世界问题 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Sub ...
- Lucene学习总结之四:Lucene索引过程分析 2014-06-25 14:18 884人阅读 评论(0) 收藏
对于Lucene的索引过程,除了将词(Term)写入倒排表并最终写入Lucene的索引文件外,还包括分词(Analyzer)和合并段(merge segments)的过程,本次不包括这两部分,将在以后 ...
随机推荐
- Haskell语言学习笔记(24)MonadWriter, Writer, WriterT
MonadWriter 类型类 class (Monoid w, Monad m) => MonadWriter w m | m -> w where writer :: (a,w) -& ...
- REST Client
1. 为什么要使用REST Client 在实际企业开发过程中经常会有这样的需求: 1.我当前开发的这个系统是需要调用其他系统的接口,也就是我们需要频繁的测试接口,尝试不同的入参参数去查看返回结果, ...
- 【346】TF-IDF
Ref: 文本挖掘预处理之向量化与Hash Trick Ref: 文本挖掘预处理之TF-IDF Ref: sklearn.feature_extraction.text.CountVectorizer ...
- 在hadoop运行tensor flow
http://www.infoq.com/cn/articles/deeplearning-tensorflow-casestudy http://www.tuicool.com/articles/a ...
- IntelliJ IDEA教程
http://www.jetbrains.com/help/idea/meet-intellij-idea.html
- java小知识点简单回顾
1.java的数据类型分为两种:简单类型和引用类型(数组.类以及接口).注意,java没有指针的说法,只有引用.简单类型的变量被声明时,存储空间也同时被分配:而引用类型声明变量(对象)时,仅仅为其分配 ...
- 安装phpcms时出现Warning: ob_start(): output handler \'ob_gzhandler\' conflicts with \'zlib
1. 解决方法一: 打开phpcms/base.php,在第57行,修改如下: if(pc_base::load_config('system','gzip') && function ...
- git format-patch 用法【转】
本文转载自:http://blog.csdn.net/xzongyuan/article/details/9425739 git format-patch相对于git diff更便于操作,是更新的打包 ...
- CFR - another java decompiler批量反编译jar文件
jd-gui众所周知,业界公认的反编译必备工具. 笔者目前遇到一个java项目,社区版,想做一个本地化的版本,询问官方,官方说闭源,无奈之下只能反编译了. 面对那么多jar,jd-gui一个个去反编译 ...
- sublime的坑