N×N크기의 행렬로 표현되는 종이가 있다. 종이의 각 칸에는 -1, 0, 1의 세 값 중 하나가 저장되어 있다. 우리는 이 행렬을 적절한 크기로 자르려고 하는데, 이때 다음의 규칙에 따라 자르려고 한다.
만약 종이가 모두 같은 수로 되어 있다면 이 종이를 그대로 사용한다.
(1)이 아닌 경우에는 종이를 같은 크기의 9개의 종이로 자르고, 각각의 잘린 종이에 대해서 (1)의 과정을 반복한다.
이와 같이 종이를 잘랐을 때, -1로만 채워진 종이의 개수, 0으로만 채워진 종이의 개수, 1로만 채워진 종이의 개수를 구해내는 프로그램을 작성하시오.
첫째 줄에 N(1 ≤ N ≤ 37, N은 3k 꼴)이 주어진다. 다음 N개의 줄에는 N개의 정수로 행렬이 주어진다.
첫째 줄에 -1로만 채워진 종이의 개수를, 둘째 줄에 0으로만 채워진 종이의 개수를, 셋째 줄에 1로만 채워진 종이의 개수를 출력한다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static StringBuilder builder = new StringBuilder();
public static int zero = 0;
public static int minus = 0;
public static int one = 0;
public static String[][] board;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(reader.readLine());
StringTokenizer tokenizer;
board = new String[N][N];
for (int i = 0; i < N; i++) {
// tokenizer = new StringTokenizer(reader.readLine());
board[i] = reader.readLine().split(" ");
}
partition(0, 0, N);
System.out.println(minus);
System.out.println(zero);
System.out.println(one);
}
public static void partition(int row, int col, int size) {
if(colorCheck(row, col, size)) {
if(board[row][col].equals("0")) {
zero++;
}
else if(board[row][col].equals("1"))
{
one++;
}
else {
minus++;
}
return;
}
int newSize = size / 3;
partition(row, col, newSize);
partition(row, col + newSize, newSize);
partition(row, col + (newSize*2), newSize);
partition(row + newSize, col, newSize);
partition(row + newSize, col + newSize, newSize);
partition(row + newSize, col + (newSize*2), newSize);
partition(row + (newSize*2), col, newSize);
partition(row + (newSize*2), col + newSize, newSize);
partition(row + (newSize*2), col + (newSize*2), newSize);
}
public static boolean colorCheck(int row, int col, int size) {
String color = board[row][col];
for (int i = row; i < row + size; i++) {
for (int j = col; j < col + size; j++) {
if (!board[i][j] .equals(color)) {
return false;
}
}
}
return true;
}
}
이렇게 구현했을때 시간이 1400ms대가 나왔다.
다른 분들보다 거의 두배 가량 많이 나왕ㅅ다.
이유가 뭘까 비교해보니
다른 부분을 바꿨을때는 크게 차이가 없었는데
board = new String[N][N];
for (int i = 0; i < N; i++) {
board[i] = reader.readLine().split(" ");
}
처음에 배열을 받을때 String으로 받고
int형이 필요할때 형변환을 했다.
board = new int[N][N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(reader.readLine());
for (int j = 0; j < N; ++j) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
요렁 식으로 바꾸고 형변환을 없애니
크게 시간이 감소 했다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static StringBuilder builder = new StringBuilder();
static int[] cnt_arr;
public static int[][] board;
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(reader.readLine());
cnt_arr = new int[3];
board = new int[N][N];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(reader.readLine());
for (int j = 0; j < N; ++j) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
partition(0, 0, N);
for (int n : cnt_arr) {
System.out.println(n);
}
reader.close();
}
public static void partition(int row, int col, int size) {
if ( colorCheck(row, col, size) ) {
cnt_arr[board[row][col]+1]++;
}
else {
int newSize = size / 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
partition( row+newSize*i,col+newSize*j, newSize);
}
}
}
}
public static boolean colorCheck(int row, int col, int size) {
int color = board[row][col];
for (int i = row; i < row + size; i++) {
for (int j = col; j < col + size; j++) {
if (board[i][j]!=color) {
return false;
}
}
}
return true;
}
}