hdu1072

广搜,遇到一次4就把它表为0,剩下时间为1时不要入队

 1 #include <stdio.h>
 2 #include<queue>
 3 using namespace std;
 4 int n,m;
 5 int map[10][10];
 6 int sx,sy;
 7 int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
 8 struct node
 9 {
10     int x,y;
11     int r,s;
12 };
13 
14 
15 node next,now;
16 
17 int bfs()
18 {
19     queue<node> q;
20     next.x=sx;next.y=sy;next.r=6;next.s=0;
21     map[sx][sy]=0;
22     q.push(next);
23     while (!q.empty())
24     {
25         now=q.front();
26         q.pop();
27         for (int i=0;i<4;i++)
28         {
29             next.x=now.x+dir[i][0];next.y=now.y+dir[i][1];
30             if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!=0&&now.r>1)
31             {
32                 if(map[next.x][next.y]==3)
33                     return now.s+1;
34                 else if(map[next.x][next.y]==1)
35                 {
36                     next.s=now.s+1;
37                     next.r=now.r-1;
38                     q.push(next);
39                 }
40                 else if(map[next.x][next.y]==4)
41                 {
42                     next.s=now.s+1;
43                     next.r=6;
44                     q.push(next);
45                     map[next.x][next.y]=0;
46                 }
47             }
48         }
49     }
50     return -1;
51     
52 }
53 int main(int argc, char *argv[])
54 {
55     int t;
56     scanf("%d",&t);
57     while (t--)
58     {
59         scanf("%d %d",&n,&m);
60         int i,j;
61         for(i=0;i<n;i++)
62         {
63             for(j=0;j<m;j++)
64             {
65                 scanf("%d",&map[i][j]);
66                 if(map[i][j]==2)
67                 {sx=i;sy=j;}
68             }
69         }
70         printf("%d\n",bfs());
71     }
72     return 0;
73 }
posted @ 2012-11-18 20:22  zerojetlag  阅读(346)  评论(0编辑  收藏  举报