48分?

P1162 填涂颜色

HappyDavid @ 2023-09-02 18:52:41

/* 看了看题解,先DFS外面,外面的搞成一个数,再搞里面的就好了……NB */
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
lint n, a[31][31], i, j;
void dfs(lint x, lint y, lint toNum) {
    if (x < 1 || x > n || y < 1 || y > n || a[x][y] != 0)
        return;
    a[x][y] = toNum;
    dfs (x - 1, y, toNum);
    dfs (x, y - 1, toNum);
    dfs (x, y + 1, toNum);
    dfs (x + 1, y, toNum);
}
int main() {
    scanf ("%lld", &n);
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            scanf ("%lld", &a[i][j]);
    dfs (1, 1, 3);
    dfs (n, n, 3);
    dfs (1, n, 3);
    dfs (n, 1, 3);
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
            if (a[i][j] == 0)
                a[i][j] = 2;
            if (a[i][j] == 3)
                a[i][j] = 0;
            printf ("%lld ", a[i][j]);
        }
        printf ("\n");
    }
    return 0;
}

by Null_h @ 2023-09-02 19:07:57

扫四条边,不是四个角


/* 看了看题解,先DFS外面,外面的搞成一个数,再搞里面的就好了……NB */
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
lint n, a[31][31], i, j;
void dfs(lint x, lint y, lint toNum) {
    if (x < 1 || x > n || y < 1 || y > n || a[x][y] != 0)
        return;
    a[x][y] = toNum;
    dfs (x - 1, y, toNum);
    dfs (x, y - 1, toNum);
    dfs (x, y + 1, toNum);
    dfs (x + 1, y, toNum);
}
int main() {
    scanf ("%lld", &n);
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            scanf ("%lld", &a[i][j]);
    for(int i=1;i<=n;i++){
        dfs(1,i,3);
        dfs(i,1,3);
        dfs(n,i,3);
        dfs(i,n,3);
    }
    for (i = 1; i <= n; i++) {
        for (j = 1; j <= n; j++) {
            if (a[i][j] == 0)
                a[i][j] = 2;
            if (a[i][j] == 3)
                a[i][j] = 0;
            printf ("%lld ", a[i][j]);
        }
        printf ("\n");
    }
    return 0;
}

by HappyDavid @ 2023-09-02 19:20:32

@kdqs8218 感谢!


by HappyDavid @ 2023-09-02 19:28:11

@HappyDavid AC了!


|