1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| import java.util.ArrayList; import java.util.List; import java.util.Scanner;
public class code3 {
public static void main(String[] args) { Scanner in = new Scanner(System.in); boolean flag = true; int cnt = 0; int line = 0; int[][] grid = new int[64][64]; boolean[][] visited = new boolean[64][64]; List<int[]> list = new ArrayList<>(); int[] res = new int[1]; while (in.hasNextLine()) { if (flag) { line = Integer.parseInt(in.nextLine()); res = new int[line]; flag = false; } else { String a = in.nextLine(); String[] split = a.split(" "); int n = (split.length - 1) / 2; int num = Integer.parseInt(split[0]); int[] ints = {cnt, num, Integer.parseInt(split[1]), Integer.parseInt(split[2])}; list.add(ints); for (int i = 0; i < n; i++) { int idx = i * 2 + 1; int x = Integer.parseInt(split[idx]); int y = Integer.parseInt(split[idx + 1]); grid[x][y] = num; } cnt++; if (line == cnt) { break; } } }
System.out.println();
for (int[] ints : list) { dfs(grid, ints, ints[2], ints[3], visited, res); }
StringBuilder sb = new StringBuilder(); for (int re : res) { sb.append(re + " "); } String substring = sb.substring(0, sb.length() - 1); System.out.println(substring); }
private static void dfs(int[][] grid, int[] ints, int x, int y, boolean[][] visited, int[] res) { if (x < 0 || x >= 64 || y < 0 || y >= 64) { res[ints[0]]++; return; } if (grid[x][y] != ints[1]) { res[ints[0]]++; } else if (!visited[x][y]) { visited[x][y] = true; dfs(grid, ints, x - 1, y, visited, res); dfs(grid, ints, x, y - 1, visited, res); dfs(grid, ints, x + 1, y, visited, res); dfs(grid, ints, x, y + 1, visited, res); } } }
|