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

一仅仅蚂蚁在一个迷宫里,1能够走。0表示墙。2起始位置,3出口,4食物。初始体力为6,每走一步就消耗体力1

假设体力消耗完不到出口就输出-1。吃到食物体力恢复到6。注意一点,当它体力变为1时。到下一步可以到有

食物的点,但却不能吃食物,由于这时候体力已经到0了。即使有食物也不能补充体力,假设这个点是出口也不能出去。另

外吃了食物后要把那个

点的值变为1。由于有可能再走这个点。

bfs,注意凝视地方。错了好几次

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define M 1000
int n,m,sx,sy,ex,ey;
int mp[M][M];
int dre[4][2]={-1,0,1,0,0,-1,0,1};
struct node{
int x,y,hp,step;
}s,v;
int bfs(){
queue <node> Q;
v.x=sx; v.y=sy; v.hp=6; v.step=0;
Q.push(v);
while(!Q.empty()){
s=Q.front();
Q.pop();
for(int i=0;i<4;i++){
v.x=s.x+dre[i][0];
v.y=s.y+dre[i][1];
v.hp=s.hp-1;
v.step=s.step+1;
if(v.hp==0) continue;//无论有没有食物,搜索其它点 
if(mp[v.x][v.y]==3) return v.step;
if(v.x<1||v.y<1||v.x>n||v.y>m||mp[v.x][v.y]==0)
continue;
if(mp[v.x][v.y]==4){
v.hp=6;
mp[v.x][v.y]=1;//这一定要变为1。由于每一个点不止能走一次
}
Q.push(v);
}
}
return -1;
}
int main(){
int i,j;
while(~scanf("%d%d",&m,&n),n+m){
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&mp[i][j]);
if(mp[i][j]==2){
sx=i; sy=j;
}
if(mp[i][j]==3){
ex=i; ey=j;
}
}
}
printf("%d\n",bfs());
}
return 0;
}

zoj 1671 Walking Ant的更多相关文章

  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. Walking Ant(bfs)

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

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

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

  5. hdoj-- Walking Ant

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

  6. [POJ1852]Ants

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

  7. Ants(思维)

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

  8. Ants (POJ 1852)

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

  9. UVA - 10714 Ants

    最多时间就是每仅仅蚂蚁选择最久的爬行方式 最少时间就是每仅仅蚂蚁选择最快地爬行方式 #include<iostream> #include<map> #include<s ...

随机推荐

  1. leetcode650 2 Keys Keyboard

    思路: 把给定的数分解质因子之后,对于每一个质因子x,都需要x次操作(一次copy all操作和x-1次paste),所以答案就是对分解后的所有质因子求和. 实现: class Solution { ...

  2. Android Retrofit+Rxjava2问题小记

    网络请求有个问题就是取消操作. 在Rxjava1中,调用subscribe之后会返回Subscription,然后利用CompositeSubscription进行统一管理. 在Rxjava2中,调用 ...

  3. Js上传图片并生成缩略图

    Js上传图片并显示缩略图的流程为 Js选择文件->Jquery上传图片->服务器接收图片流->存储图片->返回结果到Js端->显示缩略图 本文上传图片所用的Js库是aja ...

  4. 联想 Z5(L78011) 免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 10.5.254

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  5. Java中PrintStream(打印输出流)

    Java中PrintStream(打印输出流)   PrintStream 是打印输出流,它继承于FilterOutputStream. PrintStream 是用来装饰其它输出流.它能为其他输出流 ...

  6. Fiddler 修改响应内容

    1. 导入 FiddlerCore.dll 第三方库. 2. 开启侦听端口,FiddlerApplication.Startup(8888, FiddlerCoreStartupFlags.Defau ...

  7. Description Resource Path Location Type Missing artifact com.********:framework:jar:1.0.2 pom.xml /项目名 line **** Maven Dependency Problem

    问题具体描述如下图所示: 对于该问题本人是这么解决的. 在window下[Preferences]目录找到[Maven]下的[usersetting] 查看local repository 里面的路径 ...

  8. 06C语言运算符

    C语言运算符 算术运算符 运算符 描述 + 把两个操作数相加 - 从第一个操作数中减去第二个操作数 * 把两个操作数相乘 / 分子除以分母 % 取模运算符,整除后的余数 ++ 自增运算符,整数值增加 ...

  9. xmpp之配置Xcode(1)

    介绍 ios上的XMPPFramework你能够在Xcode/iPhoneXMPP 目录找到,它只是实现了XMPP的一小部分功能. 下面主要介绍在开发XMPPFramework ios应用之前的配置工 ...

  10. POJ P2096 Collecting Bugs

    思路 分类讨论,不妨先设$DP[i][j]$表示已经发现$i$种子系统中有$n$种$bug$无非只有四种情况 发现的$bug$在旧的系统旧的分类,概率$p1$是$(i/s)*(j/n)$. 发现的$b ...