zoukankan      html  css  js  c++  java
  • HDU 1240——Asteroids!(三维BFS)POJ 2225——Asteroids

    普通的三维广搜,须要注意的是输入列,行,层

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    #define M 11
    using namespace std;
    
    int dir[6][3]={{0,1,0},{0,-1,0},{1,0,0},{-1,0,0},{0,0,1},{0,0,-1}};//6个方向
    int vis[M][M][M];
    char map[M][M][M];
    int n,m,p,sx,sy,sz,ex,ey,ez;
    
    bool ck(int x,int y,int z)
    {
        if(x>=0&&x<n&&y>=0&&y<n&&z>=0&&z<n&&!vis[z][x][y]&&map[z][x][y]=='O')
            return true;
        return false;
    }
    struct node
    {
        int x,y,z,st;
    };
    
    void bfs( )
    {
        if(sx==ex&&sy==ey&&sz==ez){
            cout<<n<<" "<<0<<endl;
            return ;
        }
        memset(vis,0,sizeof vis);
        queue<node> q;
        node a,b;
        a.z=sz,a.x=sx,a.y=sy,a.st=0;
        vis[sz][sx][sy]=1;
        q.push(a);
        while(!q.empty()){
            a=q.front(),q.pop();
            for(int i=0;i<6;++i){
                b.x=a.x+dir[i][0];
                b.y=a.y+dir[i][1];
                b.z=a.z+dir[i][2];
                b.st=a.st+1;
                if(!ck(b.x,b.y,b.z)) continue;
    
                if(b.x==ex&&b.y==ey&&b.z==ez){
                    cout<<n<<" "<<b.st<<endl;
                    return;
                }
                vis[b.z][b.x][b.y]=1;
                q.push(b);
    
            }
        }
        printf("NO ROUTE
    ");
        return ;
    
    }
    int main()
    {
        //freopen("input.txt","r",stdin);
        //freopen("output.txt","w",stdout);
        while(scanf("START %d",&n)!=EOF){
            for(int i=0;i<n;++i){
                for(int j=0;j<n;++j){
                    scanf("%s",map[i][j]);
                }
            }
    
            scanf("%d%d%d %d%d%d",&sy,&sx,&sz,&ey,&ex,&ez);
            char str[10];
            cin>>str;
            getchar();
    
            bfs( );
        }
        return 0;
    }<span style="color:#3333ff;">
    </span>




  • 相关阅读:
    [LeetCode] Kth Smallest Element in a BST
    Dojo入门篇
    不要小看了get 与set
    怎样安装Windows7操作系统
    MFC Wizard创建的空应用程序中各个文件内容的解析
    hadoop hdfs空间满后重新启动不了
    树形结构——基本原理
    R语言pdf输出中文乱码处理
    Javascript基本概念梳理
    Java动态代理
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5065581.html
Copyright © 2011-2022 走看看