結果

問題 No.309 シャイな人たち (1)
ユーザー GrenacheGrenache
提出日時 2015-12-03 19:21:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,255 ms / 4,000 ms
コード長 4,205 bytes
コンパイル時間 4,534 ms
コンパイル使用メモリ 76,804 KB
実行使用メモリ 63,828 KB
最終ジャッジ日時 2023-10-12 09:34:15
合計ジャッジ時間 18,216 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 837 ms
61,788 KB
testcase_01 AC 981 ms
61,636 KB
testcase_02 AC 696 ms
63,400 KB
testcase_03 AC 1,003 ms
63,252 KB
testcase_04 AC 217 ms
61,272 KB
testcase_05 AC 684 ms
63,828 KB
testcase_06 AC 383 ms
62,084 KB
testcase_07 AC 665 ms
63,104 KB
testcase_08 AC 1,255 ms
63,492 KB
testcase_09 AC 1,150 ms
63,232 KB
testcase_10 AC 1,147 ms
62,172 KB
testcase_11 AC 1,196 ms
63,732 KB
testcase_12 AC 983 ms
62,116 KB
testcase_13 AC 115 ms
56,084 KB
testcase_14 AC 114 ms
55,696 KB
testcase_15 AC 124 ms
56,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Queue;


public class Main_yukicoder309_1 {

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

        int r = sc.nextInt();
        int c = sc.nextInt();
        int[][] p = new int[r][c];
        int[][] s = new int[r][c];
        for (int i = 0; i < r; i++) {
        	for (int j = 0; j < c; j++) {
    			p[i][j] = sc.nextInt();
        	}
        }
        for (int i = 0; i < r; i++) {
        	for (int j = 0; j < c; j++) {
        		s[i][j] = sc.nextInt();
        	}
        }

        int n = 0x1 << c;

        double[][] dp = new double[r + 1][n];
        dp[0][0] = 1.0;
		int[] nextst = new int[n];

        for (int i = 1; i <= r; i++) {
        	for (int j = 0; j < n; j++) {
				double ptmp = 1.0;
				for (int l = 0; l < c; l++) {
    				if ((j & 0x1 << l) != 0) {
    					ptmp *= (double)p[i - 1][l] / 100;
    				} else {
    					ptmp *= 1.0 - (double)p[i - 1][l] / 100;
    				}
				}
				if (ptmp == 0) {
					continue;
				}

    			Arrays.fill(nextst, -1);
        		for (int k = 0; k < n; k++) {
        			if (dp[i - 1][k] == 0) {
        				continue;
        			}

        			if (nextst[j & k] == -1) {
        				int[] po = new int[c];
        				boolean[] used = new boolean[c];
        				Queue<Integer> q = new ArrayDeque<Integer>();
        				int next = 0;
        				for (int l = 0; l < c; l++) {
        					if ((k & 0x1 << l) != 0) {
        						po[l] = 1;
        					} else {
        						po[l] = 0;
        					}
        					if ((j & 0x1 << l) != 0 && po[l] + 4 - s[i - 1][l] >= 4) {
        						next |= 0x1 << l;
        						used[l] = true;
        						q.add(l);
        					}
        				}
        				while (!q.isEmpty()) {
        					int e = q.remove();

        					if (e - 1 >= 0) {
        						po[e - 1]++;
        						if (!used[e - 1] && (j & 0x1 << e - 1) != 0 && po[e - 1] + 4 - s[i - 1][e - 1] >= 4) {
            						next |= 0x1 << e - 1;
        							used[e - 1] = true;
        							q.add(e - 1);
        						}
        					}
        					if (e + 1 < c) {
        						po[e + 1]++;
        						if (!used[e + 1] && (j & 0x1 << e + 1) != 0 && po[e + 1] + 4 - s[i - 1][e + 1] >= 4) {
            						next |= 0x1 << e + 1;
        							used[e + 1] = true;
        							q.add(e + 1);
        						}
        					}
        				}

        				nextst[j & k] = next;
        			}

            		dp[i][nextst[j & k]] += ptmp * dp[i - 1][k];
        		}
        	}
        }

        double ret = 0;
        for (int i = 1; i <= r; i++) {
        	for (int j = 0; j < n; j++) {
        		ret += dp[i][j] * Integer.bitCount(j);
        	}
        }

        pr.printf("%.14f\n", ret);

        pr.close();
        sc.close();
    }

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					String line = br.readLine();
					while (line.equals("")) {
						line = br.readLine();
					}
					it = Arrays.asList(line.split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

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