|
| 1 | +package com.algorithm2025.backjoon3.day002; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Example20250621_Q84021 { //코딩테스트 연습 깊이/너비 우선 탐색(DFS/BFS) 퍼즐 조각 채우기 복습 |
| 6 | + |
| 7 | + int[] dx = {0, 0, 1, -1}; |
| 8 | + int[] dy = {1, -1, 0, 0}; |
| 9 | + |
| 10 | + public int Example20250621_Q84021(int[][] game_board, int[][] table) { |
| 11 | + int len = game_board.length; |
| 12 | + List<List<Point>> emptySpaces = extractBlocks(game_board, 0, true); |
| 13 | + List<List<Point>> tableBlocks = extractBlocks(table, 1, false); |
| 14 | + |
| 15 | + boolean[] used = new boolean[emptySpaces.size()]; |
| 16 | + int answer = 0; |
| 17 | + |
| 18 | + for (List<Point> block : tableBlocks) { |
| 19 | + normalize(block); |
| 20 | + for (int i = 0; i < emptySpaces.size(); i++) { |
| 21 | + if (used[i] || block.size() != emptySpaces.get(i).size()) continue; |
| 22 | + if (canFit(block, emptySpaces.get(i))) { |
| 23 | + used[i] = true; |
| 24 | + answer += block.size(); |
| 25 | + break; |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + return answer; |
| 31 | + } |
| 32 | + |
| 33 | + private List<List<Point>> extractBlocks(int[][] board, int target, boolean invert) { |
| 34 | + int len = board.length; |
| 35 | + boolean[][] visited = new boolean[len][len]; |
| 36 | + List<List<Point>> blocks = new ArrayList<>(); |
| 37 | + |
| 38 | + for (int i = 0; i < len; i++) { |
| 39 | + for (int j = 0; j < len; j++) { |
| 40 | + int value = invert ? 1 - board[i][j] : board[i][j]; |
| 41 | + if (value == target && !visited[i][j]) { |
| 42 | + blocks.add(bfs(i, j, board, visited, target, invert)); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + return blocks; |
| 47 | + } |
| 48 | + |
| 49 | + private List<Point> bfs(int x, int y, int[][] board, boolean[][] visited, int target, boolean invert) { |
| 50 | + Queue<Point> q = new LinkedList<>(); |
| 51 | + List<Point> shape = new ArrayList<>(); |
| 52 | + |
| 53 | + q.add(new Point(x, y)); |
| 54 | + visited[x][y] = true; |
| 55 | + shape.add(new Point(0, 0)); |
| 56 | + |
| 57 | + while (!q.isEmpty()) { |
| 58 | + Point p = q.poll(); |
| 59 | + |
| 60 | + for (int d = 0; d < 4; d++) { |
| 61 | + int nx = p.x + dx[d]; |
| 62 | + int ny = p.y + dy[d]; |
| 63 | + |
| 64 | + if (nx >= 0 && ny >= 0 && nx < board.length && ny < board.length && !visited[nx][ny]) { |
| 65 | + int value = invert ? 1 - board[nx][ny] : board[nx][ny]; |
| 66 | + if (value == target) { |
| 67 | + visited[nx][ny] = true; |
| 68 | + q.add(new Point(nx, ny)); |
| 69 | + shape.add(new Point(nx - x, ny - y)); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + return shape; |
| 75 | + } |
| 76 | + |
| 77 | + private boolean canFit(List<Point> block, List<Point> space) { |
| 78 | + for (int i = 0; i < 4; i++) { |
| 79 | + normalize(block); |
| 80 | + if (match(block, space)) return true; |
| 81 | + rotate(block); |
| 82 | + } |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + private void rotate(List<Point> shape) { |
| 87 | + for (Point p : shape) { |
| 88 | + int temp = p.x; |
| 89 | + p.x = p.y; |
| 90 | + p.y = -temp; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + private void normalize(List<Point> shape) { |
| 95 | + int minX = Integer.MAX_VALUE; |
| 96 | + int minY = Integer.MAX_VALUE; |
| 97 | + for (Point p : shape) { |
| 98 | + minX = Math.min(minX, p.x); |
| 99 | + minY = Math.min(minY, p.y); |
| 100 | + } |
| 101 | + for (Point p : shape) { |
| 102 | + p.x -= minX; |
| 103 | + p.y -= minY; |
| 104 | + } |
| 105 | + Collections.sort(shape); |
| 106 | + } |
| 107 | + |
| 108 | + private boolean match(List<Point> a, List<Point> b) { |
| 109 | + for (int i = 0; i < a.size(); i++) { |
| 110 | + if (a.get(i).x != b.get(i).x || a.get(i).y != b.get(i).y) return false; |
| 111 | + } |
| 112 | + return true; |
| 113 | + } |
| 114 | + |
| 115 | + static class Point implements Comparable<Point> { |
| 116 | + int x, y; |
| 117 | + Point(int x, int y) { |
| 118 | + this.x = x; |
| 119 | + this.y = y; |
| 120 | + } |
| 121 | + |
| 122 | + public int compareTo(Point o) { |
| 123 | + return x != o.x ? Integer.compare(x, o.x) : Integer.compare(y, o.y); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments