HDU 1253 胜利大逃亡(三维BFS)

点我看题目

题意 : 中文题不详述。

思路 :因为还牵扯到层的问题,所以用三维的解决,不过这个还是很简单的BFS,六个方向搜一下就可以了,一开始交的时候老是超时,怎么改都不对,后来看了一个人写的博客,他说用C++交300ms,G++交600ms,我就改成C++交了,果然AC了,不过。。。。用了900ms。。。。。我也懒得改了,也不知道为什么G++交超时

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <queue>

using namespace std ;

int mapp[51][51][51] ;
bool vis[51][51][51] ;
int dir[6][3]  = {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}} ;
int A, B, C,T ;
int sum ;
struct node
{
    int x ;
    int y ;
    int z ;
    int timee ;
} temp,temp1 ;

void BFS()
{
    queue<node>que ;
    memset(vis,false,sizeof(vis)) ;
    vis[0][0][0] = true ;
    temp.x = 0 ,temp.y = 0,temp.z = 0 ;
    temp.timee = 0 ;
    que.push(temp) ;
    while(!que.empty())
    {
        node temp1 = que.front() ;
        que.pop() ;
        if(temp1.x == A-1 && temp1.y == B-1 && temp1.z == C-1)
        {
            sum = temp1.timee;
            return ;
        }
        for(int i = 0 ; i < 6 ; i++)
        {
            int xx = temp1.x+dir[i][0] ;
            int yy = temp1.y+dir[i][1] ;
            int zz = temp1.z+dir[i][2] ;
            if(!vis[xx][yy][zz] && mapp[xx][yy][zz] == 0 && xx >= 0 && xx < A && yy >= 0 && yy < B && zz >= 0 && zz < C)
            {
                temp.x = xx ;
                temp.y = yy ;
                temp.z = zz ;
                temp.timee=temp1.timee+1;
                vis[xx][yy][zz] = true ;
                que.push(temp) ;
            }
        }
    }
}
int main()
{
    int K ;
    scanf("%d",&K) ;
    while(K--)
    {
        sum = 99999999 ;
        memset(mapp,0,sizeof(mapp)) ;
        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",&mapp[i][j][k]) ;
        BFS();
        if(sum > T)
            printf("-1
") ;
        else printf("%d
",sum) ;
    }
    return 0 ;
}
View Code
原文地址:https://www.cnblogs.com/luyingfeng/p/3625861.html