結果

問題 No.360 増加門松列
ユーザー rn4rurn4ru
提出日時 2016-04-18 00:02:32
言語 Java21
(openjdk 21)
結果
AC  
実行時間 131 ms / 2,000 ms
コード長 1,609 bytes
コンパイル時間 2,348 ms
コンパイル使用メモリ 76,064 KB
実行使用メモリ 56,208 KB
最終ジャッジ日時 2023-08-25 18:36:46
合計ジャッジ時間 5,794 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,832 KB
testcase_01 AC 126 ms
56,000 KB
testcase_02 AC 122 ms
55,940 KB
testcase_03 AC 119 ms
55,612 KB
testcase_04 AC 124 ms
55,884 KB
testcase_05 AC 125 ms
55,888 KB
testcase_06 AC 125 ms
55,596 KB
testcase_07 AC 125 ms
55,536 KB
testcase_08 AC 124 ms
56,124 KB
testcase_09 AC 123 ms
55,820 KB
testcase_10 AC 125 ms
55,880 KB
testcase_11 AC 123 ms
55,952 KB
testcase_12 AC 127 ms
55,888 KB
testcase_13 AC 122 ms
55,612 KB
testcase_14 AC 122 ms
56,208 KB
testcase_15 AC 122 ms
55,948 KB
testcase_16 AC 119 ms
55,884 KB
testcase_17 AC 121 ms
55,948 KB
testcase_18 AC 127 ms
55,708 KB
testcase_19 AC 126 ms
55,824 KB
testcase_20 AC 126 ms
55,828 KB
testcase_21 AC 131 ms
56,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int[] D = new int[8];
		for (int i = 1; i <= 7; i++) {
			D[i] = scanner.nextInt();
		}
		System.out.println(new Solver().solve(D));
	}

}

class Solver {

	public String solve(int[] d) {
		if (isZoukaKadomatsu(d)) {
			return "YES";
		}

		ArrayList<Integer> list = new ArrayList<>();
		list.add(0);
		if (dfs(0, d, new boolean[8], list)) {
			return "YES";
		}

		return "NO";
	}

	private boolean dfs(int i, int[] d, boolean[] bs, ArrayList<Integer> list) {
		bs[i] = true;
		if (all(bs))
			return true;

		for (int j = 1; j <= 7; j++) {
			if (bs[j])
				continue;
			boolean[] newbs = Arrays.copyOf(bs, bs.length);
			ArrayList<Integer> newlist = new ArrayList<>(list);
			newlist.add(d[j]);
			int[] is = new int[newlist.size()];
			for (int k = 0; k < newlist.size(); k++) {
				is[k] = newlist.get(k);
			}
			if (isZoukaKadomatsu(is)) {
				if (dfs(j, d, newbs, newlist)) {
					return true;
				}
			}
		}
		return false;
	}

	private boolean all(boolean[] bs) {
		for (boolean b : bs) {
			if (!b)
				return false;
		}
		return true;
	}

	private boolean isZoukaKadomatsu(int[] d) {
		for (int i = 1; i < d.length - 2; i++) {
			if (!isKadomatsu(d[i], d[i + 1], d[i + 2])) {
				return false;
			}
		}
		return true;
	}

	private boolean isKadomatsu(int i, int j, int k) {
		if (i == j || i == k || j == k)
			return false;
		if (i > k)
			return false;

		return (j > i && j > k) || (j < i && j < k);
	}

}
0