Nightmare

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14260    Accepted Submission(s): 6930

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
Author  Ignatius.L
 
Recommend  We have carefully selected several similar problems for you:  1026 1016 1010 1175 1180 
-------------------------------------------------------------------------------------------------------------------------------------------------------
这道题目显然是用bfs来做的
但是有几点必须要考虑
因为存在时间归零的情况
所以一个点可能经过多次
又因为时间归零了过后
在这一点会有重复
所以将n=4的点标记为走过即可
这样最后如果到不了就会队列会一直运行直到空
下面拿出我有超多注释
谁都能看懂的代码
-------------------------------------------------------------------------------------------------------------------------------------------------------
 //Author:LanceYu
#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
#include<fstream>
#include<iosfwd>
#include<sstream>
#include<fstream>
#include<cwchar>
#include<iomanip>
#include<ostream>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<ctime>
#include<algorithm>
#include<complex>
#include<cmath>
#include<valarray>
#include<bitset>
#include<iterator>
#define ll long long
using namespace std;
const double clf=1e-;
//const double e=2.718281828;
const double PI=3.141592653589793;
const int MMAX=;
//priority_queue<int>p;
//priority_queue<int,vector<int>,greater<int> >pq;
int n,m,map[][];
int vis[][];
int dir[][]={{-,},{,},{,},{,-}};
struct node
{
int x,y,t,step;
}; int bfs(int a,int b,int x,int y)
{
int i;
queue<node> q;
while(!q.empty())
q.pop();
q.push(node{a,b,,});//开始也能回来再走一次vis保持0
while(!q.empty())
{
node t=q.front();
q.pop();
if(t.x==x&&t.y==y)
return t.step;
if(t.t<=)//会爆炸的情况扔掉
continue;
for(int i=;i<;i++)
{
int dx=t.x+dir[i][];
int dy=t.y+dir[i][];
if(dx>=&&dy>=&&dx<n&&dy<m&&!vis[dx][dy]&&(map[dx][dy]==||map[dx][dy]==)||map[dx][dy]==)//2也有可能重复走过
{
q.push(node{dx,dy,t.t-,t.step+});//此处可以重复走故vis不进行赋值
}
if(dx>=&&dy>=&&dx<n&&dy<m&&!vis[dx][dy]&&map[dx][dy]==)
{
vis[dx][dy]=;
q.push(node{dx,dy,,t.step+});//因为是直接归零,没必要重复走故直接把vis变成1
}
}
}
return -;
}
int main()
{
int t,a,b,x,y;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==)//记录起点
{
a=i;
b=j;
}
if(map[i][j]==)//记录终点
{
x=i;
y=j;
}
}
}
int ans=bfs(a,b,x,y);
printf("%d\n",ans);
}
return ;
}

Notes:思想要求较高

PS:笔者在做这道题的时候把bfs里面的两个判断语句弄反了,WA了好多次,可能这就是菜吧

2018-11-16  05:09:49  Author:LanceYu

最新文章

  1. JVM内存分配策略
  2. &lt;hr&gt;标签不止创建html水平线也可以画圆噢
  3. 修复bootstrap daterangepicker中的3个问题
  4. I18N 国际化
  5. 【原创】OPA857 TEST模式使用
  6. A题笔记(13)
  7. eclipse中我要同时看两个console
  8. 冒泡排序----java实现
  9. Arduino 各种模块篇 粉尘传感器 dust sensor 空气质量检测
  10. CardboardSDK-iOS 源码简单分析
  11. IT题库3-线程实现的方式
  12. 小程序longpress的bug及其解决
  13. 伪类选择器 E:nth-child(n)、E:nth-of-type(n)
  14. FOJ有奖月赛-2016年8月(daxia专场之过四题方有奖)
  15. 一道有意思的多线程面试题 C# 代码实现
  16. 解决qt提示:qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
  17. CSS技巧收集——毛玻璃效果
  18. HTTP协议学习【转】
  19. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST
  20. Ajax请求WCF服务以及跨域的问题解决

热门文章

  1. 【bzoj4006】[JLOI2015]管道连接(斯坦纳树+dp)
  2. 【cf375】D. Tree and Queries(dsu on tree+线段树)
  3. pycharm 设置django server
  4. C++标准库删除字符串中指定字符,比如空格
  5. jQuery中Ajax(三)
  6. centos7 解决docker0: iptables: No chain/target/match by that name
  7. netstat查看端口状态
  8. 局域网部署ntp时间服务器
  9. POJ 2594 (传递闭包 + 最小路径覆盖)
  10. Vue.js 源码分析(三十一) 高级应用 keep-alive 组件 详解