View

๋ฐ˜์‘ํ˜•

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

 

2630๋ฒˆ: ์ƒ‰์ข…์ด ๋งŒ๋“ค๊ธฐ

์ฒซ์งธ ์ค„์—๋Š” ์ „์ฒด ์ข…์ด์˜ ํ•œ ๋ณ€์˜ ๊ธธ์ด N์ด ์ฃผ์–ด์ ธ ์žˆ๋‹ค. N์€ 2, 4, 8, 16, 32, 64, 128 ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ƒ‰์ข…์ด์˜ ๊ฐ ๊ฐ€๋กœ์ค„์˜ ์ •์‚ฌ๊ฐํ˜•์นธ๋“ค์˜ ์ƒ‰์ด ์œ—์ค„๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์ค„๊นŒ์ง€ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

 

๐Ÿ’ก ๋ฌธ์ œ ์ ‘๊ทผ ๋ฐฉ์‹ 

๋ฒ”์œ„๋ณ„๋กœ ๋ถ„ํ• ํ•ด์„œ ์žฌ๊ท€ ๋Œ๋ฆฌ๋Š” ํ˜•ํƒœ -> ๋ถ„ํ• ์ •๋ณต์œผ๋กœ ํ’€์–ด์•ผํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฌธ์ œ

- ๊ธฐ์ €์กฐ๊ฑด 1: ์ƒ‰์ข…์ด๊ฐ€ ๋”์ด์ƒ ๋‚˜๋ˆ ์ง€์ง€ ์•Š์„ ๋•Œ return

-๊ธฐ์ €์กฐ๊ฑด2: ๋ชจ๋“  ์นธ์˜ ์ƒ‰์ข…์ด ์ƒ‰์ด ๊ฐ™์„ ๋•Œ return

2. ๊ธฐ์ €์กฐ๊ฑด 2๋ฅผ ํŒŒ์•…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” booleanํƒ€์ž…์˜ ํ™•์ธ ์ ˆ์ฐจํ•„์š” -> isSame() ํ•จ์ˆ˜

3. 1์‚ฌ๋ถ„๋ฉด, 2์‚ฌ๋ถ„๋ฉด, 3์‚ฌ๋ถ„๋ฉด, 4์‚ฌ๋ถ„๋ฉด์œผ๋กœ ๋‚˜๋ˆ ์„œ ์žฌ๊ท€๋Œ๋ฆผ 

		// ๊ฐ™์€ ์ƒ‰๊น”์ด ์•„๋‹ˆ๋ผ๋ฉด ์ž๋ฅธ๋‹ค
		search(y, x, size / 2);
		search(y + size / 2, x, size / 2);
		search(y, x + size / 2, size / 2);
		search(y + size / 2, x + size / 2, size / 2);

 

๐Ÿ’ก ์ „์ฒด ์ฝ”๋“œ

package ๋ฐฑ์ค€;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main_2630 {
	static int[][] map;
	static int N;
	static int zero, one;
	static int[] dx = { -1, 1, 0, 0 };
	static int[] dy = { 0, 0, -1, 1 };
	static boolean visit[][];

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		map = new int[N][N];
		for (int i = 0; i < N; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			for (int j = 0; j < N; j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
			}
		} // ์ž…๋ ฅ ๋
		zero = 0;
		one = 0;
		search(0, 0, N);
		System.out.println(zero);
		System.out.println(one);

	}

	private static void search(int y, int x, int size) {
		// 1. ์ข…๋ฃŒ์กฐ๊ฑด 1-> ์‚ฌ์ด์ฆˆ๊ฐ€ 1์ผ ๋•Œ ์ž๋ฅผ ์ˆ˜ ์—†์Œ
		if (size == 1) {
			// 1์ผ๋•Œ-ํŒŒ๋ž€์ƒ‰
			if (map[y][x] == 1)
				one++;
			else {
				zero++;
			}
			return;
		}

		// 2. ์ข…๋ฃŒ์กฐ๊ฑด: ์ž˜๋ผ์ง„ ์ข…์ด๊ฐ€ ๋ชจ๋‘ ํ•œ๊ฐ€์ง€ ์ƒ‰๊น”๋กœ ์น ํ•ด์ ธ์žˆ์„ ๋•Œ
		if (isSame(y, x, map[y][x], size)) {
			if (map[y][x] == 1)
				one++;
			else {
				zero++;
			}
			return;
		}

		// ๊ฐ™์€ ์ƒ‰๊น”์ด ์•„๋‹ˆ๋ผ๋ฉด ์ž๋ฅธ๋‹ค
		search(y, x, size / 2);
		search(y + size / 2, x, size / 2);
		search(y, x + size / 2, size / 2);
		search(y + size / 2, x + size / 2, size / 2);

	}

	private static boolean isSame(int y, int x, int color, int size) {
		for (int i = y; i < y + size; i++) {
			for (int j = x; j < x + size; j++) {
				if (color != map[i][j])
					return false;
			}
		}
		return true;
	}

}
๋ฐ˜์‘ํ˜•
Share Link
reply
๋ฐ˜์‘ํ˜•
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30