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 ...
随机推荐
- JQuery DataTables Editor---页面内容修改&&数据库信息修改 (2)
接上篇博文,详细说一下js代码以及JQuery DataTables Editor---页面内容修改&&数据库信息修改遇到的问题和解决办法. 1.关于dialog 初始化: $(&qu ...
- JAVA可变参数实例
public class Kebiancanshu { public static void main(String[] args) { System.out.println(average(8, 2 ...
- php快递查询
http://www.oschina.net/code/snippet_60100_25087 <?php class Express { private $expressname =array ...
- [Leetcode][Python]40: Combination Sum II
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 40: Combination Sum IIhttps://oj.leetco ...
- ubuntu14.04 qt4开发环境搭建(vnc use gnome)
1,安装qt开发环境软件包:apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4-designer qtcreator; 2 ...
- fuel Explain
http://docs.mirantis.com/openstack/fuel/fuel-5.1/ https://software.mirantis.com/quick-start/ https:/ ...
- c++ - How to use wstring and wcout to output Chinese words in Xcode? - Stack Overflow
c++ - How to use wstring and wcout to output Chinese words in Xcode? - Stack Overflow How to use wst ...
- windows开机启动nginx
1 .http://www.cuplayer.com/player/PlayerCode/Nginx/2014/0919/1577.html 2. http://www.cnblogs.com/xus ...
- HipHop算法:利用微博互动关系挖掘社交圈
/* 版权声明:可以任意转载,转载时请务必标明文章原始出处和作者信息 .*/ CopyMiddle: 张俊林 TimeStamp:2012年3 月 在微博环境下,如何 ...
- 黑马程序员_高新技术之javaBean,注解,类加载器
----------- android培训.java培训.java学习型技术博客.期待与您交流! ---------- 第一部分 javaBean 一,由内省引出javaBean 1,内省: 内省对应 ...