HDU–1253题 胜利大逃亡

一道三维搜索题,这是第二次写三维搜索题,本以为很简单的事情,不到20分钟就很随意的把代码写了出来。提交一次WA,继续提交,继续WA。后来各种修改,改了一下午!!后来看别人的博客才发现这个题要剪枝·····o(︶︿︶)o 唉,你下次能不能提示超时啊!!

运行结果:

Accepted 1253 1124MS 664K 2230 B C++ Skymoon

题目大意:又有一个悲剧的孩子被魔王抓走了(貌似最近一直在救这种可怜的娃)现在魔王出去了,这是一个逃亡的绝好机会。你的任务就是计算一下那个可怜的娃能不能在魔王的手底下逃出来。如果能逃出来就输出最小步数,不能就输出-1.这个题唯一需要注意的就是要剪枝,把特殊情况排除出去!!!!

原题地址:

HDU–1253(点击打开)

代码如下:

#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 ;
}




  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值