样例没过求助

P1141 01迷宫

yuntianming12 @ 2024-07-10 16:00:50

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int k,n,m,x,y,res,cnt=1;
string c;
int b[1005][1005],f[1005][1005];
int dx[5]={0,1,0,-1,0};
int dy[5]={0,0,1,0,-1};
void bfs(ll xx,ll yy){
    if(b[xx+1][yy]==2&&b[xx-1][yy]==2&&b[xx][yy+1]==2&&b[xx][yy-1]==2){
        return;
    }
    res++;
    f[xx][yy]=cnt;
    for(int i=1;i<=4;i++){
        int ux=xx+dx[i];
        int uy=yy+dy[i];
        if(ux>=1&&ux<=n&&uy>=1&&ux<=n&&b[ux][uy]!=b[xx][yy]&&!f[ux][uy]){
            bfs(ux,uy);
        }
    }
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>c;
        k=c.size();
        for(int j=1;j<=k;j++){
            b[i][j+1]=c[j]-'0';
        }
    }
    for(int i=1;i<=m;i++){
        cin>>x>>y;
        res=0;
        bfs(x,y);
        cout<<res<<endl;
    }
    return 0;
}

by wa_wa_wa_wa @ 2024-07-10 17:53:09

@junmixiaowei 输入的j循环改成0到k-1


by wa_wa_wa_wa @ 2024-07-10 17:55:11

@junmixiaowei 还有你这是BFS??


by wa_wa_wa_wa @ 2024-07-10 18:01:17

换思路吧


by yuntianming12 @ 2024-07-11 08:32:16

@wa_wa_wa_wa 好吧,谢谢


|