hdu 杭电 1253 胜利大逃亡 果枫

题意:从,左上后角->右上前角,的最短时间是否满足题目给出的时间,满足输出最短时间,否输出-1.

      输入数据比较难理解:

      A B C可以理解为对应输入的A->x轴的单位长度,B->z轴的单位长度(方向向向下),C->y轴的单位长度.

      注:左上后角作为坐标原点。



3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1

//第一块
1 1 1 1
1 0 0 1
0 1 1 1

//第二块
0 0 0 0
0 1 1 0
0 1 1 0

//第三块

解法:广搜,队列

峰注:进队列后必须马上标记。

ac代码:

View Code
#include<iostream>
#include<stdio.h>
#include<queue>
using namespace std;

struct node
{
    int x,y,z;    
    int t;
};


const int m=51;
int x,y,z,t;
int map[m][m][m],dir[6][3]={ 0, 0, 1,
0, 0,-1,
0, 1, 0,
0,-1, 0,
1, 0, 0,
-1, 0, 0}; //正方体六个面的方向,注:z轴方向:向下
// 方向:下,上,右,左,前,后



int check(int i,int j,int k)      //判断是否出界,(即是否在正方体内)
{
    if(i>=0&&i<x&&j>=0&&j<y&&k>=0&&k<z)
    {
        return 1;
    }
    else
        return 0;
}


int main()
{
    queue<node>q;
    node temp;
    int n;
    int i,j,k;
    while(cin>>n)
    {
        for(int l=0;l<n;l++)
        {
            cin>>x>>y>>z>>t;

            for(i=0;i<x;i++)          //三个for()循环输入
            {
                for(j=0;j<y;j++)
                {
                    for(k=0;k<z;k++)
                    {
                        scanf("%d",&map[i][j][k]);
                    }
                }
            }


            temp.x=0;
            temp.y=0;
            temp.z=0;
            temp.t=0;
            q.push(temp);             //坐标原点进队列,初始化
            map[q.front().x][q.front().y][q.front().z]=1;  //特别注意:进队列后必须标记为墙


            while(!q.empty())
            {
                if(q.front().t<t&&q.front().x==x-1&&q.front().y==y-1&&q.front().z==z-1)
                {
                    cout<<q.front().t<<endl;
                    break;
                }         //(x-1,y-1,z-1)即右下前坐标,满足提前跳出循环



                for(int a=0;a<6;a++)   
                {
                    i=q.front().x+dir[a][0];
                    j=q.front().y+dir[a][1];
                    k=q.front().z+dir[a][2];
                    if(check(i,j,k))
                    {   
                        if(map[i][j][k]==0)
                        {
                            temp.x=i;
                            temp.y=j;
                            temp.z=k;
                            temp.t=q.front().t+1;
                            map[i][j][k]=1;         //特别注意:进队列后必须标记为墙
                            q.push(temp);
                        }
                    }
                }        //一个立方体有六个方向,第一个if()判断是否出界,
                //第二个if()判断是否为墙

                map[q.front().x][q.front().y][q.front().z]=1;
                q.pop();
            }

            if(q.empty())
                cout<<"-1"<<endl;
            else
            {
                while(!q.empty())
                {
                    q.pop();
                }
            }
        }//end-for()
    }//end-while
    return 0;
}








注:不懂请留言。
原文地址:https://www.cnblogs.com/zgfailmr/p/2665877.html