博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Walking Ant(bfs)
阅读量:5797 次
发布时间:2019-06-18

本文共 6240 字,大约阅读时间需要 20 分钟。

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
2 #include
3 #include
4 #define mem(x,y) memset(x,y,sizeof(x)); 5 using namespace std; 6 int disx[4]={
1,-1,0,0}; 7 int disy[4]={
0,0,1,-1}; 8 int map[9][9]; 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
dl;20 a.x=sx;a.y=sy;21 a.ph=6;22 a.step=0;23 dl.push(a);24 int ans=0;25 while(!dl.empty()){26 a=dl.top();27 dl.pop();28 for(int i=0;i<4;i++){29 b.x=a.x+disx[i];30 b.y=a.y+disy[i];31 b.ph=a.ph-1;32 b.step=a.step+1;33 if(b.x<0||b.y<0||b.x>=w||b.y>=h||map[b.x][b.y]==0)continue;34 if(b.ph<=0)continue;35 if(map[b.x][b.y]==3){36 ans=1;37 printf("%d\n",b.step);38 return;39 }40 if(map[b.x][b.y]==4)b.ph=6,map[b.x][b.y]=0;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,0);51 for(int y=0;y

 没用优先队列:

1 #include
2 #include
3 #include
4 #define mem(x,y) memset(x,y,sizeof(x)); 5 using namespace std; 6 int disx[4]={
1,-1,0,0}; 7 int disy[4]={
0,0,1,-1}; 8 int map[9][9]; 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
dl;17 a.x=sx;a.y=sy;18 a.ph=6;19 step=0;20 dl.push(a);21 int ans=0;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=0;i<4;i++){29 b.x=a.x+disx[i];30 b.y=a.y+disy[i];31 b.ph=a.ph-1;32 if(b.x<0||b.y<0||b.x>=w||b.y>=h||map[b.x][b.y]==0)continue;33 if(b.ph<=0)continue;34 if(map[b.x][b.y]==3){35 ans=1;36 printf("%d\n",step);37 return;38 }39 if(map[b.x][b.y]==4)b.ph=6,map[b.x][b.y]=0;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,0);51 for(int y=0;y

 

转载地址:http://fvifx.baihongyu.com/

你可能感兴趣的文章
分享:Backbone.js 样例站点与入门指南
查看>>
图的基本算法
查看>>
HTML基础(一)
查看>>
boost.circular_buffer简介
查看>>
Database Appliance并非Mini版的Exadata-还原真实的Oracle Unbreakable Database Appliance
查看>>
网页图片缩放(js)
查看>>
如何用Fiddler对Android应用进行抓包
查看>>
iOS为所需要的视图添加模糊效果--UIVisualEffectView
查看>>
HDU-1222 Wolf and Rabbit (欧几里得定理)
查看>>
Camera Calibration 相机标定:原理简介(五)
查看>>
ehcache实例
查看>>
python 匿名函数
查看>>
javascript实现-------------选择排序
查看>>
centOS中VMware Tools 安装
查看>>
oracle中以dba_、user_、v$_、all_、session_、index_开头的常...
查看>>
leetcode 116- Populating Next Right Pointers in Each Node
查看>>
spring项目启动错误——java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext...
查看>>
iOS开发网络篇—GET请求和POST请求
查看>>
字典dict
查看>>
游戏名词解释
查看>>