zoukankan      html  css  js  c++  java
  • zoj1940(三维广搜)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=940

    分析:三维其实就是六个方向地搜索,思维清晰且细心点,很快就AC了。

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <cstdlib>
    #include <vector>
    #include <set>
    #include <map>
    #define LL long long
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define N 100010
    using namespace std;
    struct node
    {
        int x,y,z,step;
    };
    struct move
    {
        int l,r,c;
    }mo[6]={
        {0,0,1},{0,0,-1},{0,1,0},
        {0,-1,0},{1,0,0},{-1,0,0}
    };
    char s[35][35][35];
    int vis[35][35][35];
    int L,R,C;
    node make_node(int a,int b,int c,int x)
    {
        node temp;
        temp.x=a,temp.y=b;
        temp.z=c;temp.step=x;
        return temp;
    }
    queue<node>que;
    bool judge(int a,int b,int c)
    {
        return a>=0&&a<L&&b>=0&&b<R&&c>=0&&c<C&&s[a][b][c]!='#';
    }
    int bfs(int x,int y,int z)
    {
        memset(vis,0,sizeof(vis));
        while(!que.empty())que.pop();
        que.push(make_node(x,y,z,0));
        vis[x][y][z]=1;
        while(!que.empty())
        {
            node now=que.front();que.pop();
            for(int i=0;i<6;i++)
            {
                int a=now.x+mo[i].l,b=now.y+mo[i].r,c=now.z+mo[i].c,x=now.step;
                if(!vis[a][b][c]&&judge(a,b,c))
                {
                    que.push(make_node(a,b,c,x+1));
                    vis[a][b][c]=1;
                    if(s[a][b][c]=='E')
                    {
                        return x+1;
                    }
                }
            }
        }
        return -1;
    }
    int main()
    {
        int ans,x,y,z;
        while(scanf("%d%d%d",&L,&R,&C)&&L)
        {
            for(int i=0;i<L;i++)
            for(int j=0;j<R;j++)
            {
                scanf("%s",s[i][j]);
                for(int k=0;k<C;k++)
                {
                    if(s[i][j][k]=='S')
                    {
                        x=i;y=j;z=k;
                    }
                }
            }
            int res=bfs(x,y,z);
            if(res!=-1)printf("Escaped in %d minute(s).
    ",res);
            else puts("Trapped!");
        }
    }
    View Code
  • 相关阅读:
    cad三维多断线的合并
    将list中的每个元素转换成str
    ndarray格式的点云数组转变为open3d.open3d.geometry.PointCloud
    汉字读音积累
    python文件内的函数调用
    numpy.dot()函数
    父亲啊,儿子是您永远的牵挂
    2008江西高考0分作文
    记忆一个朋友
    暴风雨前的天空
  • 原文地址:https://www.cnblogs.com/lienus/p/4167204.html
Copyright © 2011-2022 走看看