땀두 블로그

[백준] 1780번 - 종이의 수 본문

알고리즘/백준

[백준] 1780번 - 종이의 수

땀두 2022. 3. 22. 07:54

 

dfs로 풀 수 있는 문제이다. 종이를 탐색하고 첫 인덱스와 값이 다른 경우 9등분하여 함수 호출하는 부분을 반복하여서 문제를 해결하였다.

 

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

public class p1780 {
	public static int[][] ary;
	public static boolean[][] visited;
	public static int cnt;
	public static int cnt1;
	public static int cnt2;
	public static int a;

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		cnt = 0;
		cnt1 = 0;
		cnt2 = 0;

		a = Integer.parseInt(br.readLine());

		ary = new int[a][a];
		visited = new boolean[a][a];

		for (int i = 0; i < a; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			for (int j = 0; j < a; j++) {
				ary[i][j] = Integer.parseInt(st.nextToken());
			}
		}

		dfs(0, 0, a);

		System.out.println(cnt);
		System.out.println(cnt1);
		System.out.println(cnt2);
	}

	public static void dfs(int x, int y, int size) {
//		System.out.println(x + " " + y);
		int n = ary[x][y];
		int c = 0;

		if (size < 1) {
			return;
		}

		for (int i = x; i < x + size; i++) {
			for (int j = y; j < y + size; j++) {
				if (ary[i][j] != n) {
					int s = size / 3;

					dfs(x, y, s);
					dfs(x, y + s, s);
					dfs(x, y + 2 * s, s);
					dfs(x + s, y, s);
					dfs(x + s, y + s, s);
					dfs(x + s, y + 2 * s, s);
					dfs(x + s * 2, y, s);
					dfs(x + s * 2, y + s, s);
					dfs(x + s * 2, y + 2 * s, s);

					c++;

					return;
				}
			}
		}
		if (c == 0) {
			if (n == -1) {
				cnt++;
			} else if (n == 0) {
				cnt1++;
			} else {
				cnt2++;
			}
		}
		return;
	}
}
 

 

 

'알고리즘 > 백준' 카테고리의 다른 글

[백준] 11727번 - 2*n 타일링 2  (0) 2022.03.22
[백준] 1080번 - 행렬  (0) 2022.03.22
[백준] 1541번 - 잃어버린 괄호  (0) 2022.03.22
[백준] 15650번 - N과 M (2)  (0) 2022.03.22
[백준] 2579번 - 계단 오르기  (0) 2022.03.22
Comments