
소스코드:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static int N;
static char[][] board;
static int whiteCnt = 0;
static int blueCnt = 0;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
N = Integer.parseInt(br.readLine());
board = new char[N][N];
for (int i = 0; i < N; i++) {
board[i] = br.readLine().replaceAll(" ", "").toCharArray();
}
br.close();
solve(0, 0, N);
bw.write(whiteCnt + "\n" + blueCnt + "\n");
bw.flush();
bw.close();
}
private static boolean check(int x, int y, int size) {
char cur = board[y][x];
for (int i = 0; i < size; i++) {
for (int q = 0; q < size; q++) {
if (cur != board[y + i][x + q]) {
return false;
}
}
}
return true;
}
private static void solve(int x, int y, int size) throws IOException {
if (check(x, y, size)) {
if (board[y][x] == '0') {
whiteCnt++;
} else {
blueCnt++;
}
return;
}
final int cut = size / 2;
solve(x, y, cut);
solve(x + cut, y, cut);
solve(x, y + cut, cut);
solve(x + cut, y + cut, cut);
}
}
글의 내용 중 잘못된 점이나 수정이 필요한 부분, 혹은 궁금한 사항이 있다면 언제든 댓글로 남겨주시면 감사하겠습니다.
여러분의 피드백은 더 나은 글을 작성하는 데 큰 도움이 됩니다. 감사합니다.
'알고리즘 > 백준' 카테고리의 다른 글
| [JAVA-자바] 11399번: ATM (0) | 2025.03.06 |
|---|---|
| [JAVA-자바] 1654번: 랜선 자르기 (0) | 2025.03.05 |
| [JAVA-자바] 17219번: 비밀번호 찾기 (0) | 2025.03.04 |
| [JAVA-자바] 1874번: 스택 수열 (0) | 2025.03.03 |
| [JAVA-자바] 2108번: 통계학 (0) | 2025.03.03 |