結果

問題 No.334 門松ゲーム
ユーザー jp_stejp_ste
提出日時 2016-01-31 23:50:34
言語 Java21
(openjdk 21)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 1,622 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 78,788 KB
実行使用メモリ 59,568 KB
最終ジャッジ日時 2023-10-21 18:26:30
合計ジャッジ時間 5,740 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 164 ms
58,604 KB
testcase_01 AC 131 ms
57,440 KB
testcase_02 AC 157 ms
58,128 KB
testcase_03 AC 163 ms
58,624 KB
testcase_04 AC 161 ms
58,384 KB
testcase_05 AC 164 ms
58,608 KB
testcase_06 AC 185 ms
59,048 KB
testcase_07 AC 149 ms
57,940 KB
testcase_08 AC 163 ms
58,620 KB
testcase_09 AC 147 ms
57,768 KB
testcase_10 AC 159 ms
58,444 KB
testcase_11 AC 184 ms
59,568 KB
testcase_12 AC 166 ms
58,612 KB
testcase_13 AC 173 ms
58,632 KB
testcase_14 AC 154 ms
57,940 KB
testcase_15 AC 189 ms
59,172 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	
	static int N;
	static int list[];
	
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = scan.nextInt();
		list = new int[N];
		for(int i=0; i<N; i++) {
			list[i] = scan.nextInt();
		}
		scan.close();
		
		boolean flag[] = new boolean[N];
		if(!solve(flag, 0, N)) {
			System.out.println("-1");
		}
	}
		
	static boolean solve(boolean flag[], int turn, int cardNum) {
		if(cardNum < 3) return false;
		
		boolean win = false;
		
		for(int i=0; i<N; i++) {
			for(int j=i+1; j<N; j++) {
				for(int k=j+1; k<N; k++) {
					if( flag[i]==false && flag[j]==false && flag[k]==false ) {
					
						int indexes[] = {i,j,k};
						if(check(indexes)) {
							
							boolean nextFlag[] = flag.clone();
							nextFlag[i] = true;
							nextFlag[j] = true;
							nextFlag[k] = true;
							
							if( solve(nextFlag, turn+1, cardNum-3) ) {
								continue;
							}
														
							if(turn == 0) { 
								System.out.println(i+" "+j+" "+k);
							}
							return true;
						}
					}
				}
			}
		}
		return win;
	}
	
	static boolean check(int indexes[]) {
		
		int values[] = new int[3];
		int min = Integer.MAX_VALUE;
		int max = 0;
		for(int i=0; i<3; i++) {
			values[i] = list[indexes[i]];
			min = Math.min(min, values[i]);
			max = Math.max(max, values[i]);
		}
		
		int A = values[0];
		int B = values[1];
		int C = values[2];
		
		if(A != B && A != C && B != C) {
			if((min == A && max == B) || (min == C && max == B) || (min == B && max == C) || (min == B && max == A) ) {	
				return true;
			}
		}
		return false;
	}
}
0