求助!为什么P1162我全部MLE???

P1162 填涂颜色

Wind23 @ 2022-08-01 09:59:50

#include<bits/stdc++.h>
#define N 70
using namespace std;
long long n;
long long df[N][N],ass[N][N];
long long dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
void fuck(long long x,long long y){
    if(x<0 || x>n+1 || y<0 || y>n+1){
        return ;
    }
    ass[x][y]=1;
    for(long long i=0;i<4;i++){
        fuck(x+dir[i][0],y+dir[i][1]);
    }
}
int main(){
    scanf("%d",&n);
    for(long long i=1;i<=n;i++){
        for(long long j=1;j<=n;j++){
            cin>>df[i][j];
            if(df[i][j]==0){
                ass[i][j]=0;
            }
            else{
                ass[i][j]=2;
            }
        }
    }
    fuck(1,1);
    for(long long i=1;i<=n;i++){
        for(long long j=1;j<=n;j++){
            if(ass[i][j]==0){
                cout<<2<<' ';
            }
            else{
                cout<<df[i][j]<<' ';
            }
        }
        cout<<endl;
    }
    return 0;
}

请问,我的代码为什么会MLE


by _zzzzzzy_ @ 2022-08-01 10:09:11

@Wind23 fuck报栈了?


by 野生小卒 @ 2022-08-01 10:14:07

fuck无限递归爆栈了


by 野生小卒 @ 2022-08-01 10:15:31

你这个fuck函数可能会出现fuck(0,0)<->fuck(0,1)的情况


by abensyl @ 2022-08-01 10:41:30

你还怪可以嘞,还搞一个 fuck


by ttltony @ 2022-08-01 19:52:09

供上AC代码

#include <cstdio>
#include <cstring>

using namespace std;

int n;
int a[31][31];
bool f[31][31];

void dfs(int x, int y) {
    if (!f[x][y]) return ;
    if (a[x][y] != 0) return ;
    f[x][y] = false;
    if (x + 1 <= n) dfs(x + 1, y);
    if (y + 1 <= n) dfs(x, y + 1);
    if (x - 1 >= 1) dfs(x - 1, y);
    if (y - 1 >= 1) dfs(x, y - 1);
}

int main() {
    memset(f, true, sizeof(f));
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++ ) for (int j = 1; j <= n; j ++ ) scanf("%d", &a[i][j]);
    for (int i = 1; i <= n; i ++ ) {
        if (a[1][i] == 0) { dfs(1, i); }
        if (a[i][1] == 0) { dfs(i, 1); }
        if (a[n][i] == 0) { dfs(n, i); }
        if (a[i][n] == 0) { dfs(i, n); }
    }
    for (int i = 1; i <= n; i ++ ) {
        for (int j = 1; j <= n; j ++ ) {
            if (f[i][j]) {
                if (a[i][j]) continue;
                a[i][j] = 2;
            }
        }
    }
    for (int i = 1; i <= n; i ++ ) {
        for (int j = 1; j <= n; j ++ ) {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
    return 0;
}

by __YLX @ 2022-08-22 16:37:58

简单,fuck函数改成:

void fuck(long long x,long long y){
    if(x<0 || x>n+1 || y<0 || y>n+1 || ass[x][y]==1){
        return ;
    }
    ass[x][y]=1;
    for(long long i=0;i<4;i++){
        fuck(x+dir[i][0],y+dir[i][1]);
    }
}

但是,千万不要在比赛中出现敏感的标识符啊!!


|