알고리즘/백준

[백준 / #1074] Z

셩윤 2024. 2. 18. 23:09

문제 설명

https://www.acmicpc.net/problem/1074

 

1074번: Z

한수는 크기가 2N × 2N인 2차원 배열을 Z모양으로 탐색하려고 한다. 예를 들어, 2×2배열을 왼쪽 위칸, 오른쪽 위칸, 왼쪽 아래칸, 오른쪽 아래칸 순서대로 방문하면 Z모양이다. N > 1인 경우, 배열을

www.acmicpc.net

 

 


문제풀이

- size를 반씩 줄이면서 인덱스가 몇 사분면인지 파악하여 각 사분면마다 해당하는 사이즈x사이즈x사분면을 count에다 더해준다.
- 각 해당하는 구역의 size만큼 인덱스 조정을 해준다.
- size가 1이되면 count를 return한다.
- 그렇지 않으면 size와 변한 인덱스를 다시 재귀한다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Ac_1074 {
	static int x = 0;
	static int y = 0;
	static int count = 0;
	
	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());
		int r = Integer.parseInt(st.nextToken());
		int c = Integer.parseInt(st.nextToken());
		int result = Z((int)Math.pow(2, N), r, c);
		System.out.println(result);
	}
	
	public static int Z(int n, int r, int c) {
		n /= 2;
		if(r < x + n && c < y + n) {
			count += (n * n * 0);
		}else if(r < x + n && c >= y + n) {
			count += (n * n * 1);
			y += n;
		}else if(c < y + n) {
			count += (n * n * 2);
			x += n;
		}else {
			count += (n * n * 3);
			x += n;
			y += n;
		}
		if(n == 1) return count;
		return Z(n, r, c);
	}

}