Walking Ant(bfs)
Walking Ant
Time Limit: 2 Seconds Memory Limit: 65536 KB
Ants are quite diligent. They sometimes build their nests beneath flagstones.
Here, an ant is walking in a rectangular area tiled with square flagstones, seeking the only hole leading to her nest.

The ant takes exactly one second to move from one flagstone to another. That is, if the ant is on the flagstone with coordinates (x,y) at time t, she will be on one of the five flagstones with the following coordinates at time t+1:
(x, y), (x+1, y), (x-1, y), (x, y+1), (x, y-1).
The ant cannot go out of the rectangular area. The ant can visit the same flagstone more than once.
Insects are easy to starve. The ant has to go back to her nest without starving. Physical strength of the ant is expressed by the unit "HP". Initially, the ant has the strength of 6 HP. Every second, she loses 1 HP. When the ant arrives at a flagstone with some food on it, she eats a small piece of the food there, and recovers her strength to the maximum value, i.e., 6 HP, without taking any time. The food is plenty enough, and she can eat it as many times as she wants.
When the ant's strength gets down to 0 HP, she dies and will not move anymore. If the ant's strength gets down to 0 HP at the moment she moves to a flagstone, she does not effectively reach the flagstone: even if some food is on it, she cannot eat it; even if the hole is on that stone, she has to die at the entrance of her home.
If there is a puddle on a flagstone, the ant cannot move there.
Your job is to write a program which computes the minimum possible time for the ant to reach the hole with positive strength from her start position, if ever possible.
Input
The input consists of multiple maps, each representing the size and the arrangement of the rectangular area. A map is given in the following format.
w h
d11 d12 d13 ... d1w
d21 d22 d23 ... d2w
...
dh1 dh2 dh3 ... dhw
The integers w and h are the numbers of flagstones in the x- and y-directions, respectively. w and h are less than or equal to 8. The integer dyx represents the state of the flagstone with coordinates (x, y) as follows.
0: There is a puddle on the flagstone, and the ant cannot move there.
1, 2: Nothing exists on the flagstone, and the ant can move there. `2' indicates where the ant initially stands.
3: The hole to the nest is on the flagstone.
4: Some food is on the flagstone.
There is one and only one flagstone with a hole. Not more than five flagstones have food on them.
The end of the input is indicated by a line with two zeros.
Integer numbers in an input line are separated by at least one space character.
Output
For each map in the input, your program should output one line containing one integer representing the minimum time. If the ant cannot return to her nest, your program should output -1 instead of the minimum time.
Sample Input
3 3
2 1 1
1 1 0
1 1 3
8 4
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
8 5
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
8 7
1 2 1 1 1 1 1 1
1 1 1 1 1 1 1 4
1 1 1 1 1 1 1 1
1 1 1 1 4 1 1 1
4 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 3
8 8
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 4 4 1 1 1 1 1
1 4 4 2 1 1 0 0
1 1 0 0 0 0 0 3
1 1 0 4 1 1 1 1
1 1 1 1 1 1 1 1
8 8
1 1 1 1 1 1 1 1
1 1 2 1 1 1 1 1
1 1 4 4 4 1 1 1
1 1 1 4 4 1 0 1
1 1 1 1 1 1 0 1
1 1 1 1 1 1 0 3
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
0 0
Sample Output
4
-1
13
20
-1
-1
题解:错了好长时间,真心无语。。。。我用vis数组标记了,最后队友说不用标记,我就把标记去了,把血量吃了变0;终于输出正确答案了。。。。。
优先队列代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x));
using namespace std;
int disx[]={,-,,};
int disy[]={,,,-};
int map[][];
int w,h;
struct Node{
int x,y;
int ph,step;
friend bool operator < (Node a,Node b){
return a.step>b.step;
}
};
void bfs(int sx,int sy){
Node a,b;
priority_queue<Node>dl;
a.x=sx;a.y=sy;
a.ph=;
a.step=;
dl.push(a);
int ans=;
while(!dl.empty()){
a=dl.top();
dl.pop();
for(int i=;i<;i++){
b.x=a.x+disx[i];
b.y=a.y+disy[i];
b.ph=a.ph-;
b.step=a.step+;
if(b.x<||b.y<||b.x>=w||b.y>=h||map[b.x][b.y]==)continue;
if(b.ph<=)continue;
if(map[b.x][b.y]==){
ans=;
printf("%d\n",b.step);
return;
}
if(map[b.x][b.y]==)b.ph=,map[b.x][b.y]=;
dl.push(b);
}
}
//printf("%d\n",step);
if(!ans)puts("-1");
}
int main(){
while(scanf("%d%d",&w,&h),w|h){
int sx,sy;
mem(map,);
for(int y=;y<h;y++)
for(int x=;x<w;x++){
scanf("%d",&map[x][y]);
if(map[x][y]==)sx=x,sy=y;
}
bfs(sx,sy);
}
return ;
}
没用优先队列:
#include<stdio.h>
#include<string.h>
#include<queue>
#define mem(x,y) memset(x,y,sizeof(x));
using namespace std;
int disx[]={,-,,};
int disy[]={,,,-};
int map[][];
int w,h,step;
struct Node{
int x,y;
int ph;
};
void bfs(int sx,int sy){
Node a,b;
queue<Node>dl;
a.x=sx;a.y=sy;
a.ph=;
step=;
dl.push(a);
int ans=;
while(!dl.empty()){
step++;
int t=dl.size();
while(t--){
a=dl.front();
dl.pop();
for(int i=;i<;i++){
b.x=a.x+disx[i];
b.y=a.y+disy[i];
b.ph=a.ph-;
if(b.x<||b.y<||b.x>=w||b.y>=h||map[b.x][b.y]==)continue;
if(b.ph<=)continue;
if(map[b.x][b.y]==){
ans=;
printf("%d\n",step);
return;
}
if(map[b.x][b.y]==)b.ph=,map[b.x][b.y]=;
dl.push(b);
}
}
}
//printf("%d\n",step);
if(!ans)puts("-1");
}
int main(){
while(scanf("%d%d",&w,&h),w|h){
int sx,sy;
mem(map,);
for(int y=;y<h;y++)
for(int x=;x<w;x++){
scanf("%d",&map[x][y]);
if(map[x][y]==)sx=x,sy=y;
}
bfs(sx,sy);
}
return ;
}
Walking Ant(bfs)的更多相关文章
- zoj 1671 Walking Ant【简单bfs】
Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB Ants are quite diligent. They sometime ...
- Walking Ant(一道有意思的蚂蚁游戏,bfs)
Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB Ants are quite diligent. They sometime ...
- zoj 1671 Walking Ant
Walking Ant Time Limit: 2 Seconds Memory Limit: 65536 KB Ants are quite diligent. They sometime ...
- POJ 2110 Mountain Walking 二分+bfs
传送门 昨天看到这个题还以为是个脑残的dp, 然而脑残的是我. 题目意思就是从左上角走到右下角, 设x为路径上的最大值-最小值, 求x的最小值. 二分x, 对于每一个x, 枚举下界lower, low ...
- hdoj-- Walking Ant
Walking Ant Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total S ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- [POJ1852]Ants
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12431 Accepted: 5462 Description An a ...
- Ants(思维)
Ants Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12893 Accepted: 5637 Description ...
- Ants (POJ 1852)
题目描述: Description An army of ants walk on a horizontal pole of length l cm, each with a constant spe ...
随机推荐
- SQL Server 数据库游标选项
背景: 游标控制服务器端游标的行为,相关的T-SQL如下: declare , open , fetch , close , deallocate. 1. cursor_close_on_commit ...
- android apk 导出(签名) is not translated in xx 代码混淆 反编译
apk导出遇到问题 解决方式如下 1.导出步骤第一步 2.提示错误 3.解决 其余步骤参见: 代码混淆和数字签名(现在版本混淆) http://blog.csdn.net/moruna/article ...
- Unix/Linux环境C编程入门教程(20) 搭建基于Mac的 Xcode 与 QT 开发环境
1.启动 Vmware,如果没有 VMware 的同学,请看前面我们搭建 VMware 的视频 2.打开虚拟机以后,出现虚拟机界面 3 新建一个虚拟机 4 选择自定义,单击下一步 5 选择默认的 VM ...
- WinEdt7.0 初试
刚刚开始学的时候,安装就出了些问题(关于安装的问题,请看我之前的文章)不知道如何点击运行,编译.看了些博客论坛.终于成功了. 首先先写一个小代码: \documentclass[UTF8]{ctexa ...
- HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个带边权的图.对于每一个询问(S , ...
- Select标签 依据value值默认选中 Jquery
网上找了非常多都是错的,不行的. 以下方法能够的 <script type="text/javascript"> $(document).ready(function( ...
- atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform
atitit.软件开发GUI 布局管理优缺点总结java swing wpf web html c++ qt php asp.net winform 1. Absoluti 布局(经常使用) 1 2. ...
- android 自定义ViewGroup和对view进行切图动画实现滑动菜单SlidingMenu[转]
http://blog.csdn.net/jj120522/article/details/8095852 示意图就不展示了,和上一节的一样,滑动菜单SlidingMenu效果如何大家都比较熟悉,在这 ...
- CSS3 简易照片墙
代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title& ...
- Android之ActionBar学习
关于那个问题:是关于如何生成如下图所示之ActionBar效果: 其实就在官网上就有答案,自己疏忽再加上资料繁多.寻了许久,经过指点.终于找到: To enable split action bar, ...