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)的过程,本次不包括这两部分,将在以后 ...
随机推荐
- 吴裕雄 python神经网络 水果图片识别(2)
import osimport numpy as npimport matplotlib.pyplot as pltfrom skimage import color,data,transform,i ...
- 吴裕雄 数据挖掘与分析案例实战(3)——python数值计算工具:Numpy
# 导入模块,并重命名为npimport numpy as np# 单个列表创建一维数组arr1 = np.array([3,10,8,7,34,11,28,72])print('一维数组:\n',a ...
- 一行转多行 及多行转一行的 hive语句
注意 :|,: 是特殊符号,要用 "\\|", "\\;"来表示. 一行转多行 usertags 里面有很多项,每项之间以逗号分隔 create t ...
- spark 创建稀疏向量和矩阵
http://blog.csdn.net/canglingye/article/details/41316193 [相互转换]:http://stackoverflow.com/questions/3 ...
- idea 安装uml 画图工具
centos7上: yum -y install graphviz mac上: brew install Graphviz file -> setting->plugins 安装plant ...
- edgeR
1)简介 edgeR作用对象是count文件,rows 代表基因,行代表文库,count代表的是比对到每个基因的reads数目.它主要关注的是差异表达分析,而不是定量基因表达水平. edgeR wor ...
- moco操作
1.启动 单个文件启动:将jar包跟启动的文件放在一个文件夹下 命令:java -jar moco-runner-<version>-standalone.jar http -p 12 ...
- HttpClient(一)
package com.cmy.httpClient; import org.apache.commons.httpclient.HttpClient; import org.apache.commo ...
- 利用Google Chrome开发插件,在网页中植入js代码
Google Chrome是一个很强大的浏览器,提供了各种各样的插件,大大提升了使用了的效率,比如vimium.honx等. Google在提供这些插件的同时还允许用户开发自己的插件. 最近在写js的 ...
- [leetcode]543. Diameter of Binary Tree二叉树直径
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...