HDU 1253 胜利大逃亡(BFS)

来源:互联网 发布:淘宝哪家牛仔裤质量好 编辑:程序博客网 时间:2024/06/16 10:02
#include <iostream>#include <cstdlib>#include <cstdio>#include <queue>#include <cstring>using namespace std;struct node{    int x,y,z,step;};int ma[51][51][51];int A,B,C,T;int mv[6][3] = {{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};//bool vis[51][51][51];开标记数组爆了两次内存,果断删掉void BFS(){    queue<node>q;    node f,t;    f.x = 0; f.y = 0; f.z = 0; f.step = 0;    q.push(f);    ma[f.x][f.y][f.z] = 1;    while(!q.empty())    {        t = q.front();        q.pop();        if(t.x==A-1&&t.y==B-1&&t.z==C-1 && t.step <= T)        {            printf("%d\n",t.step);            return;        }        for(int i = 0;i<6;i++)        {            f.x = t.x + mv[i][0];            f.y = t.y + mv[i][1];            f.z = t.z + mv[i][2];            if(0<=f.x && f.x<A&& 0<=f.y&&f.y<B&&0<=f.z&&f.z<C &&ma[f.x][f.y][f.z]==0)            {                f.step = t.step + 1;                ma[f.x][f.y][f.z] = 1;                q.push(f);            }        }    }    puts("-1");}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",&ma[i][j][k]);                }            }        }        BFS();    }    return 0;}


0 0
原创粉丝点击