HDU 1253 (简单三维广搜) 胜利大逃亡

奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz

而且我也优化过了的啊,尼玛还是一直爆!

先把代码贴上睡觉去了,明天再来弄

 

 1 //#define LOCAL

 2 #include <iostream>

 3 #include <cstdio>

 4 #include <cstring>

 5 #include <queue>

 6 #include <cmath>

 7 using namespace std;

 8 

 9 struct Point

10 {

11     int x, y, z;

12     int steps;

13 }start;

14 

15 int a, b, c, m;

16 int map[52][52][52];

17 int dir[6][3] = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};

18 

19 bool islegal(int x, int y, int z)

20 {

21     return (x>=0 && x<a && y>=0 && y<b && z>=0 && z<c && (map[x][y][z] == 0));

22 }

23 

24 void BFS(void)

25 {

26     queue<Point> qu;

27     start.x = start.y = start.z = start.steps = 0;

28     map[0][0][0] = 1;

29     qu.push(start);

30     while(!qu.empty())

31     {

32         Point fir = qu.front();

33         qu.pop();

34         if(fir.x==a-1 && fir.y==b-1 && fir.z==c-1)

35             {printf("%d\n", fir.steps);    return;}

36         if(fir.steps > m)

37             {printf("-1\n");    return;}

38         for(int i = 0; i < 6; ++i)

39         {

40             int xx = fir.x + dir[i][0];

41             int yy = fir.y + dir[i][1];

42             int zz = fir.z + dir[i][2];

43             if(islegal(xx, yy, zz))

44             {

45                 Point next;

46                 next.x = xx, next.y = yy, next.z = zz;

47                 next.steps = fir.steps + 1;

48                 map[zz][xx][yy] = 1;

49                 if(abs(xx-a+1)+abs(yy-b+1)+abs(zz-c+1)+next.steps > m)

50                     continue;

51                 qu.push(next);

52             }

53         }

54     }

55     printf("-1\n");

56 }

57 

58 int main(void)

59 {

60     #ifdef LOCAL

61         freopen("1253in.txt", "r", stdin);

62     #endif

63 

64     int T;

65     scanf("%d", &T);

66     while(T--)

67     {

68         scanf("%d%d%d%d", &a, &b, &c, &m);

69         for(int i = 0; i < a; ++i)

70             for(int j = 0; j < b; ++j)

71                 for(int k = 0; k < c; ++k)

72                     scanf("%d", &map[i][j][k]);

73 

74         BFS();

75     }

76     return 0;

77 }
代码君

 

最终还是自己用数组模拟队列A过去了

一个优化:

因为题目要求在规定时间内走出迷宫,如果出口距离当前位置太远(即使在没有墙壁阻挡的情况下也走不到),那么就可以直接输出-1了

这个优化让900+MS的运行时间减少到了500+MS,还是比较给力的

 1 //#define LOCAL

 2 #include <iostream>

 3 #include <cstdio>

 4 #include <cstring>

 5 using namespace std;

 6 

 7 const int maxn = 50*50*50+100;

 8 int head, tail;

 9 struct Point

10 {

11     int x, y, z;

12     int steps;

13 }qu[maxn];

14 

15 int a, b, c, m;

16 int map[52][52][52];

17 int dir[6][3] = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};

18 

19 bool islegal(int x, int y, int z)

20 {

21     return (x>=0 && x<a && y>=0 && y<b && z>=0 && z<c && (map[x][y][z] == 0));

22 }

23 

24 void BFS(void)

25 {

26     qu[0].x = qu[0].y = qu[0].z = qu[0].steps = 0;

27     head = 0, tail = 1;

28     while(head < tail)

29     {

30         if(qu[head].steps > m)

31             {printf("-1\n");    return;}

32         if(qu[head].x==a-1 && qu[head].y==b-1 && qu[head].z==c-1)

33             {printf("%d\n", qu[head].steps);    return;}

34         for(int i = 0; i < 6; ++i)

35         {

36             int xx = qu[head].x + dir[i][0];

37             int yy = qu[head].y + dir[i][1];

38             int zz = qu[head].z + dir[i][2];

39             if(islegal(xx, yy, zz))

40             {

41                 map[xx][yy][zz] = 1;

42                 if(a+b+c-3-xx-yy-zz > m-qu[head].steps-1)    //优化

43                     continue;

44                 qu[tail].x = xx;

45                 qu[tail].y = yy;

46                 qu[tail].z = zz;

47                 qu[tail++].steps = qu[head].steps + 1;

48             }

49         }

50         ++head;

51     }

52     printf("-1\n");

53 }

54 

55 int main(void)

56 {

57     #ifdef LOCAL

58         freopen("1253in.txt", "r", stdin);

59     #endif

60 

61     int T;

62     scanf("%d", &T);

63     while(T--)

64     {

65         scanf("%d%d%d%d", &a, &b, &c, &m);

66         for(int i = 0; i < a; ++i)

67             for(int j = 0; j < b; ++j)

68                 for(int k = 0; k < c; ++k)

69                     scanf("%d", &map[i][j][k]);

70 

71         //map[0][0][0] = 1;

72         BFS();

73     }

74     return 0;

75 }
代码君

 

你可能感兴趣的:(HDU)