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

[Java Algorithm] ์ฟผ๋“œํŠธ๋ฆฌ BOJ #1992

by seolhee2750 2022. 8. 18.
๋ฌธ์ œ

https://www.acmicpc.net/problem/1992

 

1992๋ฒˆ: ์ฟผ๋“œํŠธ๋ฆฌ

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

www.acmicpc.net

 

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

public class Main {
	
	static StringBuilder out = new StringBuilder();
	static int n;
	static String[][] map;
	
	public static boolean check(int depth, int x, int y) {
		String standard = map[x][y];
		for(int i = x; i < x+depth; i++) {
			for(int j = y; j < y+depth; j++) {
				if(!standard.equals(map[i][j])) return false;
			}
		}
		return true;
	}
	
	private static void QuadTree(int depth, int x, int y) {
		if(check(depth, x, y)) out.append(map[x][y]);
		else {
			out.append("(");
			depth /= 2;
			int[] dx = {0, 0, depth, depth};
			int[] dy = {0, depth, 0, depth};
			for(int i = 0; i < 4; i++) {
				QuadTree(depth, x+dx[i], y+dy[i]);
			}
			out.append(")");
		}
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		n = Integer.parseInt(in.readLine());
		map = new String[n][n];
		for(int i = 0; i < n; i++) {
			map[i] = in.readLine().split("");
		}
		
		QuadTree(n, 0, 0);
		System.out.println(out);
	}
}

๐Ÿ‘‰ ๋ถ„ํ•  ์ •๋ณต, ์žฌ๊ท€ ๋ฌธ์ œ

์ฃผ์–ด์ง„ 2์ฐจ์› ๋ฐฐ์—ด ํ˜•ํƒœ์˜ ์ž…๋ ฅ์„ 4๋“ฑ๋ถ„ํ•˜๊ณ , (0, 0)์—์„œ๋ถ€ํ„ฐ ๋ธ”๋ก์„ ์˜ฎ๊ฒจ๊ฐ€๋ฉฐ ์ฒดํฌํ•ด์ฃผ์—ˆ๋‹ค.

(๋ถ„ํ•  ์ •๋ณต์ด ์ต์ˆ™ํ•˜์ง€ ์•Š์€ ๋ถ„๋“ค์€ BOJ #1074 Z ๋ฌธ์ œ๋ฅผ ์ดํ•ดํ•˜์‹œ๋ฉด ๋” ํŽธํ• ๊ฑฐ์—์šฉ)

 

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

๋Œ“๊ธ€