|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.StringTokenizer; |
| 5 | + |
| 6 | +public class BJ_11062_카드_게임 { |
| 7 | + |
| 8 | + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 10 | + private static final StringBuilder sb = new StringBuilder(); |
| 11 | + private static StringTokenizer st; |
| 12 | + |
| 13 | + private static int N; |
| 14 | + |
| 15 | + private static int[] cards = new int[1001]; |
| 16 | + private static boolean[] visited = new boolean[1001]; |
| 17 | + private static int[][] dp = new int[1001][1001]; |
| 18 | + |
| 19 | + public static void main(String[] args) throws IOException { |
| 20 | + int T = Integer.parseInt(br.readLine()); |
| 21 | + |
| 22 | + while (T-- > 0) { |
| 23 | + init(); |
| 24 | + sol(); |
| 25 | + } |
| 26 | + bw.write(sb.toString()); |
| 27 | + bw.flush(); |
| 28 | + bw.close(); |
| 29 | + br.close(); |
| 30 | + } |
| 31 | + |
| 32 | + private static void init() throws IOException { |
| 33 | + N = Integer.parseInt(br.readLine()); |
| 34 | + |
| 35 | + Arrays.fill(cards, 0); |
| 36 | + Arrays.fill(visited, false); |
| 37 | + for (int i = 0; i < N; i++) { |
| 38 | + Arrays.fill(dp[i], 0); |
| 39 | + } |
| 40 | + |
| 41 | + st = new StringTokenizer(br.readLine()); |
| 42 | + for (int i = 0; i < N; i++) { |
| 43 | + cards[i] = Integer.parseInt(st.nextToken()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + private static void sol() throws IOException { |
| 48 | + game(0, 0, N - 1); |
| 49 | + |
| 50 | + sb.append(dp[0][N - 1]).append("\n"); |
| 51 | + } |
| 52 | + |
| 53 | + private static int game(int depth, int left, int right) { |
| 54 | + if (right < left) return 0; |
| 55 | + if (dp[left][right] != 0) return dp[left][right]; |
| 56 | + |
| 57 | + if (depth % 2 == 0) { |
| 58 | + return dp[left][right] = Math.max(game(depth + 1, left + 1, right) + cards[left] |
| 59 | + , game(depth + 1, left, right - 1) + cards[right]); |
| 60 | + } else { |
| 61 | + return dp[left][right] = Math.min(game(depth + 1, left + 1, right) |
| 62 | + , game(depth + 1, left, right - 1)); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | +} |
| 67 | +``` |
0 commit comments