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
题意:在一个N*M的地图上,有5个数字。0表示此处不可走,1表示此处可走,3是蚂蚁的洞穴,2是蚂蚁开始走的位置,4表示此处有食物。现有一个蚂蚁要回家,它初始只有6HP,且每走一步会减一,当HP为0时就会死去,当它到达食物处可以恢复满HP且该食物可以无限使用。问蚂蚁最少需要多少步才能到达洞穴,如果到不了输出-1。
思路:BFS + 优先队列。主要 第一次碰到食物就要把食物从图中消除掉(防止死循环),因为第一次补给食物后回不了洞穴,以后再到该位置补给食物也是无济于事的。
AC代码:
#include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; struct Node { int x, y, step, HP; friend bool operator < (Node a, Node b) { return a.step > b.step; } }; int Map[10][10]; int N, M; int sx, sy; void getMap() { for(int i = 0; i < N; i++) { for(int j = 0; j < M; j++) { scanf("%d", &Map[i][j]); if(Map[i][j] == 2) sx = i, sy = j; } } } bool judge(int x, int y) { return x >= 0 && x < N && y >= 0 && y < M && Map[x][y]; } void BFS(int x, int y) { priority_queue<Node> Q; int move[4][2] = {0,1, 0,-1, 1,0, -1,0}; Node now, next; now.x = x; now.y = y; now.step = 0; now.HP = 6; Q.push(now); while(!Q.empty()) { now = Q.top(); Q.pop(); if(Map[now.x][now.y] == 3) { printf("%d\n", now.step); return ; } if(now.HP == 1) continue;//HP到1 还未到洞穴,这条路已经死了 for(int k = 0; k < 4; k++) { next.x = now.x + move[k][0]; next.y = now.y + move[k][1]; next.step = now.step + 1; if(judge(next.x, next.y)) { if(Map[next.x][next.y] == 4) { next.HP = 6; Map[next.x][next.y] = 1;//去掉食物 } else next.HP = now.HP - 1; Q.push(next); } } } printf("-1\n"); } int main() { while(scanf("%d%d", &M, &N), M||N) { getMap(); BFS(sx, sy); } return 0; }