結果
| 問題 | No.506 限られたジャパリまん | 
| コンテスト | |
| ユーザー |  tenten | 
| 提出日時 | 2020-12-25 09:23:27 | 
| 言語 | Java (openjdk 23) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 261 ms / 2,000 ms | 
| コード長 | 1,970 bytes | 
| コンパイル時間 | 2,375 ms | 
| コンパイル使用メモリ | 79,516 KB | 
| 実行使用メモリ | 46,808 KB | 
| 最終ジャッジ日時 | 2024-06-28 04:25:17 | 
| 合計ジャッジ時間 | 7,501 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 25 | 
ソースコード
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int w = sc.nextInt();
        int k = sc.nextInt();
        int p = sc.nextInt();
        HashMap<Integer, Integer> places = new HashMap<>();
        String[] names = new String[k];
        for (int i = 0; i < k; i++) {
            places.put(sc.nextInt() * 100 + sc.nextInt(), i);
            names[i] = sc.next();
        }
        long max = 0;
        int key = 0;
        long[][] counts = new long[h + 1][w + 1];
        for (int i = 0; i < (1 << k); i++) {
            if (getPop(i) != p) {
                continue;
            }
            counts[0][0] = 1;
            for (int a = 0; a <= h; a++) {
                for (int b = 0; b <= w; b++) {
                    if (a == 0 && b == 0) {
                        continue;
                    }
                    counts[a][b] = 0;
                    if (places.containsKey(a * 100 + b) && ((1 << places.get(a * 100 + b)) & i) == 0) {
                        continue;
                    }
                    if (a > 0) {
                        counts[a][b] += counts[a - 1][b];
                    }
                    if (b > 0) {
                        counts[a][b] += counts[a][b - 1];
                    }
                }
            }
            if (max < counts[h][w]) {
                max = counts[h][w];
                key = i;
            }
        }
        StringBuilder sb = new StringBuilder();
        sb.append(max % 1000000007).append("\n");
        for (int i = 0; i < k; i++) {
            if (key % 2 == 1) {
                sb.append(names[i]).append("\n");
            }
            key /= 2;
        }
        System.out.print(sb);
    }
    
    static int getPop(int x) {
        int pop = 0;
        while (x > 0) {
            pop += x % 2;
            x /= 2;
        }
        return pop;
    }
}
            
            
            
        