結果

問題 No.334 門松ゲーム
ユーザー jp_ste
提出日時 2016-01-31 04:29:08
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 1,837 bytes
コンパイル時間 2,551 ms
コンパイル使用メモリ 84,512 KB
実行使用メモリ 69,628 KB
最終ジャッジ日時 2024-09-21 19:36:43
合計ジャッジ時間 7,085 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	
	static int N;
	static int list[];
	static boolean flag[];
	static ArrayList<String> matchList = new ArrayList<String>();
	static boolean win = false;

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		N = scan.nextInt();
		list = new int[N];
		flag = new boolean[N];

		for(int i=0; i<N; i++) {
			list[i] = scan.nextInt();
		}
		scan.close();
		
		ArrayList<Integer> used = new ArrayList<>();
		int turn = 0;
		solve(used, turn, 0);
		if(win) {
			System.out.println(matchList.get(0));
		} else {
			System.out.println("-1");
		}
	}
	
	static void solve(ArrayList<Integer> used, int turn, int firstI) {
		if(win) return;
		
		if(used.size() == 3) {
			if(check(used)) {
				if(turn==0) {
					int count = matchList.size();
					if(count == 1 || count == 3) {
						win = true;
						return ;
					}
					matchList = new ArrayList<String>();
				}
				matchList.add(used.get(0) + " " + used.get(1) + " " + used.get(2));
				solve(new ArrayList<Integer>(), turn+1, 0);
			} 
			return;
		}
		
		for(int i=firstI; i<N; i++) {		
			if(flag[i]) continue;
			flag[i] = true;
			used.add(Integer.valueOf(i));

			solve(used, turn, i+1);
			
			used.remove(Integer.valueOf(i));
			flag[i] = false;
		}
	}
	
	static boolean check(ArrayList<Integer> used) {
		
		int values[] = new int[3];
		int min = Integer.MAX_VALUE;
		int max = 0;
		for(int i=0; i<3; i++) {
			values[i] = list[used.get(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