bfs32分求助

P1162 填涂颜色

huangruize3 @ 2024-12-06 13:30:24

#include<bits/stdc++.h>
using namespace std;
typedef pair<int ,int> ll;
int mp[40][40];
bool vis[40][40];
int n; 
int fx[5]={0,0,1,0,-1};
int fy[5]={0,1,0,-1,0};

void bfs(int x,int y){
    queue<ll>q;
    q.push(make_pair(x,y));
    while(q.size()){
        ll xxx=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int nx,ny;
            nx=xxx.first+fx[i];
            ny=xxx.second+fy[i];
            if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&mp[nx][ny]==0&&!vis[nx][ny]){
                vis[nx][ny]=0;
                q.push(make_pair(nx,ny));

            }
        } 

    } 
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>mp[i][j];
        }
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
        if(vis[i][j]==0&&mp[i][j]==0){
                cout<<2<<' ';
            }else{
                cout<<mp[i][j]<<' ';
            }
        }
        cout<<endl;
    }
    return 0;
}

by FatLLion @ 2024-12-14 11:30:46

你这直接把所有0都输出成1了包错的啊


by huangruize3 @ 2024-12-14 16:31:30

@FatLLion 对呀,不然我就不发


by FatLLion @ 2024-12-14 17:02:04

你也没调用bfs


by huangruize3 @ 2024-12-14 17:47:12

@FatLLion原来如此


by FatLLion @ 2024-12-14 18:43:08

不是你BFS似乎也是错的QwQ


by huangruize3 @ 2024-12-14 19:58:18

@FatLLion哪错了


by FatLLion @ 2024-12-14 22:32:07

你的bfs是不是直接检测到0就直接变成1了,没有判断会不会连接边界


by huangruize3 @ 2024-12-15 20:06:41

@FatLLion 所以怎么写?


|