POJ 2251 Dungeon Master(BFS)

题目链接

 这个题,真郁闷啊!!!!昨天折腾了晚上写了个DFS。。。交了三次都超时,今天写BFS,折腾了一晚上 一个i打成start 我硬是没发现那里错了,这么大的错误,竟然过了很多数据,受不了啊。。。自己要认真认真认真啊!!!!!!

 1 #include <stdio.h>

 2 #include <stdlib.h>

 3 #include <string.h>

 4 char p[31][31][31];

 5 int ll[100001],rr[100001],cc[100001];

 6 int endl,endr,endc,l,r,c;

 7 int main()

 8 {

 9     int i,j,k,start,end,z,num;

10     int o[31][31][31];

11     int a1[7]={0,0,0,0,1,-1};

12     int a2[7]={0,0,1,-1,0,0};

13     int a3[7]={1,-1,0,0,0,0};

14     char ko[1000];

15     while(scanf("%d%d%d%*c",&l,&r,&c)!=EOF)

16     {

17         z = 0; start = end = 0;num = 0;

18         memset(o,0,sizeof(o));

19         memset(ll,0,sizeof(ll));

20         memset(rr,0,sizeof(rr));

21         memset(cc,0,sizeof(cc));

22         if(l == 0 && r == 0 && c == 0 )

23         break;

24         for(i = 0;i <= l-1;i ++)

25        {

26         for(j = 0;j <= r-1;j ++)

27         gets(p[i][j]);

28         gets(ko);

29        }

30         for(i = 0;i <= l-1;i ++)

31         for(j = 0;j <= r-1;j ++)

32         for(k = 0;k <= c-1;k ++)

33         {

34             if(p[i][j][k] == 'S')

35             {

36                 ll[0] = i ;

37                 rr[0] = j ;

38                 cc[0] = k ;

39                 o[i][j][k] = 1;

40             }

41             if(p[i][j][k] == 'E')

42             {

43                 endl = i ;

44                 endr = j ;

45                 endc = k ;

46             }

47             if(p[i][j][k] == '#')

48             {

49                 o[i][j][k] = 1;

50             }

51         }

52         while(start <= end)

53         {

54             j = 1;

55             for(i = start;i <= end;i ++)

56             {

57                for(k = 0;k <= 5;k ++)

58                {

59                    if(ll[i]+a1[k]>=0&&ll[i]+a1[k]<=l-1&&rr[i]+a2[k]>=0&&rr[i]+a2[k]<=r-1&&cc[i]+a3[k]>=0&&cc[i]+a3[k]<=c-1)

60                    {

61                        if(o[ll[i]+a1[k]][rr[i]+a2[k]][cc[i]+a3[k]] == 0)

62                        {

63                            ll[end + j] = ll[i]+a1[k];

64                            rr[end + j] = rr[i]+a2[k];

65                            cc[end + j] = cc[i]+a3[k];

66                            o[ll[i]+a1[k]][rr[i]+a2[k]][cc[i]+a3[k]] = 1;

67                            j++;

68                        }

69                    }

70                }

71             }

72             num ++;

73             if(o[endl][endr][endc])

74             {

75                 z = 1;

76                 break;

77             }

78             start = end + 1;

79             end = end + j - 1;

80         }

81         if(!z)

82         printf("Trapped!\n");

83         else

84         printf("Escaped in %d minute(s).\n",num);

85     }

86     return 0;

87 }

 

你可能感兴趣的:(master)