๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ๋ฒฝ๋Œ ๊นจ๊ธฐ SWEA #5656

by seolhee2750 2022. 10. 4.
๋ฌธ์ œ

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWXRQm6qfL0DFAUo 

 

SW Expert Academy

SW ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์—ญ๋Ÿ‰ ๊ฐ•ํ™”์— ๋„์›€์ด ๋˜๋Š” ๋‹ค์–‘ํ•œ ํ•™์Šต ์ปจํ…์ธ ๋ฅผ ํ™•์ธํ•˜์„ธ์š”!

swexpertacademy.com

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.StringTokenizer;

public class Solution {
	
	static int n, w, h;
	static int[][] map, memory;
	static boolean[][] visited;
	static int[] nums;
	static int ans;

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		StringBuilder out = new StringBuilder();
		
		int tc = Integer.parseInt(in.readLine());
		for(int t = 1; t <= tc; t++) {
			st = new StringTokenizer(in.readLine());
			n = Integer.parseInt(st.nextToken());
			w = Integer.parseInt(st.nextToken());
			h = Integer.parseInt(st.nextToken());
			map = new int[h][w];
			memory = new int[h][w];
			for(int i = 0; i < h; i++) {
				st = new StringTokenizer(in.readLine());
				for(int j = 0; j < w; j++) {
					int now = Integer.parseInt(st.nextToken());
					map[i][j] = now;
					memory[i][j] = now;
				}
			}
			
			ans = Integer.MAX_VALUE;
			makeSet();
			makePermu(0, new int[n]);
			
			out.append("#" + t + " " + ans + "\n");
		}
		System.out.print(out);
	}
	
	public static void makeSet() { // 0๋ถ€ํ„ฐ w-1๊นŒ์ง€, ๊ตฌ์Šฌ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋“ค์„ ๋‹ด๋Š” ์ง‘ํ•ฉ ๋งŒ๋“ค๊ธฐ
		nums = new int[w];
		for(int i = 0; i < w; i++) nums[i] = i;
	}
	
	public static void makePermu(int nth, int[] choosed) { // ์ค‘๋ณต ์ˆœ์—ด (๊ตฌ์Šฌ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ๋Š” ์œ„์น˜๋“ค์˜ ๋ชจ๋“  ์ˆœ์—ด)
		if(nth == choosed.length) {
			oneCase(choosed);
			return;
		}
		
		for(int i = 0; i < nums.length; i++) {
			choosed[nth] = nums[i];
			makePermu(nth+1, choosed);
		}
	}
	
	public static void oneCase(int[] choosed) { // ํ•˜๋‚˜์˜ ์ˆœ์—ด์— ๋Œ€ํ•œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜
		for(int i = 0; i < n; i++) {
			int y = choosed[i];
			for(int x = 0; x < h; x++) {
				if(map[x][y] > 0) { // ์ฒซ ๋ฒˆ์งธ ๋ฒฝ๋Œ์„ ๋งŒ๋‚ฌ์„ ๋•Œ
					Deque<int[]> que = new ArrayDeque<>();
					que.add(new int[]{x, y, map[x][y]});
					destroy(que);
					fall();
					break;
				}
			}
		}
		findAns();
		initMap();
	}
	
	public static void destroy(Deque<int[]> que) { // ํ•˜๋‚˜์˜ ๊ตฌ์Šฌ์— ์˜ํ•œ ๋ฒฝ๋Œ๋“ค์˜ ๋ถ€์„œ์ง์„ ํ‘œํ˜„
		visited = new boolean[h][w];
		while(!que.isEmpty()) {
			int[] now = que.removeFirst();
			int x = now[0];
			int y = now[1];
			int num = now[2];
			for(int i = 0; i < h; i++) { // ์ƒ, ํ•˜
				if(i < x-num+1 || i > x+num-1 || visited[i][y] == true) continue;
				que.add(new int[]{i, y, map[i][y]});
				map[i][y] = 0;
				visited[i][y] = true;
			}
			for(int i = 0; i < w; i++) { // ์ขŒ, ์šฐ
				if(i < y-num+1 || i > y+num-1 || visited[x][i] == true) continue;
				que.add(new int[]{x, i, map[x][i]});
				map[x][i] = 0;
				visited[x][i] = true;
			}
		}
	}
	
	public static void fall() { // ํ•˜๋‚˜์˜ ๊ตฌ์Šฌ์— ๋Œ€ํ•œ ๋ฒฝ๋Œ ๋ถ€์ˆจ์ด ๋๋‚˜๋ฉด, ๋ฒฝ๋“ค๋“ค์„ ์•„๋ž˜๋กœ ๋–จ์–ด๋œจ๋ฆฌ๊ธฐ
		for(int y = 0; y < w; y++) {
			for(int x = h-1; x > 0; x--) { // ๋งจ ์œ— ์นธ์€ ๋นˆ ์นธ์ด์–ด๋„ ๋–จ์–ด๋œจ๋ฆด ๋ฒฝ๋Œ์ด ์—†์œผ๋ฏ€๋กœ, ๋งจ ์œ—์นธ์˜ ๋ฐ”๋กœ ์•„๋žซ์นธ๊นŒ์ง€๋งŒ ํƒ์ƒ‰
				if(map[x][y] == 0) {
					for(int a = x-1; a >= 0; a--) {
						if(map[a][y] > 0) {
							map[x][y] = map[a][y];
							map[a][y] = 0;
							break;
						}
					}
				}
			}
		}
	}
	
	public static void findAns() { // ๋‚จ์€ ๋ฒฝ๋Œ ์ˆ˜๋ฅผ ์„ธ๊ณ , ์ตœ์†Ÿ๊ฐ’ ๊ฐฑ์‹ 
		int cnt = 0;
		for(int i = 0; i < h; i++) {
			for(int j = 0; j < w; j++) {
				if(map[i][j] > 0) cnt++;
			}
		}
		
		ans = Math.min(ans, cnt);
	}
	
	public static void initMap() { // ํ•˜๋‚˜์˜ ์กฐํ•ฉ์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์„ ๋๋‚ด๋ฉด map ๋ฐฐ์—ด ๋‹ค์‹œ ์›์ƒ๋ณต๊ท€
		for(int i = 0; i < h; i++) {
			System.arraycopy(memory[i], 0, map[i], 0, memory[i].length);
		}
	}
}

