HDU 1253 胜利大逃亡 (BFS)

题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10953

代码:

#include<stdio.h>
#include<string.h>
#include<queue>

using namespace std;

struct node
{
    int x,y,z;
    int step;
} ui,op;
int T,a,b,c;
int maps[55][55][55];
int book[55][55][55];
int fx[6]= {1,-1,0,0,0,0};
int fy[6]= {0,0,1,-1,0,0};
int fz[6]= {0,0,0,0,1,-1};
int flag=1;

void bfs(int x,int y,int z)
{
    memset(book,0,sizeof(book));
    ui.x=x;
    ui.y=y;
    ui.z=z;
    book[x][y][z]=1;
    ui.step=0;
    queue<node>q;
    q.push(ui);

    while(!q.empty())
    {
        ui=q.front();
        q.pop();

        if(ui.x==a-1&&ui.y==b-1&&ui.z==c-1&&ui.step<=T)
        {
            printf("%d\n",ui.step);
            return;
        }

        for(int i=0; i<6; i++)
        {
            op.x=ui.x+fx[i];
            op.y=ui.y+fy[i];
            op.z=ui.z+fz[i];

            //printf("%d %d %d\n",op.x,op.y,op.z);
            if(op.x>=0&&op.x<a&&op.y>=0&&op.y<b&&op.z>=0&&op.z<c)
            {
                if(maps[op.x][op.y][op.z]==0)
                {
                    if(book[op.x][op.y][op.z]==0)//&&ui.step-1<T)
                    {
                        op.step=ui.step+1;
                        book[op.x][op.y][op.z]=1;
                        q.push(op);
                    }
                }
            }
        }
    }
    printf("-1\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(maps,0,sizeof(maps));
        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",&maps[i][j][k]);
                    }
                }
            }

            bfs(0,0,0);
        }
    }
}

刚开始画蛇添足了,WA了几发。改了一下,就过了。

你可能感兴趣的:(HDU 1253 胜利大逃亡 (BFS))