这道题数据范围不是30吗,为什么35的数组会RE?

P1162 填涂颜色

AC_love @ 2023-08-16 14:27:31

#include <bits/stdc++.h>
using namespace std;
int n;
int a[35][35];
// 换成 40 就过了
// 不知道为什么

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

int dfs(int x, int y)
{
    a[x][y] = 114514;
    for(int i = 1; i <= 4; i = i + 1)
    {
        int px = x + dx[i];
        int py = y + dy[i];
        if(a[px][py] == 2)
            dfs(px, py);
    }
}

void print(int s)
{
    if(s == 114514)
        cout << 0 << " ";
    else
        cout << s << " ";
}

int main()
{
//  freopen("a.txt", "r", stdin);
    cin >> n;
    for(int i = 1; i <= n; i = i + 1)
        for(int j = 1; j <= n; j = j + 1)
        {
            cin >> a[i][j];
            if(a[i][j] == 0)
                a[i][j] = 2;
        }
    for(int i = 1; i <= n; i = i + 1)
        for(int j = 1; j <= n; j = j + 1)
        {
            if(a[i][j] != 2)
                continue;
            if(i == 1 || i == n || j == 1 || j == n)
                dfs(i, j);
        }
    for(int i = 1; i <= n; i = i + 1)
    {
        for(int j = 1; j <= n; j = j + 1)
            print(a[i][j]);
        cout << "\n"; 
    }

    return 0;
}

by ncwzdlsd @ 2023-08-16 14:32:01

@AC_love 你再交一遍。。。

https://www.luogu.com.cn/record/121192314


by AC_love @ 2023-08-16 15:28:25

破案了,真相是 int 类型函数没有返回值

不开 O2 没事,一开就会莫名其妙 RE

这个故事告诉我们,能写 void 就别写 int,真写 int 了就写上 return 0


by Phigros_Guest @ 2023-09-04 20:03:00

@AC_love 都是新版本devc++主函数无需返回惯得,但凡爆语法错误都能看出来


by Phigros_Guest @ 2023-09-04 20:03:32

@AC_love 而且你数组越界不是MLE吗


|