一道三维搜索题,这是第二次写三维搜索题,本以为很简单的事情,不到20分钟就很随意的把代码写了出来。提交一次WA,继续提交,继续WA。后来各种修改,改了一下午!!后来看别人的博客才发现这个题要剪枝·····o(︶︿︶)o 唉,你下次能不能提示超时啊!!
运行结果:
Accepted | 1253 | 1124MS | 664K | 2230 B | C++ | Skymoon |
题目大意:又有一个悲剧的孩子被魔王抓走了(貌似最近一直在救这种可怜的娃)现在魔王出去了,这是一个逃亡的绝好机会。你的任务就是计算一下那个可怜的娃能不能在魔王的手底下逃出来。如果能逃出来就输出最小步数,不能就输出-1.这个题唯一需要注意的就是要剪枝,把特殊情况排除出去!!!!
原题地址:
代码如下:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std ;
int a, b, c, T;
int map[51][51][51] ;
int tur[6][3] = {-1, 0, 0, 1, 0, 0, 0, -1, 0, 0, 1, 0, 0, 0, 1, 0, 0, -1} ;
struct node
{
int x,y,z,step;
};
void bfs()
{
queue<node> q ;
node begin={0,0,0,0};
map[0][0][0] = 1 ;
q.push(begin) ;
while(!q.empty())
{
node p = q.front() ;
q.pop() ;
for(int i=0; i<6; i++)
{
node temp = p ;
temp.x += tur[i][0] ;
temp.y += tur[i][1] ;
temp.z += tur[i][2] ;
if(temp.x==a-1&&temp.y==b-1&&temp.z==c-1&&temp.step<=T)
{
printf("%d\n", temp.step+1) ;
return ;
}
if(temp.x>=0&&temp.x<a&&temp.y>=0&&temp.y<b&&temp.z>=0&&temp.z<c&&!map[temp.x][temp.y][temp.z])
{
map[temp.x][temp.y][temp.z] = 1 ;
temp.step++ ;
q.push(temp) ;
}
}
}
printf("-1\n") ;
return ;
}
int main()
{
int t ;
scanf("%d", &t) ;
while(t--){
scanf("%d%d%d%d", &a, &b, &c, &T) ;
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]) ;
if(map[a-1][b-1][c-1]||a+b+c-3>T)
{
printf("-1\n") ;
continue ;
}
if(a==1&&b==1&&c==1)
{
printf("0\n") ;
continue ;
}
bfs() ;
}
return 0 ;
}