๐Ÿ‘‰ ์‹œ๋ฎฌ๋ ˆ์ด์…˜, ๊ตฌํ˜„ + ์ˆœ์—ด, BFS ๋ฌธ์ œ

 

makePermu ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ค‘๋ณต ์ˆœ์—ด์„ ๋งŒ๋“ค์–ด์„œ ๊ตฌ์Šฌ์ด ๋–จ์–ด์งˆ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ–ˆ๊ณ ,

ํ•˜๋‚˜์˜ ์ˆœ์—ด์„ ์™„์„ฑํ•  ๋•Œ๋งˆ๋‹ค oneCase ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ํ•ด๋‹น ์ˆœ์—ด์˜ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌํ–ˆ๋‹ค.

 

oneCase ๋ฉ”์„œ๋“œ์—์„œ๋Š” ์ˆœ์—ด์— ์ฃผ์–ด์ง„ ์—ด์„ ์œ„์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉฐ 

๊ตฌ์Šฌ์„ ๋˜์กŒ์„ ๋•Œ ๋งŒ๋‚  ์ˆ˜ ์žˆ๋Š” ์ฒซ ๋ฒˆ์งธ ๋ฒฝ๋Œ์„ ์ฐพ์•„์ฃผ์—ˆ๊ณ ,

์ฐพ์•˜์„ ์‹œ์—๋Š” destroy ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ BFS ํƒ์ƒ‰์„ ํ†ตํ•ด ๋ถ€์ˆ  ์ˆ˜ ์žˆ๋Š” ๋ฒฝ๋Œ์„ ์ฐพ์•„ ๋ถ€์‰ˆ๋‹ค.

 

destroy ๋ฉ”์„œ๋“œ๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ์„ ๋•Œ๋Š” fall ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ map์˜ ๋นˆ ์นธ๋“ค์„ ์ฑ„์›Œ์ฃผ์—ˆ๋‹ค.

map์˜ ์•„๋ž˜์—์„œ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•˜๋ฉฐ ๋นˆ ์นธ ๋ฐœ๊ฒฌ ์‹œ, ์œ„๋กœ ํƒ์ƒ‰ํ•˜์—ฌ ์ฐพ์€ ๋ฒฝ๋Œ์„ ํ•˜๋‚˜์”ฉ ์˜ฎ๊ฒจ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

 

ํ•˜๋‚˜์˜ ์ˆ˜์—ด์— ๋Œ€ํ•œ ๋ชจ๋“  ์—ฐ์‚ฐ์„ ๋งˆ์ณค์„ ๋•Œ๋Š” ๋ฒฝ๋Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ธ์–ด์ค€ ๋’ค ์ตœ์†Ÿ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๊ณ ,

map ๋ฐฐ์—ด์„ ๋‹ค์‹œ ์›์ƒ๋ณต๊ตฌ ํ•ด์ฃผ์–ด ๋‹ค์Œ ์ˆœ์—ด์— ๋Œ€ํ•œ ์—ฐ์‚ฐ์„ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ฒ˜์Œ์— ์ž˜๋ชป ์ƒ๊ฐํ•ด์„œ ์ค‘๋ณต ์ˆœ์—ด์ด ์•„๋‹Œ ์ค‘๋ณต ์กฐํ•ฉ์„ ๊ตฌํ–ˆ๋‹ค๊ฐ€,, ๋น™๋น™ ๋Œ์•„์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.ใ…œใ…œ
    ๊ตฌ์Šฌ๋“ค์„ ๊ฐ™์€ ์œ„์น˜์— ๋˜์ง€๋”๋ผ๋„ ์ˆœ์„œ๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๊ฒฐ๊ณผ๊ฐ€ ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์—ด๋กœ!! ํ•ด์•ผํ•œ๋‹ค.
  • ํšจ์œจ์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ ๋ณ„๋กœ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•˜๊ณ  ํ’€์ดํ–ˆ๋Š”๋ฐ, ๋ฒฝ๋Œ๋“ค์„ ๋ถ€์ˆœ ๋’ค ๋นˆ ์นธ์„ ์ฑ„์šฐ๋Š” ๊ณผ์ •์—์„œ
    ๋” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค. ๋‹ค์‹œ ํ•œ ๋ฒˆ ์ƒ๊ฐํ•ด๋ด์•ผ๊ฒ ๋‹ค.

๋Œ“๊ธ€