zoj1671Walking Ant【bfs】
来源:互联网 发布:自学java多久能就业 编辑:程序博客网 时间:2024/05/17 13:05
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
#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespace std;const int maxn=10;struct Node{int hp,x,y,dist;};int n,m;int map[maxn][maxn];int mov[][2]={0,1,0,-1,1,0,-1,0};bool judge(int x,int y,int hp){return x>=0&&x<n&&y>=0&&y<m&&map[x][y]&&hp; }int bfs(int x,int y){int i,j,k;Node u;queue<Node>Q;u.x=x;u.y=y;u.hp=6;u.dist=0;Q.push(u);while(!Q.empty()){u=Q.front();Q.pop();Node v;for(i=0;i<4;++i){v.x=u.x+mov[i][0];v.y=u.y+mov[i][1];if(judge(v.x,v.y,u.hp-1)){v.dist=u.dist+1;v.hp=u.hp-1;if(map[v.x][v.y]==3)return v.dist;else if(map[v.x][v.y]==4){map[v.x][v.y]=1;v.hp=6;}Q.push(v);}}}return -1;}int main(){int i,j,k,sx,sy;while(scanf("%d%d",&m,&n),n||m){for(i=0;i<n;++i){for(j=0;j<m;++j){scanf("%d",&map[i][j]);if(map[i][j]==2){sx=i;sy=j;}}}printf("%d\n",bfs(sx,sy));}return 0;}
- zoj1671Walking Ant【bfs】
- 1671 Walking Ant【bfs】
- ZOJ Walking Ant【BFS】
- ZOJ 1671 Walking Ant (BFS)
- zoj 1671 Walking Ant 【BFS】
- ZOJ 1671 --Walking Ant【BFS】
- ZOJ 1671:Walking Ant【bfs】
- zoj 1671 Walking Ant 【BFS】
- zoj 1671 Walking Ant 【bfs】
- zju 1671 Walking Ant (bfs 暴搜题)
- zoj 1671 Walking Ant (BFS)
- BFS ZOJ problem-1671 Waking Ant
- [BZOJ 3872][POI 2014]Ant colony(BFS+二分)
- 蚂蚁回家(我是这样理解)Walking Ant (BFS)
- Zoj 1671 Walking Ant(BFS+优先队列||记忆化搜索)
- ZOJ 1671(广度搜索bfs)(Walking Ant)
- ant
- ant
- studio相关
- 在UITableView或UICollectionView的自定义的cell里添加手势或button,响应方法时如何传参数
- 安卓天气应用的练习
- CygWin、MinGw和Msys的区别
- PHP 5.3以下版本 无法用下标直接取得函数返回的数组
- zoj1671Walking Ant【bfs】
- web服务器、应用服务器、http服务器区别
- android studio项目如何导入到eclipse中
- Cocos2D实现RPG队伍菜单任意调整角色顺序的效果
- 少女的天空|光合故事
- 第一次使用 gitbash
- 转换日期格式 年/月/日 /星期
- Kafka的环境搭建
- 兰州交通大学多媒体技术复习