728x90
반응형
https://www.acmicpc.net/problem/1992
1992번: 쿼드트리
첫째 줄에는 영상의 크기를 나타내는 숫자 N 이 주어진다. N 은 언제나 2의 제곱수로 주어지며, 1 ≤ N ≤ 64의 범위를 가진다. 두 번째 줄부터는 길이 N의 문자열이 N개 들어온다. 각 문자열은 0 또
www.acmicpc.net
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static int n;
public static int m;
public static int[][] arr;
public static int[] totalNum = new int[3];
public static StringBuilder sb = new StringBuilder();
public static int check(int row, int col, int n) {
int standard = arr[row][col];
for (int i = row; i < row + n; i++) {
for (int j = col; j < col + n; j++) {
if (standard != arr[i][j]) {
return -1;
}
}
}
return standard;
}
public static void divide(int row, int col, int n) {
if (check(row, col, n) != -1) {
sb.append(check(row, col, n));
} else {
// 분할
sb.append("(");
int num = n / 2;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
divide(row + (i * num), col + (j * num), num);
}
}
sb.append(")");
}
}
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
arr = new int[n][n];
// 공백 없이 붙어있어서 문자열로 입력받아줘야
for (int i = 0; i < n; i++) {
String str = sc.next();
for (int j = 0; j < n; j++) {
arr[i][j] = str.charAt(j) - '0';
}
}
divide(0, 0, n);
System.out.println(sb.toString());
}
}
|
cs |
사실상 1780번이랑 똑같은 문제다. 그저 입력받을 때 공백이 없기에 문자열로 입력받은 후 charAt을 이용해 하나씩 잘라줘야한다.
https://we1cometomeanings.tistory.com/235
[java백준] 실버 2/ 1780번 종이의 개수
https://www.acmicpc.net/problem/1780 1780번: 종이의 개수 N×N크기의 행렬로 표현되는 종이가 있다. 종이의 각 칸에는 -1, 0, 1 중 하나가 저장되어 있다. 우리는 이 행렬을 다음과 같은 규칙에 따라 적절한
we1cometomeanings.tistory.com
728x90
반응형
'알고리즘 > 분할정복' 카테고리의 다른 글
[java 백준] plat 5/ 1517번 버블정렬 (0) | 2022.02.05 |
---|---|
[java 백준]골드 4/2448번 별 찍기 -11 (0) | 2022.02.03 |
[java 백준] 실버 1/별찍기 -10 (0) | 2022.01.30 |
[java백준] 실버 2/ 1780번 종이의 개수 (0) | 2022.01.28 |
[java 백준] 실버 5/11728번 배열 합치기 (0) | 2022.01.28 |
댓글