HDU-1072 Nightmare (bfs+贪心)
Nightmare
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 4 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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
Author
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<deque>
#include<cstring>
using namespace std; int dr[][]={{,},{,},{-,},{,-} };
int n,m,i,j,sx,sy,flag,ans,t,tx,ty;
int mp[][],vis[][],time[][],ti[][];
struct node
{
int x,y;
};
deque<node> s;
int check(int x,int y)
{
if (x> && x<=n && y> && y<=m && mp[x][y]!=) return ;
else return ;
}
void bfs()
{
node p;
s.clear();
p.x=sx;
p.y=sy;
vis[sx][sy]=;
s.push_back(p);
while(!s.empty())
{
node q=s.front();
for(int i=;i<;i++)
{
int xx=q.x+dr[i][];
int yy=q.y+dr[i][];
if(check(xx,yy))
{
if(vis[xx][yy])
{
if (ti[xx][yy]<ti[q.x][q.y]-)
{
ti[xx][yy]=ti[q.x][q.y]-;
time[xx][yy]=time[q.x][q.y]+;
p.x=xx;
p.y=yy;
s.push_back(p);
}
} else
{
if(ti[q.x][q.y]->)
{
p.x=xx;
p.y=yy;
s.push_back(p);
vis[xx][yy]=;
time[xx][yy]=time[q.x][q.y]+;
ti[xx][yy]=ti[q.x][q.y]-;
if(mp[xx][yy]==)
{
ti[xx][yy]=;
mp[xx][yy]=;
}
if (mp[xx][yy]==) return;
}
} }
}
s.pop_front();
}
return;
} int main()
{
scanf("%d",&t);
for(;t>;t--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
for(j=;j<=m;j++)
{
scanf("%d",&mp[i][j]);
if (mp[i][j]==) sx=i,sy=j;
if (mp[i][j]==) tx=i,ty=j;
}
ans=INT_MAX;
flag=;
memset(vis,,sizeof(vis));
memset(ti,,sizeof(ti));
memset(time,-,sizeof(time));
ti[sx][sy]=;
time[sx][sy]=;
bfs();
/*if(flag) printf("%d\n",ans);
else printf("-1\n");*/
printf("%d\n",time[tx][ty]);
/* printf("------------------\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
printf("%d ",ti[i][j]);
printf("\n");
}*/
}
return ;
}
HDU-1072 Nightmare (bfs+贪心)的更多相关文章
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
- hdu - 1072 Nightmare(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1072 遇到Bomb-Reset-Equipment的时候除了时间恢复之外,必须把这个点做标记不能再走,不然可能造 ...
- HDU 1072 Nightmare
Description Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on ...
- HDU 1072 Nightmare (广搜)
题目链接 Problem Description Ignatius had a nightmare last night. He found himself in a labyrinth with a ...
- HDU 1072 Nightmare 题解
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ)
HDU 3085 Nightmare Ⅱ(噩梦 Ⅱ) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- [hdu P3085] Nightmare Ⅱ
[hdu P3085] Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU - 3085 Nightmare Ⅱ
HDU - 3085 Nightmare Ⅱ 双向BFS,建立两个队列,让男孩女孩一起走 鬼的位置用曼哈顿距离判断一下,如果该位置与鬼的曼哈顿距离小于等于当前轮数的两倍,则已经被鬼覆盖 #includ ...
- UVA12130 Summits(BFS + 贪心)
UVA12130 Summits(BFS + 贪心) 题目链接 题目大意: 给你一个h ∗ w 的矩阵,矩阵的每一个元素都有一个值,代表这个位置的高度. 题目要求你找出这个图中有多少个位置是峰值点.从 ...
随机推荐
- eclipse启动Heritrix
首先下载heritrix-1.14.4-src源码.可以在http://sourceforge.net/projects/archive-crawler/files/archive-crawler ( ...
- robotium和appium的一些区别
Appium是基于UIAutomator框架实现的.Appium测试进程与目标应用进程是分开的,所以Appium不能直接访问目标应用的各种element属性进行copy&paste,而只能模拟 ...
- 通过httplib2 探索的学习的最佳方式
在工作中需要对一个视频点播两百次,使其成为热门视频,才能对其p2p情况进行测试.虽然可以手动点播两百次,但是利用python发送200次post请求,能减少很多的工作量.该发送请求的方法用到了http ...
- samba server 设置
samba server 设置yum install samba.x86_64systemctl start smb.servicesystemctl enable smb.servicesamb ...
- Tennis Championship
Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- sql 查询一段时间内某个时间点数据
SELECT CONVERT(VARCHAR(10), dtCreateTime, 120) AS dtStatisticsCreateDate, COUNT(1) AS nStatisticsC ...
- how to download image from any web page in java 下载图片
http://stackoverflow.com/questions/5882005/how-to-download-image-from-any-web-page-in-java (throws I ...
- json格式数据,将数据库中查询的结果转换为json, 然后调用接口的方式返回json(方式一)
调用接口,无非也就是打开链接 读取流 将结果以流的形式输出 将查询结果以json返回,无非就是将查询到的结果转换成jsonObject ================================ ...
- 安装lnmp(linux nginx mysql php)
下载或者在云盘里找lnmp1.2-full.tar.gz 用 tar -zxvf lnmp1.2-full.tar.gz解压 进入 ,运行./install.sh安装.根据提示. 如果出现yum锁定, ...
- permutation求全排列
include <iostream> #include <string> using namespace std; void swap(char &c1, char & ...