Walking Ant(bfs)

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<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 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<Node>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<h;y++)
52             for(int x=0;x<w;x++){
53                 scanf("%d",&map[x][y]);
54                 if(map[x][y]==2)sx=x,sy=y;
55             }
56         bfs(sx,sy);
57     }
58     return 0;
59 }

 没用优先队列:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 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<Node>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<h;y++)
52             for(int x=0;x<w;x++){
53                 scanf("%d",&map[x][y]);
54                 if(map[x][y]==2)sx=x,sy=y;
55             }
56         bfs(sx,sy);
57     }
58     return 0;
59 }

 

posted @ 2015-09-25 15:59  handsomecui  阅读(276)  评论(0编辑  收藏  举报