为什么第一个点会错

P1434 [SHOI2002] 滑雪

yesido @ 2024-10-14 23:25:09

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define PII pair<int,int>
#define int long long

const int MAX = 105;
int a[MAX][MAX],st[MAX][MAX];

int n, m;
int dis[5]={1,0,-1,0,1};
int bfs(int x,int y){
    int w = 1;
    queue<PII> p;
    p.emplace(x,y);
    while(!p.empty()){
        auto t = p.front();
        p.pop();
        int d = INT_MAX;
        int xxx,yyy;
        int l = t.first,r = t.second;
        for(int i = 0; i < 4; i++){
            int xx = l + dis[i],yy = r + dis[i + 1];
            if(xx >= 1 && xx <= n && yy >= 1 && yy <= m  && a[xx][yy] < a[l][r]){
                int s  =  a[l][r] - a[xx][yy];
                if(s < d){
                    d = s;
                    xxx = xx;
                    yyy = yy;
                }
            }
        }
        if(d != INT_MAX){
            p.emplace(xxx,yyy);
            w++;
        }
    }
    return w;
}

inline void solve(){
    cin >> n >> m;
    int ans = 0;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            cin >> a[i][j];
        }
    }
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= m; j++){
            ans = max(ans,bfs(i,j));
        }
    }
    cout << ans << endl;

}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
//    cin >>  T;
    while(T--){
        solve();
    }
    return 0;
}

|