广搜2 输出路径&&三维广搜总结

· · 个人记录

T628045 迷宫问题

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

P mk(int x, int y) {
    return make_pair(x, y);
}

void bfs(int m[5][5]) {
    queue<P> q;
    vector<vector<bool>> v(5, vector<bool>(5, false));
    unordered_map<P, P, function<size_t(P)>> p(
        10, [](const P& a) { return a.first * 5 + a.second; });

    P s = mk(0, 0);
    P e = mk(4, 4);

    q.push(s);
    v[s.first][s.second] = true;

    while (!q.empty()) {
        P c = q.front();
        q.pop();

        if (c == e) break;

        for (int i = 0; i < 4; ++i) {
            int nx = c.first + dx[i];
            int ny = c.second + dy[i];

            if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && !v[nx][ny] && m[nx][ny] == 0) {
                v[nx][ny] = true;
                P n = mk(nx, ny);
                p[n] = c;
                q.push(n);
            }
        }
    }

    vector<P> pt;
    P cur = e;
    while (cur != s) {
        pt.push_back(cur);
        cur = p[cur];
    }
    pt.push_back(s);
    reverse(pt.begin(), pt.end());

    for (auto x : pt) {
        cout << "(" << x.first << ", " << x.second << ")" << endl;
    }
}

int main() {
    int maze[5][5] = {
        {0,1,0,0,0},
        {0,1,0,1,0},
        {0,0,0,0,0},
        {0,1,1,1,0},
        {0,0,0,1,0}
    };

    bfs(maze);
    return 0;
}

T628044 迷宫之输出路径-困难版

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,a[5005][5005],vis[5005][5005],u[4]={1,0,0,-1},v[4]={0,-1,1,0},kx,ky,jx,jy;
struct node{
    int x;
    int y;
};
struct Node{
    int x;
    int y;
    char c;
};
queue<node>q;
Node pp[5005][5005];
void dfs(int xx,int yy)
{
    if(xx==0&&yy==0)
    {
        return;
    }
    dfs(pp[xx][yy].x,pp[xx][yy].y);
    cout<<pp[xx][yy].c;
}
int main()
{
    cin>>n>>m;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            char cc;
            cin>>cc;
            a[i][j]=cc-'0';
        }
    }
    q.push({0,0});
    vis[0][0]=1;
    while(!q.empty())
    {
        node f=q.front();
        q.pop();
        if(f.x==n-1&&f.y==m-1)
        {
            dfs(n-1,m-1);
            return 0;
        }
        for(int i=0;i<4;i++)
        {
            ll p=f.x+u[i],qq=f.y+v[i];
            if(p>=0&&qq>=0&&qq<m&&p<n&&a[p][qq]==0&&vis[p][qq]==0)
            {
                vis[p][qq]=1;
                if(u[i]==1)
                {
                    pp[p][qq]={f.x,f.y,'D'};
                }
                if(u[i]==-1)
                {
                    pp[p][qq]={f.x,f.y,'U'};
                }
                if(v[i]==1)
                {
                    pp[p][qq]={f.x,f.y,'R'};
                }
                if(v[i]==-1)
                {
                    pp[p][qq]={f.x,f.y,'L'};
                }
                q.push({p,qq});
            }
        }
    }
        cout<<-1;
    return 0;
}

B3625 迷宫寻路

#include <bits/stdc++.h>
using namespace std;
int n;
int m;
char a[100][100];
int g = 0;
void dfs(int x, int y) {
    if(a[x][y]=='#' or x>n or y>m or x<=0 or y<=0){
        return;
    }
    if(g==1){
        return ;
    }
    if (x == n && y == m) {
        g = 1;
        return ;
    }
    a[x][y] = '#';
    dfs(x, y + 1);
    dfs(x + 1, y);
    dfs(x, y - 1);
    dfs(x - 1, y);
}
int main() {
    cin >> n;
    cin >> m;
    for (int j = 1; j <= n; j++) {
        for (int h = 1; h <= m; h++) {
            cin >> a[j][h];
        }
    }
    dfs(1, 1);
    if (g == 0) {
        cout << "NO";
    } else {
        cout << "YES";
    }
    return 0;
}

P2199 最后的迷宫

#include <bits/stdc++.h>
using namespace std;
const int N = 2e4 + 10;
int n, m, sx, sy, ex, ey;
char mp[N];
bool vis[N];
struct node {
    int x, y, step;
};
queue<node>q;
const int dx[] = {1, 1, -1, 1, 0, -1, 0, -1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

bool check(int x, int y) {
    if (x < 1 || x > n || y < 1 || y > m || mp[(x - 1)*m + y] == 'X') {
        return 0;
    } else return 1;
    }

bool look(int stx, int sty) {
    for (int i = 0; i < 8; ++i) {
        int xx = dx[i] + stx;
        int yy = dy[i] + sty;
        while (check(xx, yy)) {
            if (xx == ex && yy == ey)return 1;
            xx += dx[i];
            yy += dy[i];
        }
    }
    return 0;
}

int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j)cin >> mp[(i - 1)*m + j];
    }
    while (1) {
        scanf("%d%d%d%d", &ex, &ey, &sx, &sy);
        if (sx == sy && sy == ex && ey == ex && ex == 0)return 0;
        while (q.size())q.pop();
        memset(vis, 0, sizeof vis);
        q.push({sx, sy, 0});
        vis[(sx - 1)*m + sy] = 1;
        bool flag = 1;
        while (q.size()) {
            node now = q.front();
            q.pop();
            if (now.x == ex && now.y == ey || look(now.x, now.y)) {
                printf("%d\n", now.step);
                flag = 0;
                break;
            }
            for (int i = 0; i < 8; i += 2) {
                int xx = now.x + dx[i];
                int yy = now.y + dy[i];
                if (!check(xx, yy) || vis[(xx - 1)*m + yy])continue;
                q.push({xx, yy, now.step + 1});
                vis[(xx - 1)*m + yy] = 1;
            }
        }
        if (flag)printf("Poor Harry\n");
    }
    return 0;
}