爆栈,但不清楚哪里爆栈

B3625 迷宫寻路

__szh_DNCB__ @ 2024-09-28 18:05:56

#include<bits/stdc++.h>
using namespace std;
bool s[105][105];
int n,m;
int dx[5] = {-1,0,1,0};
int dy[5] = {0,-1,0,1};
bool check(int x,int y){
    if (x <= 0 || x >= n || y <= 0 || y >= m || s[x][y] == false)
        return false;
    return true;
}
string bfs(){
    queue <pair<int,int>> q;
    q.push({1,1});
    while (q.size()){
        pair<int,int> p = q.front();
        q.pop();
        int fi = p.first;
        int se = p.second;
        if (fi == n && se == m)return "Yes";
        if (check(p.first,p.second) == false)continue;
        else{
            for (int i=0;i<4;i++){
                if (check(fi+dx[i],se+dy[i])){
                    q.push({fi+dx[i],se+dy[i]});
                }
            }
        }
        if (q.empty())return "No";
    }
    return "No";
}
int main(){
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            char c;
            cin >> c;
            if (c == '.')s[i][j] = true;
            else s[i][j] = false;
        }
    }
    cout << bfs();
    return 0;
}

有无大佬指导一下这个bfs为啥死循环


by Texas_the_Omertosa @ 2024-09-28 18:09:49

@szh_DNCB 你走过的地方还能走啊


by __szh_DNCB__ @ 2024-09-28 18:12:19

@Texas_the_Omertosa 哦哦哦忘判断了,谢谢已关。此贴结


|