亡命逃窜---三维搜索

呵呵,哈哈,一次A  !!!!    今年比赛就算我一个人去打 我也冲银保铜 !!!

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<map>
#include<cctype>
using namespace std;
int a,b,c,a1[55][55][55],visited[55][55][55],mark;
int b1[6][3]={1,0,0,-1,0,0,0,0,1,0,0,-1,0,1,0,0,-1,0};
struct node
{
    int x,y,z,step;
};
queue<node>Q;
void DFS(int x,int y,int z)
{
    node q={x,y,z,0};
    visited[x][y][z]=1;
    Q.push(q);
    while(!Q.empty())
    {
        node e=Q.front();
        Q.pop();
        for(int i=0;i<6;i++)
        {
            if(mark)
                return ;
            q.x=e.x+b1[i][0],q.y=e.y+b1[i][1],q.z=e.z+b1[i][2];
            if(q.x>=0&&q.x<a&&q.y>=0&&q.y<b&&q.z>=0&&q.z<c&&!visited[q.x][q.y][q.z]&&a1[q.x][q.y][q.z]!=1)   //  没有 超出 范围   并且 没有访问 且  不是墙
            {
                visited[q.x][q.y][q.z]=1;
                q.step=e.step+1;
                Q.push(q);
                if(a-1==q.x&&b-1==q.y&&q.z==c-1)
                {
                    a1[a-1][b-1][c-1]=q.step;
                    mark=1;
                }
            }
        }
    }
}
int main()
{
    int t,n;
    scanf("%d",&n);
    while(n--)
    {
        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 q=0;q<c;q++)
        {
            scanf("%d",&a1[i][j][q]);
        }
        memset(visited,0,sizeof(visited));
       // a1[a-1][b-1][c-1]=-1;
       mark=0;
        DFS(0,0,0);
        if(mark&&a1[a-1][b-1][c-1]<=t)
            printf("%d\n",a1[a-1][b-1][c-1]);
        else
            printf("-1\n");
    }
    return 0;
}

  `

posted @ 2016-03-29 11:11  X-POWER  阅读(175)  评论(0编辑  收藏  举报