求助,只有第一个点AC,其他MEL

P1162 填涂颜色

Shrimp123 @ 2024-02-20 15:31:24

#include<bits/stdc++.h>
using namespace std;
int a[40][40];
int f(int x,int y){
if(a[x][y]>0)return 0;
if(a[x][y]==-1)return 1;
if(a[x][y]==0)return (f(x-1,y)+f(x+1,y)+f(x,y+1)+f(x,y-1));
}
int main()
{
    int n;
    cin>>n;
    for(int i=0; i<40; i++) {
        for(int j=0; j<40; j++) {
            a[i][j]=-1;
        }
    }
    //vector<vector<int>>a(n+10,vector<int>(n+10,-1));
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            cin>>a[i][j];
    /*for(int i=0; i<=n; i++) {
        for(int j=0; j<=n; j++) {
            cout<<a[i][j]<<" ";
            if(j==n)cout<<endl;
        }
    }*/
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=n; j++) {
            if(a[i][j]==1)continue;
            if(a[i-1][j]==-1||a[i+1][j]==-1||a[i][j-1]==-1||a[i][j+1]==-1) {
                a[i][j]=-1;
                continue;
            }
            if(f(i,j))a[i][j]=-1;
            else a[i][j]=2;
        }
    }
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=n; j++) {
            if(a[i][j]==-1)cout<<0<<" ";
            else if(a[i][j]==1)cout<<1<<" ";
            else cout<<2<<" ";
            if(j==n)cout<<endl;
        }
    }
    return 0;
}

by hayoon @ 2024-02-20 15:34:51

@Shrimp123

《MEL》


by fp0cy1tz6mn4rd_ @ 2024-02-20 15:51:52

@Shrimp123 加判断 x,y 是否越界


|