zoukankan      html  css  js  c++  java
  • HDU 1240 Asteroids!

    到比较拿手的搜索题,嗯,毕竟做的最多的就是搜索了.

    BFS,三维而已

    #include <iostream>    
    #include <queue>
    using namespace std;
    char map[11][11][11];
    int dir[6][3]={-1,0,0,0,1,0,1,0,0,0,-1,0,0,0,1,0,0,-1};
    struct point
    {
        int x;
        int y;
        int z;
        int step;
    }sta,end1;
    int n;
    bool islegal(int x,int y,int z)
    {
        return x>=0&&x<n&&y>=0&&y<n&&z>=0&&z<n&&map[z][x][y]!='X';
    }
    void BFS()
    {
        queue<point> Q;
        sta.step = 0;
        Q.push(sta);
        while(!Q.empty())
        {
            point now = Q.front();
            Q.pop();
            if(now.x==end1.x&&now.y==end1.y&&now.z==end1.z)
            {
                cout<<n<<" "<<now.step<<endl;
                return;
            }
            for(int i=0;i<6;i++)
            {
                int nextX = now.x + dir[i][0];
                int nextY = now.y + dir[i][1];
                int nextZ = now.z + dir[i][2];
                if(islegal(nextX,nextY,nextZ))
                {
                    point next = {nextX,nextY,nextZ,now.step+1};
                    map[nextZ][nextX][nextY]='X';
                    Q.push(next);
                }
            }
        }
        cout<<"NO ROUTE"<<endl;
    }
    
    int main(int argc, const char *argv[])
    {
        //freopen("input.txt","r",stdin);
        char a[10];
    
        while(cin>>a>>n)
        {
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                {
                    cin>>map[i][j];
                }
                cin>>sta.x>>sta.y>>sta.z;
                cin>>end1.x>>end1.y>>end1.z;
                cin>>a;
                BFS();
        }
        return 0;
    }
  • 相关阅读:
    二维数组实现01背包
    一维数组实现01背包
    js实现最长子串算法
    mysql__存储过程
    mysql 分页查询 limit
    转:严重: Exception loading sessions from persistent storage
    struts2---ValueStack对象
    struts2----ognl
    什么是JavaBean?
    EL表达式
  • 原文地址:https://www.cnblogs.com/destino74/p/3333861.html
Copyright © 2011-2022 走看看