반응형
1780번: 종이의 개수
N×N크기의 행렬로 표현되는 종이가 있다. 종이의 각 칸에는 -1, 0, 1의 세 값 중 하나가 저장되어 있다. 우리는 이 행렬을 적절한 크기로 자르려고 하는데, 이때 다음의 규칙에 따라 자르려고 한다. 만약 종이가 모두 같은 수로 되어 있다면 이 종이를 그대로 사용한다. (1)이 아닌 경우에는 종이를 같은 크기의 9개의 종이로 자르고, 각각의 잘린 종이에 대해서 (1)의 과정을 반복한다. 이와 같이 종이를 잘랐을 때, -1로만 채워진 종이의 개수, 0으
www.acmicpc.net
색종이 만들기랑 동일한 방식입니다.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class Main { | |
static int[][] field; | |
static int[] count = new int[3]; // 0: -1, 1: 0, 2: 1 | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
int n = sc.nextInt(); | |
field = new int[n][n]; | |
for(int i=0; i<n; i++) | |
for(int j=0; j<n; j++) | |
field[i][j] = sc.nextInt(); | |
coleredPaper(n, 0, 0); | |
System.out.print(count[0]+"\n"+count[1]+"\n"+count[2]); | |
} | |
static void coleredPaper(int n, int x, int y){ | |
if(isSame(n, x, y)){ | |
count[field[y][x]+1]++; | |
return; | |
} | |
int m = n/3; | |
coleredPaper(m, x, y); // 상좌 | |
coleredPaper(m, x+m, y); // 상중 | |
coleredPaper(m, x+m*2, y); // 상우 | |
coleredPaper(m, x, y+m); // 중좌 | |
coleredPaper(m, x+m, y+m); // 중중 | |
coleredPaper(m, x+m*2, y+m); // 중우 | |
coleredPaper(m, x, y+m*2); // 하좌 | |
coleredPaper(m, x+m, y+m*2); // 하중 | |
coleredPaper(m, x+m*2, y+m*2); // 하우 | |
} | |
static boolean isSame(int n, int x, int y){ | |
int criteria = field[y][x]; | |
for(int i=y; i<y+n; i++) | |
for(int j=x; j<x+n; j++) | |
if (criteria!=field[i][j]) return false; | |
return true; | |
} | |
} |

반응형
'Algorithm' 카테고리의 다른 글
프로그래머스 코딩테스트 연습 고득점kit 완주하지 못한 선수 [ Java ] (0) | 2020.03.07 |
---|---|
백준 9375번 패션왕 신해빈 [ Java ] (0) | 2020.03.07 |
백준 1992번 쿼드트리 [ Java ] (0) | 2020.03.04 |
백준 2630번 색종이 만들기 [ Java ] (0) | 2020.03.04 |
프로그래머스 코딩테스트 연습 Level2 - 소수찾기 [ Java ] (0) | 2020.03.03 |