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;终于输出正确答案了。。。。。

优先队列代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #define mem(x,y) memset(x,y,sizeof(x));
  5. using namespace std;
  6. int disx[]={,-,,};
  7. int disy[]={,,,-};
  8. int map[][];
  9. int w,h;
  10. struct Node{
  11. int x,y;
  12. int ph,step;
  13. friend bool operator < (Node a,Node b){
  14. return a.step>b.step;
  15. }
  16. };
  17. void bfs(int sx,int sy){
  18. Node a,b;
  19. priority_queue<Node>dl;
  20. a.x=sx;a.y=sy;
  21. a.ph=;
  22. a.step=;
  23. dl.push(a);
  24. int ans=;
  25. while(!dl.empty()){
  26. a=dl.top();
  27. dl.pop();
  28. for(int i=;i<;i++){
  29. b.x=a.x+disx[i];
  30. b.y=a.y+disy[i];
  31. b.ph=a.ph-;
  32. b.step=a.step+;
  33. if(b.x<||b.y<||b.x>=w||b.y>=h||map[b.x][b.y]==)continue;
  34. if(b.ph<=)continue;
  35. if(map[b.x][b.y]==){
  36. ans=;
  37. printf("%d\n",b.step);
  38. return;
  39. }
  40. if(map[b.x][b.y]==)b.ph=,map[b.x][b.y]=;
  41. dl.push(b);
  42. }
  43. }
  44. //printf("%d\n",step);
  45. if(!ans)puts("-1");
  46. }
  47. int main(){
  48. while(scanf("%d%d",&w,&h),w|h){
  49. int sx,sy;
  50. mem(map,);
  51. for(int y=;y<h;y++)
  52. for(int x=;x<w;x++){
  53. scanf("%d",&map[x][y]);
  54. if(map[x][y]==)sx=x,sy=y;
  55. }
  56. bfs(sx,sy);
  57. }
  58. return ;
  59. }

没用优先队列:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. #define mem(x,y) memset(x,y,sizeof(x));
  5. using namespace std;
  6. int disx[]={,-,,};
  7. int disy[]={,,,-};
  8. int map[][];
  9. int w,h,step;
  10. struct Node{
  11. int x,y;
  12. int ph;
  13. };
  14. void bfs(int sx,int sy){
  15. Node a,b;
  16. queue<Node>dl;
  17. a.x=sx;a.y=sy;
  18. a.ph=;
  19. step=;
  20. dl.push(a);
  21. int ans=;
  22. while(!dl.empty()){
  23. step++;
  24. int t=dl.size();
  25. while(t--){
  26. a=dl.front();
  27. dl.pop();
  28. for(int i=;i<;i++){
  29. b.x=a.x+disx[i];
  30. b.y=a.y+disy[i];
  31. b.ph=a.ph-;
  32. if(b.x<||b.y<||b.x>=w||b.y>=h||map[b.x][b.y]==)continue;
  33. if(b.ph<=)continue;
  34. if(map[b.x][b.y]==){
  35. ans=;
  36. printf("%d\n",step);
  37. return;
  38. }
  39. if(map[b.x][b.y]==)b.ph=,map[b.x][b.y]=;
  40. dl.push(b);
  41. }
  42. }
  43. }
  44. //printf("%d\n",step);
  45. if(!ans)puts("-1");
  46. }
  47. int main(){
  48. while(scanf("%d%d",&w,&h),w|h){
  49. int sx,sy;
  50. mem(map,);
  51. for(int y=;y<h;y++)
  52. for(int x=;x<w;x++){
  53. scanf("%d",&map[x][y]);
  54. if(map[x][y]==)sx=x,sy=y;
  55. }
  56. bfs(sx,sy);
  57. }
  58. return ;
  59. }

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. android XML格式颜色

    <!--android:background="@color/"-> <?xml version="1.0" encoding="u ...

  2. 计算机图形学--旋转变换(java)

    import java.awt.Color; import java.awt.Frame; import java.awt.Graphics; import java.awt.event.Window ...

  3. Codeforces 241B

    因为博客园的公式编辑有点坑,所以-- 原题

  4. Udacity-Artificial Intelligence for Robotics 课程笔记

    Lesson 1 Localization 蒙特卡洛机器人定位模型 sense 贝叶斯模型 move 全概率公式 localization练习 # The function localize take ...

  5. poj3429(有错)

    不知道为什么错.. /************************************************************************* > File Name: ...

  6. leftpad填充函数;

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. PHP+jQuery实现翻板抽奖

    翻板抽奖的实现流程:前端页面提供6个方块,用数字1-6依次表示6个不同的方块,当抽奖者点击6个方块中的某一块时,方块翻转到背面,显示抽奖中奖信息.看似简单的一个操作过程,却包含着WEB技术的很多知识面 ...

  8. C#的输入输出及基本类型

    //输出 Console.WriteLine("摩西摩西"); Console.Write("hollo");不带回车的 注意: 1.注意大小写敏感.(快捷键操 ...

  9. TextView 为部分文字添加下划线,并实现单击事件

    在开发应用的过程中经常会遇到显示一些不同的字体风格的信息,如关键词高亮显示的等.对于类似的情况,一般我们会想着使用多个TextView去实现,对于每个TextView设置不同的字体风格来满足需求.   ...

  10. 经典:十步完全理解 SQL

    经典:十步完全理解 SQL   来源:伯乐在线 链接:http://blog.jobbole.com/55086/ 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完 ...