60pts求助

B3625 迷宫寻路

Howard30 @ 2024-12-07 17:20:30

大佬们也帮我看看我的bfs哪里错了?救救我吧。

#include <iostream>
#include <queue>
using namespace std;

int map[105][105];
int vis[105][105] = {};
int dx[5] = {0,-1,0,1,0};
int dy[5] = {0,0,-1,0,1};
int n, m;
bool found = false;

struct node{
    int x, y;
};
queue<node> q;

void bfs() {
    while(!q.empty()) {
        node temp = q.front();
        q.pop();
        if(temp.x == n && temp.y == m) {
            found = true;
            return;
        }
        for(int i = 1; i <= 4; ++i) {
            int nx = temp.x + dx[i];
            int ny = temp.y + dy[i];
            if(nx < 1 || nx > n || ny < 1 || ny > m) {
                continue;
            }
            if(vis[nx][ny] == 1) {
                continue;
            }
            if(map[nx][ny] == '#') {
                continue;
            }
            vis[nx][ny] = 1;
            q.push(node{nx,ny});
        }
    }
}

int main() {
    cin >> n >> m;
    for(int i  =1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            cin >> map[i][j];
        }
    }
    vis[1][1] = 1;
    q.push(node{1,1});
    bfs();
    if(found) {
        cout << "Yes";
    }
    if(!found){
        cout << "No";
    }
    return 0;
}

by Howard30 @ 2024-12-07 21:09:23

有没有大佬讲讲bfs的思路啊?


|