結果

問題 No.334 門松ゲーム
ユーザー uafr_csuafr_cs
提出日時 2016-02-02 22:24:51
言語 Java21
(openjdk 21)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 1,514 bytes
コンパイル時間 2,413 ms
コンパイル使用メモリ 79,288 KB
実行使用メモリ 58,560 KB
最終ジャッジ日時 2023-10-21 18:46:30
合計ジャッジ時間 5,710 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 171 ms
58,436 KB
testcase_01 AC 132 ms
57,500 KB
testcase_02 AC 133 ms
57,392 KB
testcase_03 AC 165 ms
58,556 KB
testcase_04 AC 166 ms
58,396 KB
testcase_05 AC 166 ms
58,316 KB
testcase_06 AC 164 ms
58,464 KB
testcase_07 AC 131 ms
57,372 KB
testcase_08 AC 167 ms
58,556 KB
testcase_09 AC 132 ms
57,456 KB
testcase_10 AC 139 ms
57,400 KB
testcase_11 AC 169 ms
58,548 KB
testcase_12 AC 167 ms
58,372 KB
testcase_13 AC 167 ms
58,560 KB
testcase_14 AC 131 ms
57,612 KB
testcase_15 AC 168 ms
58,556 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static boolean dfs(final int deep, final int N, int[] array, int used, boolean[] visited, boolean[] memo){
		if(visited[used]){
			return memo[used];
		}
		visited[used] = true;
		
		for(int fst = 0; fst < N; fst++){
			if((used & (1 << fst)) != 0){ continue; }
			
			for(int snd = fst + 1; snd < N; snd++){
				if((used & (1 << snd)) != 0){ continue; }
				if(array[fst] == array[snd]){ continue; }
				
				for(int thd = snd + 1; thd < N; thd++){
					if((used & (1 << thd)) != 0){ continue; }
					if(array[fst] == array[thd]){ continue; }
					if(array[snd] == array[thd]){ continue; }
					if(array[fst] < array[snd] && array[snd] < array[thd]){ continue; }
					if(array[fst] > array[snd] && array[snd] > array[thd]){ continue; }
					
					if(!dfs(deep + 1, N, array, used | (1 << fst) | (1 << snd) | (1 << thd), visited, memo)){
						if(deep == 0){
							System.out.println(fst + " " + snd + " " + thd);
						}
						return memo[used] = true;
					}
				}
			}
		}
		
		return memo[used] = false;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();
		
		int[] array = new int[N];
		for(int i = 0; i < N; i++){
			array[i] = sc.nextInt();
		}
		
		final boolean ret = dfs(0, N, array, 0, new boolean[1 << N], new boolean[1 << N]);
		if(!ret){ System.out.println(-1); }
		
	}

}
0