求助大神,只过了1和5

P1162 填涂颜色

china178 @ 2024-01-25 23:44:37

import java.util.Scanner;

public class P1162 {
    static int[] dirx = {0, 1, 0, -1};
    static int[] diry = {1, 0, -1, 0};
    static int[][] map;
    static boolean[][] judge;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        map = new int[n][n];
        judge = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            for (int i1 = 0; i1 < n; i1++) {
                map[i][i1] = scanner.nextInt();
                if (map[i][i1] == 0) judge[i][i1] = true;
            }
        }
        for (int i = 0; i < n; i++) {
            dfs(i, 0);
            dfs(n - 1, 0);
            dfs(0, i);
            dfs(0, n - 1);
        }
        for (int i = 0; i < n; i++) {
            for (int i1 = 0; i1 < n; i1++) {
                if (map[i][i1] == 1 && !judge[i][i1]) System.out.print("1 ");
                else if (map[i][i1] == 1 && judge[i][i1]) System.out.print("0 ");
                else System.out.print("2 ");
            }
            System.out.println();
        }
    }

    static void dfs(int x, int y) {
        if (map[x][y] == 1) return;
        map[x][y] = 1;
        for (int i = 0; i < 4; i++) {
            int xx = x + dirx[i];
            int yy = y + diry[i];
            if (xx < 0 || yy < 0 || xx >= map.length || yy >= map.length) continue;
            dfs(xx, yy);
        }
    }
}

|