zoukankan      html  css  js  c++  java
  • 迷宫问题——BFS

    改进版

    BFS


    #include <bits/stdc++.h>
    using namespace std;
    #define coordi(x,y) ( m*(x-1)+y )
    const int maxn = 30;
    const int dx[] = {0,0,1,-1};
    const int dy[] = {1,-1,0,0};
    
    int mp[maxn+10][maxn+10];
    int nxtx[maxn+10][maxn+10];
    int nxty[maxn+10][maxn+10];
    bool vis[maxn+10][maxn+10];
    int fa[(maxn+10)*(maxn+10)];
    int n , m;
    int stx , sty , edx , edy;
    
    inline int check( int x , int y )
    {
        return 1<=x && x<=n && 1<=y && y<=m;
    }
    inline void print_map()
    {
        puts("
    ==============================================");
        for( int i = 1; i <= n; i++ )
        {
            for( int j = 1; j <= m; j++ )
                printf("%c",mp[i][j]);
            putchar('
    ');
        }
        puts("==============================================");
    }
    // 并查集
    int getfa( int x )
    {
        return x==fa[x]?x:fa[x] = getfa(fa[x]);
    }
    void unio( int a , int b )
    {
        int fx = getfa(a) , fy = getfa(b);
        if ( fx != fy ) fa[fx] = fy;
    }
    // 并查集
    
    void connect()
    {
        int t = n*m/3*2;
        for( int i = 1; i <= n*m; i++ ) fa[i] = i;
        int fs = getfa(coordi(stx,sty)) , ft = getfa(coordi(edx,edy));
        while( fs != ft || t > 0 )
        {
            t--;
            int px = rand()%n+1 , py = rand()%m+1;
            if ( mp[px][py] == 'X' )
            {
                mp[px][py] = '#';
                for( int k = 0 ; k< 4; k++ )
                {
                    int xx = px + dx[k] , yy = py + dy[k];
                    if ( check(xx,yy) && mp[xx][yy] != 'X' ) unio( coordi(px,py) , coordi(xx,yy) );
                }
            }
            fs = getfa(coordi(stx,sty)) , ft = getfa(coordi(edx,edy));
        }
    }
    
    void init()
    {
        srand(time(0));
        n = rand()%maxn+10;
        m = rand()%maxn+10;
        cout<<"map size : "<<n<<" * "<<m<<endl;
        for( int i = 1; i <= n; i++ )
            for( int j = 1; j <= m; j++ ) mp[i][j] = 'X';
        stx = rand()%n+1 , sty = rand()%m+1;
        edx = rand()%n+1 , edy = rand()%m+1;
        while( abs(edx-stx) + abs(edy-sty) <= 1 ) edx = rand()%n+1 , edy = rand()%m+1;
        mp[stx][sty] = 'S' , mp[edx][edy] = 'T';
        cout<<"start:("<<stx<<","<<sty<<")"<<endl;
        cout<<"end:("<<edx<<","<<edy<<")"<<endl;
        connect();
        print_map();
    }
    
    void print_path() // path = '*'  st = S , ed = T   ,  road = ## wall =  X
    {
        int x = edx , y = edy;
        while( !( x == stx && y == sty ) )
        {
            mp[x][y] = '*';
            int tx = nxtx[x][y];
            y = nxty[x][y];
            x = tx;
        }
        mp[edx][edy] = 'T';
        print_map();
    }
    
    void bfs()
    {
        queue< pair<int,int> > q;
        q.push( make_pair(stx,sty) );
        memset(vis,0,sizeof(vis));
        vis[stx][sty] = true;
        while( !q.empty() )
        {
            pair<int,int> temp = q.front();
            q.pop();
            if ( temp.first == edx && temp.second == edy )
            {
                print_path();
                return;
            }
            for( int k = 0; k < 4; k++ )
            {
                int xx = temp.first + dx[k] , yy = temp.second + dy[k];
                if ( !check(xx,yy) || vis[xx][yy] || mp[xx][yy] == 'X' ) continue;
                vis[xx][yy] = 1 , nxtx[xx][yy] = temp.first , nxty[xx][yy] = temp.second;
                q.push( make_pair(xx,yy) );
            }
        }
    }
    
    int main()
    {
        init();
        bfs();
        return 0;
    }
    
    

    宁鸣默笙
  • 相关阅读:
    [转]Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
    [转]OAuth 2.0
    SpringMVC之七:SpringMVC中使用Interceptor拦截器
    多数据源问题--Spring+Ibatis 访问多个数据源(非分布式事务)
    读写分离
    SVN中检出(check out) 和 导出(export) 的区别
    Hbase之三:Hbase Shell使用入门
    hadoop之一:概念和整体架构
    Twitter Storm如何保证消息不丢失
    Twitter Storm: storm的一些常见模式
  • 原文地址:https://www.cnblogs.com/Solomon-xm/p/9364040.html
Copyright © 2011-2022 走看看