結果

問題 No.102 トランプを奪え
ユーザー ぴろずぴろず
提出日時 2014-12-21 10:46:48
言語 Java21
(openjdk 21)
結果
AC  
実行時間 267 ms / 5,000 ms
コード長 2,245 bytes
コンパイル時間 4,496 ms
コンパイル使用メモリ 79,072 KB
実行使用メモリ 65,036 KB
最終ジャッジ日時 2023-09-02 20:36:07
合計ジャッジ時間 4,874 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
57,416 KB
testcase_01 AC 128 ms
57,968 KB
testcase_02 AC 161 ms
60,820 KB
testcase_03 AC 126 ms
57,864 KB
testcase_04 AC 127 ms
57,672 KB
testcase_05 AC 172 ms
58,200 KB
testcase_06 AC 160 ms
60,528 KB
testcase_07 AC 164 ms
60,856 KB
testcase_08 AC 177 ms
60,556 KB
testcase_09 AC 267 ms
64,712 KB
testcase_10 AC 254 ms
65,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no102;

import java.util.Scanner;

public class Main {
	static int[] n = new int[4];
	static int sum = 0;
	static int[][][][][] dp; //unknown-0 taro-1 jiro-2 draw-3
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		for(int i=0;i<4;i++) {
			n[i] = sc.nextInt();
			sum += n[i];
		}
		dp = new int[14][14][14][14][sum+1];
		int ans = dfs(n[0],n[1],n[2],n[3],0);
		if (ans == 1) {
			System.out.println("Taro");
		}else if(ans == 2) {
			System.out.println("Jiro");
		}else{
			System.out.println("Draw");
		}
	}
	static int dfs(int n1,int n2,int n3,int n4,int hand) {
		if (dp[n1][n2][n3][n4][hand] != 0) {
			return dp[n1][n2][n3][n4][hand];
		}
		int ret = 0;
		if (n1 + n2 + n3 + n4 == 0) {
			if (hand * 2 == sum) {
				ret = 3;
			}else if (hand * 2 > sum) {
				ret = 1;
			}else{
				ret = 2;
			}
		}else{
			boolean win = false;
			boolean draw = false;
			for(int i=1;i<=3;i++) {
				if (i > n1) {
					break;
				}
				int x;
				if (i == n1) {
					x = dfs(n1-i,n2,n3,n4,(sum-n1-n2-n3-n4-hand)/2);
				}else{
					x = dfs(n1-i,n2,n3,n4,sum-n1-n2-n3-n4-hand);
				}
				if (x == 2) {
					win = true;
				}else if(x == 3) {
					draw = true;
				}
			}
			for(int i=1;i<=3;i++) {
				if (i > n2) {
					break;
				}
				int x;
				if (i == n2) {
					x = dfs(n1,n2-i,n3,n4,(sum-n1-n2-n3-n4-hand)/2);
				}else{
					x = dfs(n1,n2-i,n3,n4,sum-n1-n2-n3-n4-hand);
				}
				if (x == 2) {
					win = true;
				}else if(x == 3) {
					draw = true;
				}
			}
			for(int i=1;i<=3;i++) {
				if (i > n3) {
					break;
				}
				int x;
				if (i == n3) {
					x = dfs(n1,n2,n3-i,n4,(sum-n1-n2-n3-n4-hand)/2);
				}else{
					x = dfs(n1,n2,n3-i,n4,sum-n1-n2-n3-n4-hand);
				}
				if (x == 2) {
					win = true;
				}else if(x == 3) {
					draw = true;
				}
			}
			for(int i=1;i<=3;i++) {
				if (i > n4) {
					break;
				}
				int x;
				if (i == n4) {
					x = dfs(n1,n2,n3,n4-i,(sum-n1-n2-n3-n4-hand)/2);
				}else{
					x = dfs(n1,n2,n3,n4-i,sum-n1-n2-n3-n4-hand);
				}
				if (x == 2) {
					win = true;
				}else if(x == 3) {
					draw = true;
				}
			}
			if (win) {
				ret = 1;
			}else if(draw) {
				ret = 3;
			}else{
				ret = 2;
			}
		}
		return dp[n1][n2][n3][n4][hand] = ret;
	}
}
0