• 문제 링크
11867번: 박스 나누기 게임
첫째 줄에 N과 M이 주어진다. (1 ≤ N, M ≤ 100, N과 M이 모두 1인 경우는 없다)
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
static int[][] cache = new int[101][101];
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read(), m = read();
bw.write((n < m ? recur(n, m) : recur(m, n)) == 1 ? "A" : "B");
bw.flush();
}
private static int recur(int a, int b) {
if (a == 2 || b == 2) return 1;
if (cache[a][b] != 0) return cache[a][b];
cache[a][b] = -1;
for (int i = 1; i <= a >> 1; i++) if (recur(i, a - i) == -1) return cache[a][b] = 1;
for (int i = 1; i <= b >> 1; i++) if (recur(i, b - i) == -1) return cache[a][b] = 1;
return cache[a][b];
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 6221 The Bale Tower - Dynamic Programming / Java (0) | 2024.01.20 |
---|---|
[백준] 10752 Cow Hopscotch - Dynamic Programming / Java (0) | 2024.01.19 |
[백준] 4811 알약 - Dynamic Programming / Java (0) | 2024.01.17 |
[백준] 25632 소수 부르기 게임 - Greedy / Java (0) | 2024.01.16 |
[백준] 31066 비 오는 날 - Greedy / Java (0) | 2024.01.15 |
댓글