poj 1253 三维数组+广搜+减枝

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
const int N=51;
int A,B,C,step;
int map[N][N][N];
int visited[N][N][N];
int ds[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,-1},{0,0,1}}; 
struct Cube
{
       int x;
       int y;
       int z;
       int step;
}cube[N*N*N];
void bfs()
{
       int x,y,z,sp;
       queue<Cube> qu;
       Cube cub,next;
       cub.x=0;
       cub.y=0;
       cub.z=0;
       cub.step=0;
       if(A==1&&B==1&&C==1)
       {
              printf("0\n");
              return ;
       }             
       qu.push(cub);
       visited[0][0][0]=1;
       while(!qu.empty())
       {
              Cube cc=qu.front();
              qu.pop();
              for(int i=0;i<6;i++)
              {
                      
                      next.x=x=cc.x+ds[i][0];
                      next.y=y=cc.y+ds[i][1];
                      next.z=z=cc.z+ds[i][2];
                      next.step=sp=cc.step+1;
                      if(sp>step)
                      continue;
                      if(x>=0&&x<A&&y>=0&&y<B&&z>=0&&z<C&&map[x][y][z]==0&&visited[x][y][z]==0)
                      {
                             
                              if(x==A-1&&y==B-1&&z==C-1)
                              {
                                      printf("%d\n",sp);
                                      return ;
                                      
                              }                  
                              qu.push(next);
                              visited[x][y][z]=1;
                      }
              }
       }
       //system("pause");
       printf("-1\n"); 
}
int main()
{
       int T;
       scanf("%d",&T);
       while(T--)
       {
              memset(map,0,sizeof(map));
              memset(visited,0,sizeof(visited));   
              scanf("%d%d%d%d",&A,&B,&C,&step);
              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(A+B+C-3>step)
              {
                    printf("-1\n");
                    continue;
              }          
              bfs();
       }
       system("pause");
       return 0;
}
                           
                                                     
                                               
                                                                                              
                      
              
                                    
              
              
                         
       
                 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值