From 276fde51c551130ed293647a310f2268dc2c31db Mon Sep 17 00:00:00 2001 From: Haram Baek Date: Tue, 2 Apr 2024 13:12:47 +0900 Subject: [PATCH] =?UTF-8?q?Create=20=EB=B2=BD=EB=8F=8C=EA=B9=A8=EA=B8=B0.j?= =?UTF-8?q?ava?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\353\217\214\352\271\250\352\270\260.java" | 145 ++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 "Haram/SWEA/\353\252\250\354\235\230\354\227\255\353\237\211\355\205\214\354\212\244\355\212\270/\353\262\275\353\217\214\352\271\250\352\270\260.java" diff --git "a/Haram/SWEA/\353\252\250\354\235\230\354\227\255\353\237\211\355\205\214\354\212\244\355\212\270/\353\262\275\353\217\214\352\271\250\352\270\260.java" "b/Haram/SWEA/\353\252\250\354\235\230\354\227\255\353\237\211\355\205\214\354\212\244\355\212\270/\353\262\275\353\217\214\352\271\250\352\270\260.java" new file mode 100644 index 00000000..f5f8a076 --- /dev/null +++ "b/Haram/SWEA/\353\252\250\354\235\230\354\227\255\353\237\211\355\205\214\354\212\244\355\212\270/\353\262\275\353\217\214\352\271\250\352\270\260.java" @@ -0,0 +1,145 @@ +import java.awt.Point; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.Queue; +import java.util.StringTokenizer; + +public class Solution { + static int T, N, W, H, map[][], originMap[][], targets[], answer; + static StringTokenizer st; + static BufferedReader br; + + static int[] dx = {-1, 1, 0, 0}; + static int[] dy = {0, 0, -1, 1}; + public static void main(String[] args) throws Exception { + br = new BufferedReader(new InputStreamReader(System.in)); + + T = Integer.parseInt(br.readLine()); + + for(int t = 1; t < T+1; t++) { + input(); + + targets = new int[N]; + permutation(0); + + StringBuilder sb = new StringBuilder(); + sb.append("#" + t + " " + answer); + System.out.println(sb.toString()); + } + } + + private static void permutation(int num) { + if(num == N) { + map = copy(originMap); + for(int target : targets) { + Point start = findTop(target); + if(start == null) continue; + bfs(start); + rebuildMap(); + } + answer = Math.min(answer, count()); + return; + } + + for(int i = 0; i < W; i++) { + targets[num] = i; + permutation(num+1); + } + } + + private static int count() { + int cnt = 0; + for(int i = 0; i < H; i++) { + for(int j = 0; j < W; j++) { + if(map[i][j] > 0) cnt++; + } + } + return cnt; + } + + private static void rebuildMap() { + for(int j = 0; j < W; j++) { + Queue queue = new ArrayDeque<>(); + for(int i = H-1; i > -1; i--) { + if(map[i][j] == 0) continue; + queue.offer(map[i][j]); + map[i][j] = 0; + } + + for(int i = H-1; i > -1; i--) { + if(queue.isEmpty()) break; + map[i][j] = queue.poll(); + } + } + } + + private static void bfs(Point start) { + Queue queue = new ArrayDeque<>(); + queue.offer(start); + + while(!queue.isEmpty()) { + Point target = queue.poll(); + int range = map[target.x][target.y]; + map[target.x][target.y] = 0; + + //if(range == 1) continue; + for(int d = 0; d < 4; d++) { + int nx = target.x; + int ny = target.y; + for(int r = 0; r < range-1; r++) { + nx += dx[d]; + ny += dy[d]; + + if(!isIn(nx, ny)) continue; + if(map[nx][ny] == 0) continue; + queue.offer(new Point(nx, ny)); + } + } + } + } + + private static boolean isIn(int x, int y) { + return (-1 < x && x < H && -1 < y && y < W) ? true : false; + } + + private static Point findTop(int y) { + for(int i = 0; i < H; i++) { + if(map[i][y] == 0) continue; + else return new Point(i, y); + } + return null; + } + + private static void input() throws Exception { + answer = Integer.MAX_VALUE; + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + W = Integer.parseInt(st.nextToken()); + H = Integer.parseInt(st.nextToken()); + + map = new int[H][W]; + originMap = new int[H][W]; + + for(int i = 0; i < H; i++) { + st = new StringTokenizer(br.readLine()); + for(int j = 0; j < W; j++) { + int ele = Integer.parseInt(st.nextToken()); + map[i][j] = ele; + originMap[i][j] = ele; + } + } + } + + private static int[][] copy(int[][] map) { + int[][] result = new int[H][W]; + + for(int i = 0; i < H; i++) { + for(int j = 0; j < W; j++) { + result[i][j] = map[i][j]; + } + } + return result; + } +}