結果

問題 No.832 麻雀修行中
ユーザー GrenacheGrenache
提出日時 2019-05-24 22:01:33
言語 Java19
(openjdk 21)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 3,306 bytes
コンパイル時間 4,103 ms
コンパイル使用メモリ 85,800 KB
実行使用メモリ 61,284 KB
最終ジャッジ日時 2023-10-17 12:35:35
合計ジャッジ時間 11,979 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 194 ms
59,928 KB
testcase_01 AC 214 ms
59,928 KB
testcase_02 AC 204 ms
59,968 KB
testcase_03 AC 242 ms
61,012 KB
testcase_04 AC 220 ms
60,212 KB
testcase_05 AC 209 ms
60,040 KB
testcase_06 AC 211 ms
60,244 KB
testcase_07 AC 215 ms
60,244 KB
testcase_08 AC 210 ms
60,220 KB
testcase_09 AC 207 ms
60,000 KB
testcase_10 AC 229 ms
60,276 KB
testcase_11 AC 230 ms
60,408 KB
testcase_12 AC 240 ms
61,196 KB
testcase_13 AC 183 ms
59,924 KB
testcase_14 AC 239 ms
61,068 KB
testcase_15 AC 219 ms
60,240 KB
testcase_16 AC 191 ms
60,236 KB
testcase_17 AC 197 ms
59,928 KB
testcase_18 AC 208 ms
60,028 KB
testcase_19 AC 238 ms
61,284 KB
testcase_20 AC 190 ms
59,900 KB
testcase_21 AC 228 ms
60,008 KB
testcase_22 AC 205 ms
59,976 KB
testcase_23 AC 212 ms
60,220 KB
testcase_24 AC 201 ms
59,996 KB
testcase_25 AC 198 ms
60,288 KB
testcase_26 AC 208 ms
59,940 KB
testcase_27 AC 206 ms
60,004 KB
testcase_28 AC 190 ms
60,512 KB
testcase_29 AC 223 ms
60,236 KB
testcase_30 AC 201 ms
60,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder832 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		char[] s = sc.next().toCharArray();

		int[] cnt = new int[9];
		for (char c : s) {
			cnt[c - '1']++;
		}
		
		for (int i = 0; i < 9; i++) {
			if (cnt[i] == 4) {
				continue;
			}
			
			int[] tmp = Arrays.copyOf(cnt, cnt.length);
			tmp[i]++;
			
			if (isOK(tmp)) {
				pr.println(i + 1);
			}
		}
	}

	private static boolean isOK(int[] cnt) {
		// chitoi
		boolean chitoi = true;
		for (int e : cnt) {
			if (e != 2 && e != 0) {
				chitoi = false;
				break;
			}
		}
		if (chitoi) {
			return true;
		}
		
		for (int a = 0; a < 9; a++) {
			if (cnt[a] < 2) {
				continue;
			}
			
			int[] tmp = Arrays.copyOf(cnt, cnt.length);
			tmp[a] -= 2;
			
			if (isOK2(tmp)) {
				return true;
			}
		}
		
		return false;
	}

	private static boolean isOK2(int[] cnt) {
		Deque<List<Integer>> st = new ArrayDeque<>();
		st.push(new ArrayList<>());
		while (!st.isEmpty()) {
			List<Integer> list = st.pop();
			
			if (list.size() == 4) {
				if (isOK3(list, cnt)) {
					return true;
				}
			} else {
				int s;
				if (list.size() == 0) {
					s = 0;
				} else {
					s = list.get(list.size() - 1) + 1;
				}
				
				for (int i = s; i < men.length; i++) {
					if (isOK3(list, cnt)) {
						List<Integer> tmp = new ArrayList<>(list);
						tmp.add(i);
						st.push(tmp);
					}
				}
			}
		}
		
		return false;
	}

	private static boolean isOK3(List<Integer> list, int[] cnt) {
		int[] tmp = new int[9];
		for (int e : list) {
			for (int i = 0; i < 9; i++) {
				tmp[i] += men[e][i];
			}
		}
		
		for (int i = 0; i < 9; i++) {
			if (tmp[i] > cnt[i]) {
				return false;
			}
		}
		
		return true;
	}

	private static int[][] men = {
			{3, 0, 0, 0, 0, 0, 0, 0, 0},
			{0, 3, 0, 0, 0, 0, 0, 0, 0},
			{0, 0, 3, 0, 0, 0, 0, 0, 0},
			{0, 0, 0, 3, 0, 0, 0, 0, 0},
			{0, 0, 0, 0, 3, 0, 0, 0, 0},
			{0, 0, 0, 0, 0, 3, 0, 0, 0},
			{0, 0, 0, 0, 0, 0, 3, 0, 0},
			{0, 0, 0, 0, 0, 0, 0, 3, 0},
			{0, 0, 0, 0, 0, 0, 0, 0, 3},
			{1, 1, 1, 0, 0, 0, 0, 0, 0},
			{0, 1, 1, 1, 0, 0, 0, 0, 0},
			{0, 0, 1, 1, 1, 0, 0, 0, 0},
			{0, 0, 0, 1, 1, 1, 0, 0, 0},
			{0, 0, 0, 0, 1, 1, 1, 0, 0},
			{0, 0, 0, 0, 0, 1, 1, 1, 0},
			{0, 0, 0, 0, 0, 0, 1, 1, 1},
			{1, 1, 1, 0, 0, 0, 0, 0, 0},
			{0, 1, 1, 1, 0, 0, 0, 0, 0},
			{0, 0, 1, 1, 1, 0, 0, 0, 0},
			{0, 0, 0, 1, 1, 1, 0, 0, 0},
			{0, 0, 0, 0, 1, 1, 1, 0, 0},
			{0, 0, 0, 0, 0, 1, 1, 1, 0},
			{0, 0, 0, 0, 0, 0, 1, 1, 1},
			{1, 1, 1, 0, 0, 0, 0, 0, 0},
			{0, 1, 1, 1, 0, 0, 0, 0, 0},
			{0, 0, 1, 1, 1, 0, 0, 0, 0},
			{0, 0, 0, 1, 1, 1, 0, 0, 0},
			{0, 0, 0, 0, 1, 1, 1, 0, 0},
			{0, 0, 0, 0, 0, 1, 1, 1, 0},
			{0, 0, 0, 0, 0, 0, 1, 1, 1},
			{1, 1, 1, 0, 0, 0, 0, 0, 0},
			{0, 1, 1, 1, 0, 0, 0, 0, 0},
			{0, 0, 1, 1, 1, 0, 0, 0, 0},
			{0, 0, 0, 1, 1, 1, 0, 0, 0},
			{0, 0, 0, 0, 1, 1, 1, 0, 0},
			{0, 0, 0, 0, 0, 1, 1, 1, 0},
			{0, 0, 0, 0, 0, 0, 1, 1, 1},
	};

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);
			
		solve();
			
		pr.close();
		sc.close();
	}

	static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0