hdu 1072 bfs

题目大意:一个走在迷宫中,没走一步身上的计数器+1,当到6的时候就爆炸,在数字为4的地方计数器可以回复到0,求最后逃出去的时间
0: 墙
1: 路
2: st
3: ed
4:回复
6分钟爆炸

 

以前的老题,属于比较基础的bfs了,这种类似的题,只要判断好状态,细心一点即可

 1 #include<cstdio>

 2 #include<iostream>

 3 #include<algorithm>

 4 #include<cstring>

 5 #include<cmath>

 6 #include<queue>

 7 #include<map>

 8 using namespace std;

 9 #define MOD 1000000007

10 const double eps=1e-5;

11 #define cl(a) memset(a,0,sizeof(a))

12 #define ts printf("*****\n");

13 const int MAXN=1005;

14 int n,m,tt;

15 int d[4][2]={1,0,0,1,-1,0,0,-1};

16 int a[MAXN][MAXN];

17 struct node   //以后结点表示统一用node

18 {

19     int x,y,t,s;

20     node(){};

21     node(int xx,int yy,int tt,int ss)

22     {

23         x=xx,y=yy,t=tt,s=ss;

24     }

25 }st,ed;

26 void bfs()

27 {

28     bool vis[MAXN][MAXN];

29     cl(vis);

30     queue<node> q;

31     node now,next;

32     q.push(node(st.x,st.y,6,0));

33     while(!q.empty())

34     {

35         now=q.front();

36         q.pop();

37         if(now.x==ed.x&&now.y==ed.y&&now.t>=1)

38         {

39             printf("%d\n",now.s);

40             return;

41         }

42         for(int i=0;i<4;i++)

43         {

44             next.x=now.x+d[i][0];

45             next.y=now.y+d[i][1];

46             next.t=now.t-1;

47             next.s=now.s+1;

48             if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&a[next.x][next.y]!=0&&next.t>0&&!vis[next.x][next.y])

49             {

50                 if(a[next.x][next.y]==4)

51                 {

52                     next.t=6;

53                     vis[next.x][next.y]=1;

54                 }

55                 q.push(next);

56             }

57         }

58     }

59     printf("-1\n");

60 }

61 int main()

62 {

63     int i,j,k;

64     #ifndef ONLINE_JUDGE

65     freopen("1.in","r",stdin);

66     #endif

67     scanf("%d",&tt);

68     while(tt--)

69     {

70         scanf("%d%d",&n,&m);

71         for(i=0;i<n;i++)

72         {

73             for(j=0;j<m;j++)

74             {

75                 scanf("%d",&a[i][j]);

76                 if(a[i][j]==2)  st.x=i,st.y=j;

77                 if(a[i][j]==3)  ed.x=i,ed.y=j;

78             }

79         }

80         bfs();

81     }

82 }

 

你可能感兴趣的:(HDU)