求助dalao 为什么会wa掉几个点

P1162 填涂颜色

Gent7c @ 2024-05-01 13:00:37

#include<iostream>
#include<cstring>
using namespace std;
int mp[35][35];
bool vis[35][35];
int n;
int dx[4] = {0,1,0,-1};
int dy[4] = { 1,0,-1,0};
void dfs(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i];
        if (mp[nx][ny] == 1 || vis[nx][ny]||nx<0||nx>n-1||ny<0||ny>n-1) return;
        dfs(nx, ny);
        vis[nx][ny] = true;
    }
}
int main() {
    cin >> n;
    memset(vis, false, sizeof(vis));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++) {
            cin >> mp[i][j];
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (!vis[i][j]&&(mp[i][j] == 0 && i == 0 || mp[i][j] == 0 && i == n - 1 || mp[i][j] == 0 && j == 0 || mp[i][j] == 0 && j == n - 1))
            {
                vis[i][j] = true;
                dfs(i, j);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (mp[i][j] == 1||vis[i][j])
                cout << mp[i][j]<<" ";
            else cout << 2<<" ";
        }
        cout << endl;
    }
}

by Dino_tang @ 2024-05-11 15:49:41

要用BFS哟

by junhaowang @ 2024-05-24 20:34:33

不一定要 BFS


by 123huchenghao @ 2024-06-28 18:31:44

试一下这个


by 123huchenghao @ 2024-06-28 18:31:53

#include<bits/stdc++.h>
using namespace std;
struct pos
{
    int x;
    int y;
};
bool mark[32][32];
int pic[32][32],dx[]= {0,0,1,-1},dy[]= {1,-1,0,0},n;
queue<pos>q;
void bfs(int x,int y)
{
    int tx,ty;
    q.push(pos{x,y});
    mark[x][y]=true;
    while(!q.empty())
    {
        tx=q.front().x;
        ty=q.front().y;
        q.pop();
        for(int i=0; i<4; i++)
        {
            if(tx+dx[i]<0||ty+dy[i]<0||tx+dx[i]>n+1||ty+dy[i]>n+1||mark[tx+dx[i]][ty+dy[i]])continue;
            q.push(pos{tx+dx[i],ty+dy[i]});
            mark[tx+dx[i]][ty+dy[i]]=true;
        }
    }
}
int main()
{
    memset(pic,0,sizeof(pic));
    memset(mark,0,sizeof(mark));
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            cin>>pic[i][j];
            mark[i][j]=pic[i][j];
        }
    }
    bfs(0,0);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(mark[i][j])cout<<pic[i][j]<<" ";
            else cout<<"2"<<" ";
        }
        cout<<endl;
    }
}

|