Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Java
- 완주하지못한선수
- 특정인덱스바꾸기
- 타겟넘버
- 백양로브레이크
- startswith
- 알고리즘
- 자바
- SSAFY
- 진수 int형으로
- 스타일리쉬들여쓰기
- 전화번호목록
- 단어변환
- 타도
- 객체정렬
- 11562
- git
- 프로그래머스
- 7699
- 명령어
- 2579
- 백준
- toCharArray()
- 프로젝트
- 그래프adt
- SWEA
- K번째수
- 10580번
- 시작
- django
Archives
- Today
- Total
합리적 낙관주의자
Baekjoon 1992번: 쿼드트리 (분할정복) 본문
1992번: 쿼드트리
첫째 줄에는 영상의 크기를 나타내는 숫자 N 이 주어진다. N 은 언제나 2의 제곱수로 주어지며, 1≤N ≤64의 범위를 가진다. 두 번째 줄부터는 길이 N 의 문자열이 N 개 들어온다. 각 문자열은 0 또는
www.acmicpc.net
모두 같은 수가 아닐경우, 모두 같은 숫자가 존재할때까지 정사각형을 4등분해서 확인하는 분할문제
처음부터 이차원 행렬이 모두 같은 수 일때, ( )없이 숫자 하나만 나올 수 있도록 하는 부분에서 버벅거렸다. 문제를 제대로 잘 읽어야함.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//1992번 쿼드트리
public class Main {
static char[][] map;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
map = new char [N][N];
boolean flag = true;
for (int i = 0; i < N; i++) {
String tmp = br.readLine();
for (int j = 0; j < tmp.length(); j++) {
map[i][j] = tmp.charAt(j);
if(map[i][j] != map[0][0]) flag = false;
}
}//end of for
if (!flag) {
System.out.print("(");
check(0, 0, N / 2);
check(0, N / 2, N / 2);
check(N / 2 , 0, N / 2);
check(N / 2 , N / 2, N / 2);
System.out.print(")");
} else {
System.out.println(map[0][0]);
}
}//end of main
// 모두 같은 원소인지 확인하는 메소드
// x,y 시작위치 size 정사각형 사이즈
public static void check(int x, int y, int size) {
if(size == 1) {
System.out.print(map[x][y]);
return;
}
char fix = map[x][y];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (map[x+i][y+j] == fix) continue;
System.out.print("(");
check(x, y, size / 2);
check(x, y + size / 2, size / 2);
check(x + size / 2, y, size / 2);
check(x + size / 2, y + size / 2, size / 2);
System.out.print(")");
return;
}
}
System.out.print(map[x][y]);
}//end of solution
/*
8
11110000
11110000
00011100
00011100
11110000
11110000
11110011
11110011
8
00000000
00000000
00001111
00001111
00011111
00111111
00111111
00111111
8
00000000
00000000
00000000
00000000
00000000
00000000
00000000
00000000
8
11111111
11111111
11111111
11111111
11111111
11111111
11111111
11111111
*/
}//end of class
'Computer Thinking 🌟 > Algorithm 📝' 카테고리의 다른 글
Baekjoon 1780번: 종이의 개수 (분할정복) (0) | 2020.10.29 |
---|---|
Programmers [정렬] K번째수 (0) | 2020.10.23 |
Programmers 주식가격: Stack/Queue (0) | 2020.09.11 |
SWEA [D3] 10505번: 소득 불균형 (0) | 2020.08.31 |
SWEA [D3] 10580번: 전봇대 / Collections.sort()와 Arrays.sort()의 차이 (0) | 2020.08.31 |