풀기 전:
4개의 분할로 계속 나누어서 하나의 덩어리가 될때까지 쪼개는 것을 반복하면 될 것같다.
그것을 바탕으로 문제를 풀어 보겠다.
자바(JAVA)
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.Collections;
public class Main{
static int arr[][];
static int BlueCount;
static int WhiteCount;
public static boolean isAllSame(int startPosX,int startPosY,int n){
int firstNum=arr[startPosX][startPosY];
boolean Same=true;
for(int i=startPosX;i<startPosX+n;i++){
for(int j=startPosY;j<startPosY+n;j++){
if(firstNum!=arr[i][j]){
Same=false;
break;
}
}
}
return Same;
}
public static void DivideBlue(int startPosX,int startPosY,int n){
if(n==1){
if(arr[startPosX][startPosY]==1){
BlueCount++;
}
else {
WhiteCount++;
}
return;
}
else if(isAllSame(startPosX,startPosY,n)){
if(arr[startPosX][startPosY]==1) {
BlueCount++;
}
else{
WhiteCount++;
}
return;
}
else{
DivideBlue(startPosX,startPosY,n/2);
DivideBlue(startPosX+n/2,startPosY,n/2);
DivideBlue(startPosX,startPosY+n/2,n/2);
DivideBlue(startPosX+n/2,startPosY+n/2,n/2);
}
}
public static void main(String[] args) throws IOException {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
arr=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
arr[i][j]=scanner.nextInt();
}
}
DivideBlue(0,0,n);
System.out.println(WhiteCount);
System.out.println(BlueCount);
}
}
C++
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
int** arr;
int BlueCount = 0;
int WhiteCount = 0;
bool isAllSame(int startPosX, int startPosY, int n) {
int firstNum = arr[startPosX][startPosY];
bool Same = true;
for (int i = startPosX; i < startPosX + n; i++) {
for (int j = startPosY; j < startPosY + n; j++) {
if (firstNum != arr[i][j]) {
Same = false;
break;
}
}
}
return Same;
}
void DivideBlue(int startPosX, int startPosY, int n) {
if (n == 1) {
if (arr[startPosX][startPosY] == 1) {
BlueCount++;
}
else {
WhiteCount++;
}
return;
}
else if (isAllSame(startPosX, startPosY, n)) {
if (arr[startPosX][startPosY] == 1) {
BlueCount++;
}
else {
WhiteCount++;
}
return;
}
else {
DivideBlue(startPosX, startPosY, n / 2);
DivideBlue(startPosX + n / 2, startPosY, n / 2);
DivideBlue(startPosX, startPosY + n / 2, n / 2);
DivideBlue(startPosX + n / 2, startPosY + n / 2, n / 2);
}
}
int main(void) {
int n;
cin >> n;
arr = new int* [n];
for (int i = 0; i < n; i++) {
arr[i] = new int[n];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> arr[i][j];
}
}
DivideBlue(0, 0, n);
cout << WhiteCount << '\n' << BlueCount;
}
6개월후에 다시 풀었을 때:
일단 의식의 흐름대로 풀었다. 위처럼 이쁜 형식으로 풀 수 있었는데 의식의 흐름으로 풀다가 바꾸려했지만 그냥 얼마 안걸릴것같아서 밀고 나갔다.
하지만 다음부터는 위처럼 이쁘게 풀어야 한다. 코드가길어지면 실수할 여지가 매우 많아 지기 때문이다.
import java.io.*;
import java.lang.reflect.Array;
import java.nio.Buffer;
import java.util.*;
public class Main {
static int white=0;
static int blue=0;
static int arr[][];
public static void divide(int n,int first,int second){
boolean needDivide=false;
int firstThing=arr[first][second];
for(int i=first;i<first+n;i++){
for(int j=second;j<second+n;j++){
if(firstThing!=arr[i][j]){
needDivide=true;
}
}
}
if(!needDivide){
if(firstThing==0){
white++;
}
else{
blue++;
}
}
else {
int tmp = arr[first][second];
boolean firstNeedMore = false;
for (int i = first; i < first + (n / 2); i++) {
for (int j = second; j < second + (n / 2); j++) {
if (tmp != arr[i][j]) {
firstNeedMore = true;
}
}
}
if (firstNeedMore) {
divide(n / 2, first, second);
} else {
if (tmp == 0) {
white++;
} else {
blue++;
}
}
tmp = arr[first][second + (n / 2)];
boolean secondNeedMore = false;
for (int i = first; i < first + (n / 2); i++) {
for (int j = second + (n / 2); j < second+n; j++) {
if (tmp != arr[i][j]) {
secondNeedMore = true;
}
}
}
if (secondNeedMore) {
divide(n / 2, first, second + (n / 2));
} else {
if (tmp == 0) {
white++;
} else {
blue++;
}
}
tmp = arr[first + (n / 2)][second];
boolean thirdNeedMore = false;
for (int i = first + (n / 2); i < first+n; i++) {
for (int j = second; j < second + (n / 2); j++) {
if (tmp != arr[i][j]) {
thirdNeedMore = true;
}
}
}
if (thirdNeedMore) {
divide(n / 2, first + (n / 2), second);
} else {
if (tmp == 0) {
white++;
} else {
blue++;
}
}
tmp = arr[first + (n / 2)][second + (n / 2)];
boolean fourthNeedMore = false;
for (int i = first + (n / 2); i <first+ n; i++) {
for (int j = second + (n / 2); j <second+ n; j++) {
if (tmp != arr[i][j]) {
fourthNeedMore = true;
}
}
}
if (fourthNeedMore) {
divide(n / 2, first + (n / 2), second + (n / 2));
} else {
if (tmp == 0) {
white++;
} else {
blue++;
}
}
}
}
public static void main(String[] args) throws IOException {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
arr=new int[n+1][n+1];
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
arr[i][j]= scanner.nextInt();
}
}
divide(n,1,1);
System.out.println(white);
System.out.println(blue);
}
}