大佬,求助

P1162 填涂颜色

XSean @ 2022-11-09 21:47:32

我有点看不懂题解的一个地方 就是为什么dfs时要搜边界 以下是一个题解

#include <cstdio>
using namespace std;
int n;
int a[32][32];
void dfs(int x, int y){
    if(x >= 0 && x <= n + 1 && y >= 0 && y <= n + 1){
        if(a[x][y] == 1 || a[x][y] == 3) return ;
        else{a[x][y] = 3;
            dfs(x + 1, y); dfs(x - 1, y);
            dfs(x, y + 1); dfs(x, y - 1);}
    }
}
int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; ++ i)
    for(int j = 1; j <= n; ++ j)
    scanf("%d", &a[i][j]);
    dfs(0, 0);
    for(int i = 1; i <= n; ++ i)
    for(int j = 1; j <= n; ++ j)
    if(a[i][j] == 3) a[i][j] = 0;
    else if(a[i][j] == 0) a[i][j] = 2;
    for(int i = 1; i <= n; ++ i, printf("\n"))
    for(int j = 1; j <= n; printf("%d ", a[i][j]), ++ j) ;
    return 0;
}

by zxy123bc @ 2022-11-09 22:43:43

@Sean_xzx 哪一行


by XSean @ 2022-11-10 08:02:34

第6行

if(x >= 0 && x <= n + 1 && y >= 0 && y <= n + 1){

|