请问这串代码为什么会RE

P1162 填涂颜色

hola0826 @ 2024-10-24 11:43:47

#include <iostream>
using namespace std;
int a[100][100],b[100][100];
int d[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
int n;
void dfs(int x, int y) {
    if (x<0 || x>n + 1 || y < 0 || y>n + 1)return;
    for (int i = 0; i <= 3; i++) {
        int xx = x + d[i][0];
        int yy = y + d[i][1];
        if (a[xx][yy] == 0) {
            a[xx][yy] = -1;
            b[xx][yy] = a[xx][yy];
            dfs(xx, yy);
            a[xx][yy] = 0;
        }
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> a[i][j];
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            b[i][j] = a[i][j];
        }
    }
    dfs(0, 0);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (b[i][j] == -1) {
                b[i][j] = 0;
                cout << b[i][j];
            }
            else if (b[i][j] == 0) {
                b[i][j] = 2;
                cout << b[i][j];
            }
            else {
                cout << b[i][j];
            }
        }
        cout << endl;
    }
    return 0;
}

by Civilight_Eterna @ 2024-10-24 11:56:08

刚刚我出言不逊(假设你看到了), dfs的边界条件应该取等 @hola0826


by hola0826 @ 2024-10-24 12:02:28

@xionghaoran123 xiexie


by hola0826 @ 2024-10-24 12:04:04

@xionghaoran123 取等wa了,,不过我知道RE的原因了,数组边界溢出了


by hola0826 @ 2024-10-24 12:04:33

@xionghaoran123 还是谢谢dalao'


by Civilight_Eterna @ 2024-10-24 12:54:01

@hola0826 WA看看样例吧


|