题目连接:http://acm.nyist.edu.cn/JudgeOnline/status.php?pid=523
简单的搜索。
参考代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<algorithm>
using namespace std;
#define CLR(arr,v) memset(arr,v,sizeof(arr))
struct point
{
int x,y,z,v;
}mid;
struct point que[50000];
int head,total;
int m,n,c,t;
int map[55][55][55],mp[55][55][55];
const int d[6][3] = {{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};
void init()
{
CLR(map,0); CLR(mp,-1);
head = total = 0;
}
int scan()
{
char ch;
int m = 0;
while(1)
{
ch = getchar();
if(ch == ' ' || ch == '\n') return m;
m = m * 10 + ch - '0';
}
}
int BFS()
{
mid.x = 1;
mid.y = 1;
mid.z = 1;
mid.v = 0;
map[1][1][1] = 1;
que[total++] = mid;
int x,y,z,v,xx,yy,zz;
while(head < total)
{
struct point p = que[head++];
if(head >= 50000) head %= 50000;
x = p.x; y = p.y;
z = p.z; v = p.v;
if(v > t) return -1;
if(x == m && y == n && z == c) return v;
for(int i = 0;i < 6;++i)
{
mid.x = xx = d[i][0] + x;
mid.y = yy = d[i][1] + y;
mid.z = zz = d[i][2] + z;
if(map[xx][yy][zz] && (v + 1 < mp[xx][yy][zz] || mp[xx][yy][zz] == -1))
{
map[xx][yy][zz] = 0;
mid.v = v + 1;
mp[xx][yy][zz] = v + 1;
que[total++] = mid;
if(total >= 50000) total %= 50000;
}
}
}
return -1;
}
int main()
{
//freopen("1.txt","r",stdin);
//freopen("2.txt","w",stdout);
int T;
scanf("%d",&T);
getchar();
while(T--)
{
init();
c = scan();
m = scan();
n = scan();
t = scan();
int num1 = 0,num2 = 0;
for(int i = 1;i <= c;++i)
for(int j = 1;j <= m;++j)
for(int k = 1;k <= n;++k)
{
map[j][k][i] = scan() ^ 1;
num1 += map[j][k][i];
}
num2 = m + n + c - 3;
if(num1 < num2 || map[m][n][c] == 0)
{
printf("-1\n");
continue;
}
printf("%d\n",BFS());
}
//printf("%.5lf\n",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
421

被折叠的 条评论
为什么被折叠?



