본문 바로가기

알고리즘

백준 Z-1074

www.acmicpc.net/problem/1074

 

1074번: Z

한수는 2차원 배열 (항상 2^N * 2^N 크기이다)을 Z모양으로 탐색하려고 한다. 예를 들어, 2*2배열을 왼쪽 위칸, 오른쪽 위칸, 왼쪽 아래칸, 오른쪽 아래칸 순서대로 방문하면 Z모양이다. 만약, 2차원

www.acmicpc.net

단순 분할 정복 알고리즘을 사용했다

package BAEKJOON;

import java.util.Scanner;

public class TheZ_1074 {
	static long r;
	static long c;
	public static int where(long k, long y, long x) {
		if ((k / 2 + y > r) && (k / 2 + x > c)) {
			return 0;
		} else if ((k / 2 + y > r) && (k / 2 + x <= c)) {
			return 1;
		} else if ((k / 2 + y <= r) && (k / 2 + x > c)) {
			return 2;
		} else {
			return 3;
		}
	}
	
	public static long TheZ(long l, long y, long x) {
		if (l == 1) {
			return 0;
		} else {
			int quadrant = where(l, y, x);
			switch (quadrant) {
			case 0 : 
				return TheZ(l / 2, y, x);
			case 1 : 
				return Math.round(Math.pow(l / 2, 2)) + TheZ(l / 2, y, x + l / 2);
			case 2 : 
				return Math.round(Math.pow(l / 2, 2)) * 2 + TheZ(l / 2, y + l / 2, x);
			default: 
				return Math.round(Math.pow(l / 2, 2)) * 3 + TheZ(l / 2, y + l / 2, x + l / 2);
			}
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		r = sc.nextInt();
		c = sc.nextInt();
		sc.close();
		
		System.out.println(TheZ(Math.round(Math.pow(2, n)), 0, 0));
		
	}

}

'알고리즘' 카테고리의 다른 글

백준 별찍기-2448  (0) 2020.11.08
백준 토마토-7576  (0) 2020.11.08
백준 설탕 배달-2839  (0) 2020.11.08
백준 별 찍기10-2447  (0) 2020.11.08
백준 쿼드트리-1992  (0) 2020.11.08