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)的更多相关文章

  1. zoj 1671 Walking Ant【简单bfs】

    Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometime ...

  2. Walking Ant(一道有意思的蚂蚁游戏,bfs)

    Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometime ...

  3. zoj 1671 Walking Ant

    Walking Ant Time Limit: 2 Seconds      Memory Limit: 65536 KB Ants are quite diligent. They sometime ...

  4. POJ 2110 Mountain Walking 二分+bfs

    传送门 昨天看到这个题还以为是个脑残的dp, 然而脑残的是我. 题目意思就是从左上角走到右下角, 设x为路径上的最大值-最小值, 求x的最小值. 二分x, 对于每一个x, 枚举下界lower, low ...

  5. hdoj-- Walking Ant

    Walking Ant Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total S ...

  6. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  7. [POJ1852]Ants

    Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12431   Accepted: 5462 Description An a ...

  8. Ants(思维)

    Ants Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 12893   Accepted: 5637 Description ...

  9. Ants (POJ 1852)

    题目描述: Description An army of ants walk on a horizontal pole of length l cm, each with a constant spe ...

随机推荐

  1. SQLite header and source version mismatch 解决方案

    我下载了sqlite源码,并且configure, make, make install. 然后就出现SQLite header and source version mismatch  的错误. 上 ...

  2. arm-linux-gcc 安装和测试

    下载交叉编译器http://pan.baidu.com/share/link?shareid=984027778&uk=388424485 第一步进行解压: tar -zxvf 文件 第二部将 ...

  3. JDK Debug

    http://ishare.iask.sina.com.cn/f/23897007.html http://hi.baidu.com/bd_hare/item/7edd0415b60f0101e65c ...

  4. C#中通过Process启动的外部第三方程序MainWindowHandle句柄为0

    原文 C#中通过Process启动的外部第三方程序MainWindowHandle句柄为0 前几天遇到了一个在C#的winform程序中,启动一个第三方jar程序,并修改jar运行窗体的标题的问题. ...

  5. jQuery 中的 Ajax $.ajax() load() $.get() $.post() $.getJSON() $.getScript()

    1. $.ajax()方法 参数对象属性如下: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") ...

  6. Asp.Net MVC4配置Ext.Net

    首先,下载MVC用的DLL包(Ext.NET.MVC.Pro.2.2.0.zip).讲DLL文件拷贝到工程bin目录下,引用进工程. 第二步,配置Views文件夹下的web.config文件(具体文字 ...

  7. java应用程序远程登录linux并执行其命令(ssh jar包)

    http://www.ganymed.ethz.ch/ssh2/在这个网址下载一个调用ssh和scp命令的jar包. 然后,就可以写程序了.将上面的jar包导入MyEclipse,下面是一个类的实例代 ...

  8. RBF network

    1.radial basis function RBF表示某种距离,$\mu_m$为中心点,相当于将点$x$到中心点的某种距离作为特征转换 Output方法可以根据需求任意选取(比如使用SVM,log ...

  9. @Transactional 注解说明

    先让我们看代码吧! 以下代码为在"Spring3事务管理--基于tx/aop命名空间的配置"基础上修改.首先修改applicationContext.xml如下: <pre ...

  10. js/jquery获取浏览器窗口可视区域高度和宽度以及滚动条高度实现代码

    获取浏览器窗口的可视区域高度和宽度,滚动条高度有需要的朋友可参考一下.IE中,浏览器显示窗口大小只能以下获取: 代码如下复制代码 代码如下: document.body.offsetWidth doc ...