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 | 
													Tags
													
											
												
												- 01BFS
 - YBMCOS
 - PS
 - 백준코딩테스트
 - 세그먼트트리
 - COSPRO
 - QUICKSTARTGUIDE
 - DFS
 - 우선순위큐
 - 시뮬레이션
 - 다익스트라
 - 취득후기
 - 백준
 - 구현
 - 재귀함수
 - deque
 - 알고리즘
 - GatherTown
 - dp
 - spring
 - java
 - 네트워크플로우
 - 엘라스틱서치
 - BFS
 - COSPROJAVA1급
 - 완전탐색
 - 게더타운시작
 - 자바PS
 - 이젠 골드구현도 어렵네..
 - 다이나믹프로그래밍
 
													Archives
													
											
												
												- Today
 
- Total
 
공부공간
BOJ-2630 ) 색종이 만들기 본문



https://www.acmicpc.net/problem/2630
2630번: 색종이 만들기
첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.
www.acmicpc.net
현재좌표와 한변의 길이를 재귀함수의 인자값으로 받는다, 한변의 길이가 N이라면 N>>1씩 쪼개서
현재범위가 같은 색상인지 판단한다. 같은색상이라면, 탐색을 멈추고 다른색상이라면 4등분하여 탐색을 진행한다.
package algorithm_2022;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BOJ_2630 {
	public static int map[][],blue,white;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int N = Integer.parseInt(st.nextToken());
		map=new int[N][N];
		for(int i=0;i<N;i++) {
			st = new StringTokenizer(br.readLine());
			for(int j=0;j<N;j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		recursion(0,0,N);
		System.out.println(white+"\n"+blue);
	}
	private static void recursion(int y,int x, int n) {
		boolean isAllSame = true;
		int init = map[y][x];
		for(int i=y;i<y+n;i++) {
			for(int j=x;j<x+n;j++) {
				if(map[i][j]!=init) {
					isAllSame=false;
				}
			}
			if(!isAllSame) break;
		}
		if(isAllSame) {
			init = (init==1) ? blue++ : white++;
			return;
		} else {
			recursion(y,x,n>>1);
			recursion(y,x+(n>>1),n>>1);
			recursion(y+(n>>1),x,n>>1);
			recursion(y+(n>>1),x+(n>>1),n>>1);
			return;
		}
	}
}

'알고리즘' 카테고리의 다른 글
| BOJ-1074 ) Z (0) | 2022.02.13 | 
|---|---|
| BOJ-11723 ) 집합 (0) | 2022.02.06 | 
| BOJ-1021 ) 회전하는 큐 (1) | 2022.02.03 | 
			  Comments