01-09 04:37
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๋ถ„ํ• ์ •๋ณต 1992 :: ์ฟผ๋“œํŠธ๋ฆฌ ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๋ถ„ํ• ์ •๋ณต 1992 :: ์ฟผ๋“œํŠธ๋ฆฌ

miinsun 2022. 4. 10. 19:51

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

ํ‘๋ฐฑ ์˜์ƒ์„ ์••์ถ•ํ•˜์—ฌ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ ์ฟผ๋“œ ํŠธ๋ฆฌ(Quad Tree)๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ํฐ ์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” 0๊ณผ ๊ฒ€์€ ์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” 1๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์˜์ƒ(2์ฐจ์› ๋ฐฐ์—ด)์—์„œ ๊ฐ™์€ ์ˆซ์ž์˜ ์ ๋“ค์ด ํ•œ ๊ณณ์— ๋งŽ์ด ๋ชฐ๋ ค์žˆ์œผ๋ฉด, ์ฟผ๋“œ ํŠธ๋ฆฌ์—์„œ๋Š” ์ด๋ฅผ ์••์ถ•ํ•˜์—ฌ ๊ฐ„๋‹จํžˆ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฃผ์–ด์ง„ ์˜์ƒ์ด ๋ชจ๋‘ 0์œผ๋กœ๋งŒ ๋˜์–ด ์žˆ์œผ๋ฉด ์••์ถ• ๊ฒฐ๊ณผ๋Š” "0"์ด ๋˜๊ณ , ๋ชจ๋‘ 1๋กœ๋งŒ ๋˜์–ด ์žˆ์œผ๋ฉด ์••์ถ• ๊ฒฐ๊ณผ๋Š” "1"์ด ๋œ๋‹ค. ๋งŒ์•ฝ 0๊ณผ 1์ด ์„ž์—ฌ ์žˆ์œผ๋ฉด ์ „์ฒด๋ฅผ ํ•œ ๋ฒˆ์— ๋‚˜ํƒ€๋‚ด์ง€๋ฅผ ๋ชปํ•˜๊ณ , ์™ผ์ชฝ ์œ„, ์˜ค๋ฅธ์ชฝ ์œ„, ์™ผ์ชฝ ์•„๋ž˜, ์˜ค๋ฅธ์ชฝ ์•„๋ž˜, ์ด๋ ‡๊ฒŒ 4๊ฐœ์˜ ์˜์ƒ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์••์ถ•ํ•˜๊ฒŒ ๋˜๋ฉฐ, ์ด 4๊ฐœ์˜ ์˜์—ญ์„ ์••์ถ•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๊ด„ํ˜ธ ์•ˆ์— ๋ฌถ์–ด์„œ ํ‘œํ˜„ํ•œ๋‹ค
์œ„ ๊ทธ๋ฆผ์—์„œ ์™ผ์ชฝ์˜ ์˜์ƒ์€ ์˜ค๋ฅธ์ชฝ์˜ ๋ฐฐ์—ด๊ณผ ๊ฐ™์ด ์ˆซ์ž๋กœ ์ฃผ์–ด์ง€๋ฉฐ,
์ด ์˜์ƒ์„ ์ฟผ๋“œ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์••์ถ•ํ•˜๋ฉด "(0(0011)(0(0111)01)1)"๋กœ ํ‘œํ˜„๋œ๋‹ค.

  N ×N ํฌ๊ธฐ์˜ ์˜์ƒ์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ด ์˜์ƒ์„ ์••์ถ•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

  • ์ฒซ์งธ ์ค„์—๋Š” ์˜์ƒ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž N ์ด ์ฃผ์–ด์ง„๋‹ค.
  • N ์€ ์–ธ์ œ๋‚˜ 2์˜ ์ œ๊ณฑ์ˆ˜๋กœ ์ฃผ์–ด์ง€๋ฉฐ, 1 ≤ N ≤ 64์˜ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง„๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ๋Š” ๊ธธ์ด N์˜ ๋ฌธ์ž์—ด์ด N๊ฐœ ๋“ค์–ด์˜จ๋‹ค.
  • ๊ฐ ๋ฌธ์ž์—ด์€ 0 ๋˜๋Š” 1์˜ ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ์˜์ƒ์˜ ๊ฐ ์ ๋“ค์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

์ถœ๋ ฅ

  • ์˜์ƒ์„ ์••์ถ•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

8
11110000
11110000
00011100
00011100
11110000
11110000
11110011
11110011

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

((110(0101))(0010)1(0001))

 

โ€‹

๐Ÿ’ป  Main.java

 

  • (x, y), (x, y + n/2), (x + n/2, y), (x + n/2, y + n/2) 4๊ตฌ์—ญ์œผ๋กœ ๋‚˜๋ˆ ์„œ ํŒŒํ‹ฐ์…˜ ๊ฒ€์‚ฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค.
  • ์••์ถ•์ด ๊ฐ€๋Šฅํ•˜๋ฉด answer์— ์••์ถ• ๊ฒฐ๊ณผ๋ฅผ ํ•ฉํ•œ๋‹ค.
  • ์••์ถ•์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉด ๊ด„ํ˜ธ๋ฅผ ์—ด๊ณ  ๋ฒ”์œ„ ๋ณ„๋กœ ํŒŒํ‹ฐ์…˜ ๊ฒ€์‚ฌ ์žฌ๊ท€๋ฅผ ์‹œ์ž‘ํ•œ๋‹ค.
  • ๊ฐ ํŒŒํ‹ฐ์…˜๋ณ„๋กœ ์žฌ๊ท€๊ฐ€ ๋๋‚ ๋•Œ๋งˆ๋‹ค ๊ด„ํ˜ธ๋ฅผ ๋‹ซ์•„์ค€๋‹ค.

 

/* ๋ฐฑ์ค€ ๋ถ„ํ• ์ •๋ณต - 1992 :: ์ฟผ๋“œํŠธ๋ฆฌ */
import java.io.*;

public class Main {
	static int[][] arr;
	static String answer = "";
	public static void partition(int x, int y, int size) {
		if (check(x, y, size)) {
			if(arr[x][y] == 1) { 
				answer += "1";
			}
			else if(arr[x][y] == 0) {
				answer += "0";
			}
 
			return;
		}
 
		int newSize = size / 2;
		
		answer += "(";

		partition(x, y, newSize);						// ์™ผ์ชฝ ์œ„
		partition(x, y + newSize, newSize);				// ์˜ค๋ฅธ์ชฝ ์œ„
		
		partition(x + newSize, y, newSize);				// ์™ผ์ชฝ ์•„๋ž˜
		partition(x + newSize, y + newSize, newSize);	// ์˜ค๋ฅธ์ชฝ ์•„๋ž˜
		
		answer += ")";
 
	}
	
	static boolean check(int x, int y, int size) {
		int base = arr[x][y];
		
		for(int i = x; i < x + size; i++) {
			for(int j = y; j < y + size; j++) {
				if(base != arr[i][j]) {
					return false;
				}
			}
		}
		
		return true;
	}
	
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in ));
		int n = Integer.parseInt(br.readLine());
		
		arr = new int[n][n];
		
		for(int i = 0; i < n; i++) {
			char[] tmp = br.readLine().toCharArray();
			for(int j = 0; j < n; j++)
				arr[i][j] = tmp[j] - '0';
		}
		
		partition(0, 0, n);
		
		System.out.println(answer);
		
		br.close();
	}
}
Comments