풀기 전:
이 문제도 마찬가지로 2630 풀이에다가 간단한 수정만 해주면 되는데
이 문제같은 경우 9개로 나누어야 하기때문에 일일히 하나하나 나열해서 재귀함수를 쓰는 대신
반복문안에다가 재귀함수를 넣었다.
자바(JAVA)
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.util.Collections;
public class Main{
static int arr[][];
static int MinusCount;
static int ZeroCount;
static int PlusCount;
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 Divide(int startPosX,int startPosY,int n){
if(n==1){
if(arr[startPosX][startPosY]==-1){
MinusCount++;
}
else if(arr[startPosX][startPosY]==0){
ZeroCount++;
}
else {
PlusCount++;
}
return;
}
else if(isAllSame(startPosX,startPosY,n)){
if(arr[startPosX][startPosY]==-1){
MinusCount++;
}
else if(arr[startPosX][startPosY]==0){
ZeroCount++;
}
else {
PlusCount++;
}
return;
}
else{
for(int i=startPosX;i<startPosX+n;i+=n/3){
for(int j=startPosY;j<startPosY+n;j+=n/3){
Divide(i,j,n/3);
}
}
}
}
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();
}
}
Divide(0,0,n);
System.out.println(MinusCount);
System.out.println(ZeroCount);
System.out.println(PlusCount);
}
}
C++
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
int** arr;
int PlusCount = 0;
int ZeroCount = 0;
int MinusCount = 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 Divide(int startPosX, int startPosY, int n) {
if (n == 1) {
if (arr[startPosX][startPosY] == -1) {
MinusCount++;
}
else if (arr[startPosX][startPosY] == 0) {
ZeroCount++;
}
else {
PlusCount++;
}
return;
}
else if (isAllSame(startPosX, startPosY, n)) {
if (arr[startPosX][startPosY] == -1) {
MinusCount++;
}
else if (arr[startPosX][startPosY] == 0) {
ZeroCount++;
}
else {
PlusCount++;
}
return;
}
else {
for (int i = startPosX; i < startPosX + n; i += n / 3) {
for (int j = startPosY; j < startPosY + n; j += n / 3) {
Divide(i, j, n / 3);
}
}
}
}
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];
}
}
Divide(0, 0, n);
cout << MinusCount << '\n' << ZeroCount<<'\n'<<PlusCount;
}
6개월 후에 다시 풀었을때:
이전에 했던 방식과 유사하지만 조금더 늘어썼다. 이전에서 n==1일때의 경우는 isAllSame에 포함되기에 생략했다.
import java.io.*;
import java.lang.reflect.Array;
import java.nio.Buffer;
import java.util.*;
public class Main {
static int minus=0;
static int zero=0;
static int plus=0;
static int arr[][];
public static boolean isAllSame(int n,int first,int second){
boolean same=true;
int tmp=arr[first][second];
for(int i=first;i<first+n;i++){
for(int j=second;j<second+n;j++){
if(tmp!=arr[i][j]){
same=false;
}
}
}
return same;
}
public static void divide(int n,int first,int second){
if(isAllSame(n,first,second)){
if(arr[first][second]==-1){
minus++;
}
else if(arr[first][second]==0){
zero++;
}
else{
plus++;
}
}
else{
divide(n/3,first,second);
divide(n/3,first,second+(n/3));
divide(n/3,first,second+(n/3)*2);
divide(n/3,first+(n/3),second);
divide(n/3,first+(n/3),second+(n/3));
divide(n/3,first+(n/3),second+(n/3)*2);
divide(n/3,first+(n/3)*2,second);
divide(n/3,first+(n/3)*2,second+(n/3));
divide(n/3,first+(n/3)*2,second+(n/3)*2);
}
}
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(minus);
System.out.println(zero);
System.out.println(plus);
}
}