|
| 1 | +package Simulation.swea5656; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.util.*; |
| 5 | + |
| 6 | +public class Solution { |
| 7 | + |
| 8 | + static int T, N, W, H, ans; |
| 9 | + static int[][] map, tmp; |
| 10 | + static int[] di = {-1, 0, 1, 0}; |
| 11 | + static int[] dj = {0, 1, 0, -1}; |
| 12 | + |
| 13 | + public static void main(String[] args) throws Exception { |
| 14 | + System.setIn(new FileInputStream("src/Simulation/swea5656/sample_input.txt")); |
| 15 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 16 | + StringBuilder sb = new StringBuilder(); |
| 17 | + |
| 18 | + T = Integer.parseInt(br.readLine()); |
| 19 | + for (int t = 1; t <= T; t++) { |
| 20 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 21 | + N = Integer.parseInt(st.nextToken()); |
| 22 | + W = Integer.parseInt(st.nextToken()); |
| 23 | + H = Integer.parseInt(st.nextToken()); |
| 24 | + |
| 25 | + map = new int[H][W]; |
| 26 | + for (int i = 0; i < H; i++) { |
| 27 | + st = new StringTokenizer(br.readLine()); |
| 28 | + for (int j = 0; j < W; j++) { |
| 29 | + map[i][j] = Integer.parseInt(st.nextToken()); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + tmp = new int[H][W]; |
| 34 | + ans = W*H; |
| 35 | + dfs(0, ans); |
| 36 | + |
| 37 | + sb.append("#").append(t).append(" ").append(ans).append("\n"); |
| 38 | + } |
| 39 | + |
| 40 | + System.out.println(sb.toString()); |
| 41 | + } |
| 42 | + |
| 43 | + static void dfs(int cnt, int res) { |
| 44 | + if (cnt == N || res == 0) { |
| 45 | + ans = Math.min(ans, res); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + for (int w = 0; w < W; w++) { |
| 50 | + for (int h = 0; h < H; h++) { |
| 51 | + if (map[h][w] > 0) { |
| 52 | + int[][] copy = new int[H][W]; |
| 53 | + for (int i = 0; i < H; i++) { |
| 54 | + copy[i] = Arrays.copyOf(map[i], W); |
| 55 | + tmp[i] = Arrays.copyOf(map[i], W); |
| 56 | + } |
| 57 | + crash(h, w); |
| 58 | + dfs(cnt+1, down()); |
| 59 | + for (int i = 0; i < H; i++) map[i] = Arrays.copyOf(copy[i], W); |
| 60 | + break; |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static void crash(int i, int j) { |
| 67 | + tmp[i][j] = 0; |
| 68 | + for (int k = 0; k < 4; k++) { |
| 69 | + for (int n = 1; n < map[i][j]; n++) { |
| 70 | + int to_i = i + di[k]*n; |
| 71 | + int to_j = j + dj[k]*n; |
| 72 | + if (!isValidPath(to_i, to_j)) break; |
| 73 | + if (tmp[to_i][to_j] > 0) crash(to_i, to_j); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + static int down() { |
| 79 | + int cnt = 0; |
| 80 | + map = new int[H][W]; |
| 81 | + for (int j = 0; j < W; j++) { |
| 82 | + for (int i = H-1, k = H-1; i >= 0; i--) { |
| 83 | + if (tmp[i][j] > 0) { |
| 84 | + map[k--][j] = tmp[i][j]; |
| 85 | + cnt ++; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return cnt; |
| 90 | + } |
| 91 | + |
| 92 | + static boolean isValidPath(int i, int j) { |
| 93 | + return 0 <= i && i < H && 0 <= j && j < W; |
| 94 | + } |
| 95 | +} |
0 commit comments