迷宮問題——BFS

来源:https://www.cnblogs.com/Solomon-xm/archive/2018/07/25/9364040.html
-Advertisement-
Play Games

改進版 BFS cpp include 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 ...


改進版

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("\n==============================================");
    for( int i = 1; i <= n; i++ )
    {
        for( int j = 1; j <= m; j++ )
            printf("%c",mp[i][j]);
        putchar('\n');
    }
    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;
}


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • // 6.2.4 組合使用構造函數和原型模式————創建自定義對象的方法: function Person(name, age, job) { this.name = name; this.age = age; this.job = job; this.friends = ["Shelby", "C ...
  • <!-- 解決圖片旋轉 --> <script src="/libs/jquery/jquery.min.js"></script> <script src="/libs/exif/exif.js"></script> // html <input type="file" accept="image ...
  • 緩存這個東西相信大家工作中都接觸得比較多,相應的在不同場景下也會遇到各種各樣的問題。下麵我列舉幾種可能會遇到的問題並提供一些解決建議。 1、如何把海量數據存放在緩存中並提供快速查詢 現實中我們的緩存通常都是以string,map,array,list,set,tree等具體的類型或者集合存放記憶體中, ...
  • 在簡單工廠模式中產品的創建統一在工廠類的靜態工廠方法中創建,體現了面形對象的封裝性,客戶程式不需要知道產品產生的細節,也體現了面向對象的單一職責原則(SRP),這樣在產品很少的情況下使用起來還是很方便, 但是如果產品很多,並且不斷的有新產品加入,那麼就會導致靜態工廠方法變得極不穩定,每次加入一個新產 ...
  • guava是google的一個開源java框架,其github地址是 https://github.com/google/guava。guava工程包含了若幹被Google的 Java項目廣泛依賴的核心庫,例如:集合 [collections] 、緩存 [caching] 、原生類型支持 [prim ...
  • 主要使用三種集成環境進行安裝,小主們可以自行選擇: ...
  • 一、xxxxxx獲取指定任務爬取的所有url的介面 介面名稱:xxxxxx獲取指定任務爬取的所有url的介面 訪問鏈接: http://IP:PORT/crwalTask/findUrlExceptionById?ctId=ctIdVal&time=timeVal&limit=limitVal 傳入 ...
  • 頁面代碼: <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePat ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...