![[백준 1074번 / Java] Z](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FvY5ML%2FbtsmGh1mrvZ%2FkV5h7MdiDs97B4nPHUy4l0%2Fimg.png)
[백준 1074번 / Java] Z코딩테스트/백준2023. 7. 7. 19:56
Table of Contents
728x90
728x90
문제 링크
Silver 1
https://www.acmicpc.net/problem/1074
1074번: Z
한수는 크기가 2N × 2N인 2차원 배열을 Z모양으로 탐색하려고 한다. 예를 들어, 2×2배열을 왼쪽 위칸, 오른쪽 위칸, 왼쪽 아래칸, 오른쪽 아래칸 순서대로 방문하면 Z모양이다. N > 1인 경우, 배열을
www.acmicpc.net
풀이
단순 구현문제라고 생각했고 재귀를 통해 풀었다.
문제에서 2^N의 판이 주어진다고 했기 때문에 재귀 조건을 반대로 2^N의 수에서 /2를 해가며 1이나올 때 까지 반복했다.
그렇게 하면 초기 1칸일 때의 Z배열까지 반복을 돌릴 수 있다.
과정에서, 현재 [r,c]가 4분면 중 어디에 속해있는지만 알 수 있으면, 값을 더해주기만 하면 된다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class BaekJoon1074 {
private static int cnt = 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 where = (int) Math.pow(2 ,N);
findWhere(where, r, c);
System.out.println(cnt);
}
private static void findWhere(int where, int r, int c) {
if(where == 1) return;
if(r < where/2 && c < where/2) {
findWhere(where/2, r, c);
}
else if(r < where/2 && c >= where/2) {
cnt += where * where / 4;
findWhere(where/2, r, c - where/2);
}
else if(r >= where/2 && c < where/2) {
cnt += (where * where / 4) * 2;
findWhere(where/2, r - where/2, c);
}
else {
cnt += (where * where / 4) * 3;
findWhere(where/2, r - where/2, c - where/2);
}
}
}
728x90
300x250
@mag1c :: 꾸준히 재밌게
2023.04 ~ 백엔드 개발자의 기록
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!