您的位置:首页 > 其它

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

2014-08-15 23:51 302 查看
奇葩!这么简单的广搜居然爆内存了,而且一直爆,一直爆,Orz

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

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

//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 50*50*50+100;
int head, tail;
struct Point
{
int x, y, z;
int steps;
}qu[maxn];

int a, b, c, m;
int map[52][52][52];
int dir[6][3] = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};

bool islegal(int x, int y, int z)
{
return (x>=0 && x<a && y>=0 && y<b && z>=0 && z<c && (map[x][y][z] == 0));
}

void BFS(void)
{
qu[0].x = qu[0].y = qu[0].z = qu[0].steps = 0;
head = 0, tail = 1;
while(head < tail)
{
if(qu[head].steps > m)
{printf("-1\n");    return;}
if(qu[head].x==a-1 && qu[head].y==b-1 && qu[head].z==c-1)
{printf("%d\n", qu[head].steps);    return;}
for(int i = 0; i < 6; ++i)
{
int xx = qu[head].x + dir[i][0];
int yy = qu[head].y + dir[i][1];
int zz = qu[head].z + dir[i][2];
if(islegal(xx, yy, zz))
{
map[xx][yy][zz] = 1;
if(a+b+c-3-xx-yy-zz > m-qu[head].steps-1)    //优化
continue;
qu[tail].x = xx;
qu[tail].y = yy;
qu[tail].z = zz;
qu[tail++].steps = qu[head].steps + 1;
}
}
++head;
}
printf("-1\n");
}

int main(void)
{
#ifdef LOCAL
freopen("1253in.txt", "r", stdin);
#endif

int T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d%d", &a, &b, &c, &m);
for(int i = 0; i < a; ++i)
for(int j = 0; j < b; ++j)
for(int k = 0; k < c; ++k)
scanf("%d", &map[i][j][k]);

//map[0][0][0] = 1;
BFS();
}
return 0;
}


代码君
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: