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

注意:行列别弄反了  题中输入的w代表列h代表行

题意:2为起点,3为终点,1为空地,4为食物,0为水池不可通过,蚂蚁起始的HP为6,每走一步HP减少1,如果蚂蚁

可以吃到食物则HP恢复为6(到达食物所在地时最少HP要求为1)问蚂蚁需要几步可以从起点走到终点

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<queue>
  4. using namespace std;
  5. int n,m;
  6. int map[10][10];
  7. int x1,x2,y1,y2;
  8. struct node
  9. {
  10. int x,y;
  11. int step;
  12. int time;
  13. friend bool operator < (node a,node b)
  14. {
  15. return a.step>b.step;
  16. }
  17. };
  18. void getmap()
  19. {
  20. int i,j;
  21. for(i=0;i<n;i++)
  22. {
  23. for(j=0;j<m;j++)
  24. {
  25. scanf("%d",&map[i][j]);
  26. if(map[i][j]==2)
  27. {
  28. x1=i;y1=j;
  29. }
  30. else if(map[i][j]==3)
  31. {
  32. x2=i;y2=j;
  33. }
  34. }
  35. }
  36. }
  37. int judge(int r,int c)
  38. {
  39. if(r < 0||r >= n)
  40. return 0;
  41. if(c < 0||c >= m)
  42. return 0;
  43. if(map[r][c]==0)
  44. return 0;
  45. return 1;
  46. }
  47. void bfs()
  48. {
  49. int i,j;
  50. int move[4][2]={0,1,0,-1,1,0,-1,0};
  51. node beg,end;
  52. priority_queue<node>q;
  53. beg.x=x1;
  54. beg.y=y1;
  55. beg.step=0;
  56. beg.time=6;
  57. q.push(beg);
  58. while(!q.empty())
  59. {
  60. end=q.top();
  61. q.pop();
  62. if(end.x==x2&&end.y==y2)
  63. {
  64. printf("%d\n",end.step);
  65. return ;
  66. }
  67. if(end.time==1)//当到达食物的前一步时血量为1则就会死
  68. continue;
  69. for(i=0;i<4;i++)
  70. {
  71. beg.x=end.x+move[i][0];
  72. beg.y=end.y+move[i][1];
  73. if(judge(beg.x,beg.y))
  74. {
  75. if(map[beg.x][beg.y]==4)
  76. {
  77. beg.time=6;
  78. map[beg.x][beg.y]=1;//标记吃过的食物不能再吃
  79. }
  80. else
  81. beg.time=end.time-1;
  82. beg.step=end.step+1;
  83. q.push(beg);
  84. }
  85. }
  86. }
  87. printf("-1\n");
  88. }
  89. int main()
  90. {
  91. int i,j;
  92. while(scanf("%d%d",&m,&n),n|m)
  93. {
  94. getmap();
  95. bfs();
  96. }
  97. return 0;
  98. }

  

zoj 1671 Walking Ant【简单bfs】的更多相关文章

  1. zoj 1671 Walking Ant

    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. LightOJ 1012 简单bfs,水

    1.LightOJ 1012  Guilty Prince  简单bfs 2.总结:水 题意:迷宫,求有多少位置可去 #include<iostream> #include<cstr ...

  5. POJ3185(简单BFS,主要做测试使用)

    没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是(1<<20) 第一次提交使用STL的queue,并且是在队首判断是否达到终点,达到终点就退出,超时:(其实 ...

  6. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  7. hdu1312 Red and Black 简单BFS

    简单BFS模版题 不多说了..... 直接晒代码哦.... #include<cstdlib> #include<iostream> #include<cstdio> ...

  8. 逃脱 (简单BFS)

    题目传送门 G逃脱  题目描述 这是mengxiang000和Tabris来到幼儿园的第四天,幼儿园老师在值班的时候突然发现幼儿园某处发生火灾,而且火势蔓延极快,老师在第一时间就发出了警报,位于幼儿园 ...

  9. zoj 1622 Switch 开关灯 简单枚举

    ZOJ Problem Set - 1622 Switch Time Limit: 2 Seconds      Memory Limit: 65536 KB There are N lights i ...

随机推荐

  1. UITextField 对输入金额的约束

    [2016/1/18更新] -- 五个人辛辛苦苦干了一年的项目终于上线了,今天有空看了一下正则表达式教程,然后开始rebuild之前的种种对字符串的约束,首先就从这个金额输入框开始吧,修改后的代码如下 ...

  2. JAVA学习-基础知识

    1.Java程序都是以类的形式编写的.2.存放源代码的文件叫源文件.(电脑不能直接看懂的,需要编译一下,电脑才能懂)如何编译源文件?用javac命令输入"javac 123.Java&quo ...

  3. SVM(支持向量机)算法

    第一步.初步了解SVM 1.0.什么是支持向量机SVM 要明白什么是SVM,便得从分类说起. 分类作为数据挖掘领域中一项非常重要的任务,它的目的是学会一个分类函数或分类模型(或者叫做分类器),而支持向 ...

  4. shell命令:echo命令详解

    功能说明:显示文字. 语 法:echo [-ne][字符串] / echo [--help][--version] 补充说明:echo会将输入的字符串送往标准输出.输出的字符串间以空白字符隔开, 并在 ...

  5. django基本命令备忘录

    1. 新建一个 django project django-admin.py startproject project-name 新建 app python manage.py startapp ap ...

  6. WF学习笔记(四)

    1.使用WorkflowApplication.OnUnhandledException 处理Code异常 ,用于产生异常的Activity: public class ExceptionActivi ...

  7. (转)ASP.NET-关于Container dataitem 与 eval方法介绍

    Container是一个数据容器,代表集合类或者dataview中的一行,而Container.dataitem代表该行的数据:所有的container   被存 放在是一个栈堆stack中,自动的将 ...

  8. ecshop安装程序源码阅读-安装脚本(1)

    定义系统判断常量 引入安装初始化文件 设置时区 报告所有错误 定义站点根常量 定义php自身相对路径 引入系统,公共函数库 引入并初始化错误处理对象 引入并初始化模板引擎 引入安装相关业务 发送HTT ...

  9. 对于volatile的理解

    哎.要学的东西太多,时间太少.一周的工作下来要总结的东西太多,还处理不完,越积越多.大周末的好想出去玩啊.... 得嘞,废话止于此. 无聊时候乱看网页发现了volatile的一篇文章,以前曾经对vol ...

  10. POJ 1013 Counterfeit Dollar 集合上的位运算

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...