Nightmare

题目链接:       杭电oj 1702

题目描述:

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

4

-1

13

#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#define MAX 250
using namespace std;
int map[MAX][MAX];
int judge[MAX][MAX]={0};
int N,M;
int dir[4][2]={{0,1},{1,0},{0,-1},{-1,0}};//定义四个方向 typedef struct PLACE{
int x;
int y;
int step;//记录步数
int time;//记录剩余时间 bool operator <(const PLACE &a) const        //重载排序方法{return a.step<step;}
};//定义步数优先的结构体用于优先队列 PLACE Ignatius;void drow_map(int N,int M)
{int i,j;for(i=0;i<N;i++){for(j=0;j<M;j++){cin>>map[i][j];if(map[i][j]==2)  //找Ignatius的初始位置 {Ignatius.x=i;Ignatius.y=j;Ignatius.step=0;Ignatius.time=6;judge[i][j]=1;}}}
}int main()
{int i,flag,step,door,n,life;priority_queue<PLACE> place;//定义步数优先的优先队列 PLACE place_deal,deal;   cin>>n;while(n--){flag=0;//用于标记是否到达出口 life=1;//用于记录是否生存 cin>>N>>M;drow_map(N,M);//录入地图 place.push(Ignatius);while(!place.empty()){if(flag==1)break;//到达出口 place_deal=place.top();place.pop();for(int i=0;i<4;i++)//对当前位置的四个方向依次判断 {life=1;deal.step=place_deal.step+1;deal.x=place_deal.x+dir[i][0];deal.y=place_deal.y+dir[i][1];deal.time=place_deal.time-1;if(deal.time==0)//当时间为0时炸弹爆炸,人物死亡 life=0;if(map[deal.x][deal.y]==4&&life==1)//对爆炸时间修改 {deal.time=6;}if(map[deal.x][deal.y]==3&&life==1){flag=1;step=deal.step;break;}if(life==1&&deal.x>=0&&deal.x<N&&deal.y>=0&&deal.y<M&&(map[deal.x][deal.y]==1||map[deal.x][deal.y]==4)){if(map[deal.x][deal.y]==4)map[deal.x][deal.y]=0;place.push(deal);}}}if(flag==1)cout<<step<<endl;elsecout<<"-1"<<endl;while(!place.empty()){place.pop();}memset(map,0,sizeof(map));}
return 0;
}

杭电oj-----Nightmare(BFS)相关推荐

  1. 杭电OJ分类题目(1)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...

  2. 地下城夺宝游戏——杭电OJ 1044题解析

    题目来源:杭电OJ-1044 题目大意:一个探险家身处一个危险的地下城,城中很危险,并且城中分散着若干个珠宝:现在地下城即将塌陷,冒险家需要在有限的时间内逃出去,但他希望在逃生的过程中获取一些珠宝并使 ...

  3. 【ACM】杭电OJ 2037

    题目链接:杭电OJ 2037 先把b[i]进行排序,然后,b[i]与a[i+1]进行比较. #include <iostream> #include <cstdio> #inc ...

  4. 【ACM】杭电OJ 2020(排序)

    题目链接:杭电OJ 2020 排序可以有冒泡排序,选择排序,或者直接调用函数. 下面是选择排序: #include <stdio.h> #include <math.h> in ...

  5. 【ACM】杭电OJ 2018

    题目链接:杭电OJ 2018 从n>4开始,每一年的牛的数量=前一年的牛的数量+三年前的牛的数量 问:为什么是三年前? 答:假设三年前有一头小牛出生,出生的那一年即为第一年,到了第四年,即三年后 ...

  6. 【ACM】杭电OJ 1005

     题目链接:杭电OJ 1005 超时代码如下(而且开辟的数组空间大小不够): #include <stdio.h> int m[100000]; int f(int n,int a,int ...

  7. 【ACM】杭电OJ 1004

     题目链接:杭电OJ 1004 运行环境:Dev-C++ 5.11 思路: 先把先把num数组全部赋值为1:第一个颜色单独输入,从第二个开始,需要与前面的进行比较,如果前面有相同的颜色,则在目前的nu ...

  8. 【ACM】杭电OJ 2012。

    题目链接:杭电OJ 2012 思路很简单,但是有一种高效算法显示编译错误,不知道为什么 运行环境:VS2017 AC代码: #include <stdio.h> #include < ...

  9. 【ACM】杭电OJ 1003。

    运行环境VS2017  题目链接:杭电OJ 1003 主要思想是: 用d[i]来存放前i项中最大的和,得到end,然后再倒推,得起始的位置begin 然而在程序42行的疑问,大家可以讨论一下吗???? ...

  10. 【ACM】杭电OJ 1241(深度优先搜索小结)

    题目链接:杭电OJ 1241 深度优先搜索问题 深度优先搜索是搜索的手段之一.它从某个状态开始,不断地转移状态直到无法转移,然后回退到前一步的状态,继续转移到其他状态,如此不断重复,直至找到最终的解. ...

最新文章

  1. 关于strutsdemo实例的理解
  2. 【Linux系统编程】浅谈进程地址空间与虚拟存储空间
  3. 迁移 WinForm 应用从 dotnet framework 到 dotnetcore3.0
  4. DEDECMS给图集图片{dede:productimagelist}自动编号
  5. docker多个容器一起打包_如何实现多个docker容器同时执行一条命令?
  6. ocx控件 postmessage消息会消失_通过HackerOne漏洞报告学习PostMessage漏洞实战场景中的利用与绕过...
  7. php+代码模板下载地址,简单而强大的PHP模板引擎
  8. 光头强的圆球机器人视频_《熊出没狂野大陆》快上映了,看了多年光头强,还能有新鲜动画吗...
  9. 调整Redmine的用户显示格式
  10. 最小二乘法滤波 c语言,线性回归,最小二乘法 C语言实现
  11. 新能源行业SCM供应链管理平台构建一站式新能源供应链交易闭环
  12. kaggle电影数据分析报告
  13. 超大文件调用讯飞语音听写解决方案
  14. 为什么要避免使用malloc()和free()函数?
  15. python 交通_Python可视化交通拥堵情况
  16. GB2312和BIG5,Unicode/UTF8等编码之间的互相转化
  17. 计算机技术基础概念,2017年计算机三级网络技术基本概念与名词解释:计算机基础知识部分...
  18. 广东外语外贸大学计算机考研资料汇总
  19. node.js 系列——Buffer
  20. 红帽子认证辅导教程(转)

热门文章

  1. Mybatis常见面试题
  2. View的setLayerType() , setDrawingCacheEnabled() 方法用法
  3. datagrid鼠标悬浮提示
  4. Fresco几处不太好的地方
  5. 无法使用资源管理器浏览文档库?
  6. python cookbook 2字符串(2)
  7. MySql+Memcached架构的问题
  8. 数据结构——队列操作
  9. echarts词云第一次出现不了数据要刷新才能出现_隐秘的角落弹幕分析,制作词云,看看观众们对该剧的评价如何...
  10. 将计算机知识应用于生活中,电脑知识在生活中的灵活运用(6页)-原创力文档...