poj3984 迷宮問題(簡單的輸出路徑的bfs)


題目鏈接 http://poj.org/problem?id=3984

中文題題意不解釋了

反正就是簡單的結構體套結構體存一下路徑就行了

 

#include <iostream>
#include <cstring>
#include <deque>
#include <queue>
using namespace std;
int map[6][6];
struct ss {
    int x , y;
};
struct TnT {
    deque<ss>mm;
};
int vis[6][6] , dr[4][2] = {1 , 0 , -1 , 0 , 0 , 1 , 0 , -1};
TnT bfs(int x , int y) {
    memset(vis , 0 , sizeof(vis));
    TnT gg;
    ss gl;
    gl.x = x , gl.y = y;
    vis[x][y] = 1;
    gg.mm.push_back(gl);
    queue<TnT>q;
    q.push(gg);
    while(!q.empty()) {
        TnT ml = q.front();
        ss xg = ml.mm.back();
        if(xg.x == 4 && xg.y == 4) {
            return ml;
        }
        for(int i = 0 ; i < 4 ; i++) {
            TnT sg = ml;
            ss xl = ml.mm.back();
            xl.x += dr[i][0];
            xl.y += dr[i][1];
            if(xl.x >= 0 && xl.x < 5 && xl.y >= 0 && xl.y < 5 && !map[xl.x][xl.y] && !vis[xl.x][xl.y]) {
                vis[xl.x][xl.y] = 1;
                sg.mm.push_back(xl);
                q.push(sg);
            }
        }
        q.pop();
    }
    return q.front();
}
int main()
{
    for(int i = 0 ; i < 5 ; i++) {
        for(int j = 0 ; j < 5 ; j++) {
            cin >> map[i][j];
        }
    }
    TnT q = bfs(0 , 0);
    while(!q.mm.empty()) {
        ss sl = q.mm.front();
        q.mm.pop_front();
        cout << "(" << sl.x << ", " << sl.y << ")" << endl;
    }
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM