迷宫问题 POJ - 3984(BFS + 打印路径)

题目点它→:https://vjudge.net/problem/POJ-3984

又是一个打印路径的迷宫,没有太多细节,已保证有唯一解,这里给出两种BFS存路径方法:

1.正向BFS求出最短路,记录每个点(可到达)到起点距离;反向BFS朝距离差为1的方向搜索到起点,然后递归倒序打印

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include<iomanip>
#include <stack>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
int dx[] = {1,-1, 0,0};
int dy[] = {0, 0,-1,1};
struct P {
    int x, y;
    P(int x = 0, int y = 0): x(x), y(y) {}
};
int a[5][5];
int d[5][5];
void bfs() {
    memset(d, INF, sizeof(d));
    d[0][0] = 0;
    queue<P> q;
    q.push(P(0,0));

    while(!q.empty()) {
        P t = q.front(); q.pop();
        if(t.x == 4 && t.y == 4) return;
        for(int i=0;i<4;i++) {
            int mx = t.x + dx[i];
            int my = t.y + dy[i];
            if(mx>=0 && mx<5 && my>=0 && my<5)
                if(a[mx][my] == 0 && d[mx][my] > d[t.x][t.y]+1) {
                    d[mx][my] = d[t.x][t.y] + 1;
                    q.push(P(mx, my));
                }
        }
    }
}

void rbfs(int r, int c) {
    for(int i=0;i<4;i++) {
        int mr = r+dx[i];
        int mc = c+dy[i];
        if(mr>=0 && mr<5 && mc>=0 && mc<5)
            if(d[mr][mc] == d[r][c] - 1) {
                rbfs(mr, mc);
                printf("(%d, %d)\n", mr, mc);
                return;
            }
    }
}
int main() {
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            cin>>a[i][j];
    bfs();

    rbfs(4,4);
    printf("(4, 4)\n");
    return 0;
}

2.正向BFS存下每个结点的唯一前结点,然后递归逆序打印,用前结点寻求目标

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include<iomanip>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
const int maxn = 1e4+10;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
int dx[] = {1,-1, 0,0};
int dy[] = {0, 0,-1,1};
 P {
    int x, y;
    P(int x = 0, int y = 0): x(x), y(y) {}
};
int a[5][5];
int vis[5][5];
P pre[5][5];
bool cmp (const void* p1, const void* p2) {
    return true;
}
void bfs() {
    memset(vis, 0, sizeof(vis)); vis[0][0] = 1;
    queue<P> q; q.push(P(0,0));
    while(!q.empty()) {
        P tmp = q.front(); q.pop();
        if(tmp.x == 4 && tmp.y == 4) return;
        for(int i = 0; i < 4; i++) {
            int mx = tmp.x + dx[i];
            int my = tmp.y + dy[i];
            if(mx>=0 && mx<5 && my>=0 && my<5 && !vis[mx][my] && !a[mx][my]) {
                vis[mx][my] = 1;
                q.push(P(mx, my));
                pre[mx][my] = tmp;
            }
        }
    }
}
void print(int x, int y) {
    if(x || y)
        print(pre[x][y].x, pre[x][y].y);
    printf("(%d, %d)\n", x, y);
    return;
}
int main() {//int T; cin >> T; getchar();
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            scanf("%d", &a[i][j]);
    bfs();
    print(4, 4);
    return 0;
}
发布了54 篇原创文章 · 获赞 43 · 访问量 1956

猜你喜欢

转载自blog.csdn.net/Jungle_st/article/details/104662106
今日推荐