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 ...
随机推荐
- Oracle EBS-SQL (INV-4):检查负库存记录数.sql
DEFINE DATE1="01/15/20** 23:59:59" /*输入指定日期*/DEFINE CODE="%" ...
- Linux网桥
linux网桥的功能 转发数据包 网桥的功能在延长网络跨度上类似于中继器,然而它能提供智能化连接服务,即根据帧的终点地址处于哪一网段来进行转发和滤除.网桥对站点所处网段的了解是靠"自学习&q ...
- 调magento自定义模板发邮件
1. 设置邮件模板 <global> <template> <email> <custom_email_template1 module="Samp ...
- CodeForces 111B - Petya and Divisors 统计..想法题
找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...
- JAVA责任链设计模式
<JAVA与模式>之责任链模式 在阎宏博士的<JAVA与模式>一书中开头是这样描述责任链(Chain of Responsibility)模式的: 责任链模式是一种对象的行为模 ...
- c++中自增(++)和自减(--)操作符
自增(++)和自减(--)操作符为对象加1 或减1 操作提供了方便简短的实现方式.它们有前置和后置两种使用形式.到目前为止,我们已经使用过前自增操作,该操作使其操作数加1,操作结果是修改后的值.同理, ...
- SQL数据库知识二(Day 25)
又到了总结知识的时候了,今天主要把SQL数据库给简单的学完了,明天开始就要开始学ADO.NET的知识了.好了,话不多说,还是看一下今天都学了哪些内容. 1 字符串类型的知识点 --类型的使用 --截 ...
- MySQL常用指令
1.win下启动MySQL 命令行下输入: mysql –h localhost –u root -p / mysql -uroot -p 2.MySql下建表 输入命令 show database ...
- 查询EBS在线用户SQL(R12)
SELECT U.USER_NAME, APP.APPLICATION_SHORT_NAME, FAT.APPLICATION_NAME, FR.RESPONSIBILITY_KEY, FRT.RES ...
- 使用meaven打包过程中遇到的一些问题
开始使用如下代码进行打包 <build> <!-- mvn assembly:assembly -Dmaven.test.skip=true --> <plugins&g ...