hdoj1253

一题简直模板的
BFS,只是三维遍历而已。

#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string.h>
#include <math.h>
#include<stdlib.h>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
int A,B,C,t;
int dxy[7][3]=
{
    0,-1,0,
    0,1,0,
    1,0,0,
    -1,0,0,
    0,0,1,
    0,0,-1
};
int a[55][55][55];
int flag[55][55][55];

struct asd
{
    int x,y,z;
    int step;
} now,ne;

queue<asd>q;

void bfs()
{
    int i;
    memset(flag,0,sizeof(flag));
    while(!q.empty())
        q.pop();
    now.x=0;
    now.y=0;
    now.z=0;
    now.step=0;
    flag[0][0][0]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.step+1>t)
            break;
        for(i=0; i<6; i++)
        {
            int dx=now.x+dxy[i][0];
            int dy=now.y+dxy[i][1];
            int dz=now.z+dxy[i][2];
            if(dx==A-1&&dy==B-1&&dz==C-1&&now.step+1<=t)
            {
                printf("%d\n",now.step+1);
                return;
            }
            if(dx<0||dy<0||dz<0||dz>=C||dy>=B||dx>=A||flag[dx][dy][dz]||a[dx][dy][dz])
                continue;
            flag[dx][dy][dz]=1;
            ne.x=dx;
            ne.y=dy;
            ne.z=dz;
            ne.step=now.step+1;
            q.push(ne);
        }
    }
    printf("-1\n");
}

int main()
{
    int i,j,k,T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d",&A,&B,&C,&t);
        for(i=0; i<A; i++)
        {
            for(j=0; j<B; j++)
            {
                for(k=0; k<C; k++)
                    scanf("%d",&a[i][j][k]);
            }
        }
        bfs();
    }
    return 0;
}
posted @ 2016-02-25 16:31  see_you_later  阅读(156)  评论(0编辑  收藏  举报