結果

問題 No.334 門松ゲーム
ユーザー nCk_cvnCk_cv
提出日時 2016-02-04 21:06:39
言語 Java21
(openjdk 21)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 2,648 bytes
コンパイル時間 2,334 ms
コンパイル使用メモリ 84,636 KB
実行使用メモリ 60,264 KB
最終ジャッジ日時 2023-10-21 19:13:26
合計ジャッジ時間 6,093 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 167 ms
58,452 KB
testcase_01 AC 129 ms
57,436 KB
testcase_02 AC 174 ms
57,448 KB
testcase_03 AC 164 ms
58,260 KB
testcase_04 AC 164 ms
58,444 KB
testcase_05 AC 165 ms
58,272 KB
testcase_06 AC 184 ms
58,580 KB
testcase_07 AC 163 ms
57,436 KB
testcase_08 AC 196 ms
58,416 KB
testcase_09 AC 162 ms
57,412 KB
testcase_10 AC 186 ms
59,536 KB
testcase_11 AC 193 ms
60,264 KB
testcase_12 AC 169 ms
58,436 KB
testcase_13 AC 169 ms
58,324 KB
testcase_14 AC 171 ms
59,652 KB
testcase_15 AC 198 ms
58,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.math.*;
import java.io.*;
 
public class Main {
	static int[] ret = new int[3];
	static int[] k;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		k = new int[n];
		for(int i = 0; i < n; i++) {
			k[i] = sc.nextInt();
		}
		boolean ret = dfs(n,new boolean[n],true);
		if(ret) {
			System.out.println(Main.ret[0] + " " + Main.ret[1] + " " + Main.ret[2]);
		}
		else {
			System.out.println(-1);
		}
	}
	static boolean dfs(int a, boolean[] use, boolean t) {
		if(a == 0)  return !t;
		if(a == use.length) {
			int[] ret = new int[3];
			for(int i = 0; i < use.length; i++) {
				ret[0] = i;
				for(int j = i+1; j < use.length; j++) {
					ret[1] = j;
					for(int k = j+1; k < use.length; k++) {
						int[] tes = new int[3];
						tes[0] = Main.k[i];
						tes[1] = Main.k[j];
						tes[2] = Main.k[k];
						Arrays.sort(tes);
						if(tes[1] == Main.k[j]) continue;
						ret[2] = k;
						use[i] = true;
						use[j] = true;
						use[k] = true;
						if(dfs(a - 3,use,false)) {
							use[i] = false;
							use[j] = false;
							use[k] = false;
							Main.ret = ret;
							return true;
						}
						use[i] = false;
						use[j] = false;
						use[k] = false;
					}
				}
			}
			return false;
		}
		else if(t) {
			for(int i = 0; i < use.length; i++) {
				if(use[i]) continue;
				for(int j = i+1; j < use.length; j++) {
					if(use[j]) continue;
					for(int k = j+1; k < use.length; k++) {
						if(use[k]) continue;
						int[] tes = new int[3];
						tes[0] = Main.k[i];
						tes[1] = Main.k[j];
						tes[2] = Main.k[k];
						Arrays.sort(tes);
						if(tes[1] == Main.k[j]) continue;
						use[i] = true;
						use[j] = true;
						use[k] = true;
						if(dfs(a - 3,use,false)) {
							use[i] = false;
							use[j] = false;
							use[k] = false;
							return true;
						}
						use[i] = false;
						use[j] = false;
						use[k] = false;
					}
				}
			}
			return false;
		}
		else {
			boolean ok = true;
			boolean ret = true;
			for(int i = 0; i < use.length; i++) {
				if(use[i]) continue;
				for(int j = i+1; j < use.length; j++) {
					if(use[j]) continue;
					for(int k = j+1; k < use.length; k++) {
						if(use[k]) continue;
						int[] tes = new int[3];
						tes[0] = Main.k[i];
						tes[1] = Main.k[j];
						tes[2] = Main.k[k];
						Arrays.sort(tes);
						if(tes[1] == Main.k[j]) continue;
						ok = true;
						use[i] = true;
						use[j] = true;
						use[k] = true;
						ret &= dfs(a - 3,use,true);
						use[i] = false;
						use[j] = false;
						use[k] = false;
					}
				}
			}
			return ret & ok;
		}
	}
}
0