Walking Ant

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 13   Accepted Submission(s) : 9
Problem Description
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. /*蚂蚁在2,家在3,4是食物刚开始是hp=6,走一步hp-1,吃过食物之后hp=6,但是到家的时候
  2. hp=0失败,到食物的时候hp=0也失败,失败之后输出-1,bfs查找*/
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<queue>
  6. using namespace std;
  7. int map[1010][1010];
  8. int dx[4]={1,0,0,-1};
  9. int dy[4]={0,1,-1,0};
  10. int x,y,ex,ey,m,n;
  11. struct node
  12. {
  13. int x,y;
  14. int hp;
  15. int time;
  16. }p,temp;
  17. bool judge(node s)
  18. {
  19. if(s.x<0||s.x>=n||s.y<0||s.y>=m)
  20. return 1;
  21. if(map[s.x][s.y]==0)/*不能越界,不能是墙*/
  22. return 1;
  23. return 0;
  24. }
  25. int bfs()
  26. {
  27. queue<node>q;
  28. while(!q.empty()) q.pop();
  29. p.x=x;
  30. p.y=y;
  31. p.time=0;
  32. p.hp=6;
  33. q.push(p);
  34. //vis[x][y]=1;/*不需要标记,每一步走过之后还可以走回来,但是食物只能吃一次*/
  35. while(!q.empty())
  36. {
  37. p=q.front();
  38. q.pop();
  39. if(p.hp==0) continue;/*到家的时候hp不能等于零*/
  40. if(map[p.x][p.y]==3)
  41. return p.time;
  42. for(int i=0;i<4;i++)
  43. {
  44. temp.x=p.x+dx[i];
  45. temp.y=p.y+dy[i];
  46. temp.time=p.time+1;
  47. if(judge(temp))
  48. continue;
  49. temp.hp=p.hp-1;/*每走一步时间加一,hp-1*/
  50. if(map[temp.x][temp.y]==4&&temp.hp)
  51. {
  52. temp.hp=6;
  53. map[temp.x][temp.y]=1;/*吃过之后标记,表示为路*/
  54. }
  55. //vis[temp.x][temp.y]=1;
  56. q.push(temp);
  57. }
  58. }
  59. return -1;
  60. }
  61. int main()
  62. {
  63. while(scanf("%d%d",&m,&n),m|n)
  64. {
  65. for(int i=0;i<n;i++)
  66. for(int j=0;j<m;j++)
  67. {
  68. scanf("%d",&map[i][j]);
  69. if(map[i][j]==2)
  70. {
  71. x=i;y=j;
  72. }
  73. /*if(map[i][j]==3)
  74. {
  75. ex=i;ey=j;
  76. }*/
  77. }
  78. /*for(int i=0;i<n;i++)
  79. {
  80. for(int j=0;j<m;j++)
  81. printf("%d ",map[i][j]);
  82. printf("\n");
  83. }*/
  84. int num =bfs();
  85. printf("%d\n",num);
  86. }
  87. return 0;
  88. }

hdoj-- 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. zoj 1671 Walking Ant

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

  5. hdoj 3018 Ant Trip(无向图欧拉路||一笔画+并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3018 思路分析:题目可以看做一笔画问题,求最少画多少笔可以把所有的边画一次并且只画一次: 首先可以求出 ...

  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. Runtime的相关知识

    Runtime是近年来面试遇到的一个高频方向,也是我们平时开发中或多或少接触的一个领域,那么什么是runtime呢?它又可以用来做什么呢? 什么是Runtime?平时项目中有用过么? OC是一门动态性 ...

  2. Oracle存储过程给变量赋值的方法

    截止到目前我发现有三种方法可以在存储过程中给变量进行赋值: 1.直接法     := 如:v_flag := 0; 2.select into 如:假设变量名为v_flag,select count( ...

  3. 图像连通域检测的2路算法Code

    本文算法描述参考链接:http://blog.csdn.net/icvpr/article/details/10259577 两遍扫描法: (1)第一次扫描: 访问当前像素B(x,y),如果B(x,y ...

  4. html 底部虚线

    <div style="width: 100%; font-size: 14px; color: #666; border-bottom: 1px dashed #666;" ...

  5. C# 获取 IEnumerable 集合的个数

    IEnumerable<DocApply> data1 = data.Where(n => n.DocName.Contains(search)); if (data1.GetEnu ...

  6. 【转载】java的常见类型转换

    //Int型数字转换成String int num1=123456; //方法1 String str1=num1+""; System.out.println(str1); // ...

  7. Django 模型层(标签、过滤器、模板的继承与导入)

    过滤器/自定义过滤器 模板语法中的过滤器类似于python中的内置方法,在我们把数据从后端通过rander传入到前端html文件中之后,在前端我们可以通过模板语法,对传入的数据再进行以通骚操作. 首先 ...

  8. html 报告页面样式

    修改了下HTML页面样式 页面代码 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  9. ecshop ad调用指定广告的方法 邓士鹏

      在include/lib_goods.php文件下面新增:function getads($cat,$num){$time = gmtime();$sql = "SELECT * FRO ...

  10. Codeforces 1106F Lunar New Year and a Recursive Sequence (数学、线性代数、线性递推、数论、BSGS、扩展欧几里得算法)

    哎呀大水题..我写了一个多小时..好没救啊.. 数论板子X合一? 注意: 本文中变量名称区分大小写. 题意: 给一个\(n\)阶递推序列\(f_k=\prod^{n}_{i=1} f_{k-i}b_i ...