AC不了一点

P1162 填涂颜色

Tiks_code @ 2024-03-19 18:23:41

#include <bits/stdc++.h>

using namespace std;

const int N = 40;

int n;

bool st[N][N];

int a[N][N] = { 0 };

typedef pair<int, int> PII;

int dx[] = { -1,0,1,0 };
int dy[] = { 0,1,0,-1 };

queue <PII> q;

void bfs(int x, int y) {
    q.push(PII{ x,y });
    st[x][y] = true;
    while (!q.empty()) {
        PII p = q.front();
        q.pop();
        int x = p.first, y = p.second;
        for (int i = 0; i < 4; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];
            if (st[tx][ty]) continue;
            if (tx < 1 || tx > n || ty < 1 || ty > n) continue;

            st[tx][ty] = true;
            bfs(tx, ty);
        }
    }
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
            if(a[i][j]){
                st[i][j] = true;
            }
        }
    }

    int bn[][2] = { {1,1},{1,n},{n,1},{n,n} };
    for (int i = 0; i <= 3; i++) {
         bfs(bn[i][0], bn[i][1]);
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (!st[i][j]) a[i][j] = 2;
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            printf("%-2d",a[i][j]);
        }
        cout << endl;
    }
    return 0;
}

怎么都过不去3,4,6,真的看不出问题呀呜呜


by Voltaris @ 2024-03-20 20:12:58

为什么BFS要递归??


by Voltaris @ 2024-03-20 20:13:17

bfs(tx, ty);??


by Lazy_make_name @ 2024-03-28 22:18:12

你的程序太复杂了,我看不懂,但我这有个简单的能AC的程序,你可以消化一下(仅31行)

#include<iostream>
using namespace std;
int t[35][35];
int main(){
    int n,shu;
    cin>>n;
    for(int i=2;i<=n+1;i++)
        for(int j=2;j<=n+1;j++){
            cin>>t[i][j];
            if(t[i][j]==0)
                t[i][j]=2;
            }
    for(int ci=1;ci<=900;ci++)
        for(int i=1;i<=n+2;i++)
            for(int j=1;j<=n+2;j++)
                if(t[i][j]==0){
                    if(t[i+1][j]!=1)
                        t[i+1][j]=0;
                    if(t[i-1][j]!=1)
                        t[i-1][j]=0;
                    if(t[i][j+1]!=1)
                        t[i][j+1]=0;
                    if(t[i][j-1]!=1)
                        t[i][j-1]=0;
                }   
    for(int i=2;i<=n+1;i++){
        for(int j=2;j<=n+1;j++)
            cout<<t[i][j]<<" ";
        cout<<endl;
    }
} 

|