結果

問題 No.5006 Hidden Maze
ユーザー EvbCFfp1XBEvbCFfp1XB
提出日時 2022-06-12 17:55:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 421 ms / 2,000 ms
コード長 6,695 bytes
コンパイル時間 2,662 ms
実行使用メモリ 72,620 KB
スコア 69,729
平均クエリ数 303.71
最終ジャッジ日時 2022-06-12 17:55:53
合計ジャッジ時間 34,178 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 275 ms
69,924 KB
testcase_01 AC 308 ms
71,360 KB
testcase_02 AC 245 ms
62,848 KB
testcase_03 AC 304 ms
68,560 KB
testcase_04 AC 229 ms
67,320 KB
testcase_05 AC 243 ms
68,560 KB
testcase_06 AC 295 ms
70,788 KB
testcase_07 AC 276 ms
68,988 KB
testcase_08 AC 395 ms
69,712 KB
testcase_09 AC 219 ms
65,204 KB
testcase_10 AC 274 ms
67,788 KB
testcase_11 AC 197 ms
68,052 KB
testcase_12 AC 274 ms
70,520 KB
testcase_13 AC 154 ms
63,244 KB
testcase_14 AC 411 ms
72,620 KB
testcase_15 AC 170 ms
63,356 KB
testcase_16 AC 184 ms
63,432 KB
testcase_17 AC 154 ms
63,432 KB
testcase_18 AC 373 ms
70,096 KB
testcase_19 AC 170 ms
65,368 KB
testcase_20 AC 183 ms
64,184 KB
testcase_21 AC 232 ms
66,096 KB
testcase_22 AC 218 ms
67,636 KB
testcase_23 AC 200 ms
67,936 KB
testcase_24 AC 337 ms
71,108 KB
testcase_25 AC 265 ms
70,976 KB
testcase_26 AC 413 ms
70,692 KB
testcase_27 AC 322 ms
71,504 KB
testcase_28 AC 163 ms
65,888 KB
testcase_29 AC 153 ms
64,788 KB
testcase_30 AC 179 ms
67,284 KB
testcase_31 AC 226 ms
68,636 KB
testcase_32 AC 386 ms
70,800 KB
testcase_33 AC 334 ms
69,268 KB
testcase_34 AC 204 ms
64,772 KB
testcase_35 AC 186 ms
67,296 KB
testcase_36 AC 133 ms
63,460 KB
testcase_37 AC 281 ms
69,932 KB
testcase_38 AC 286 ms
68,420 KB
testcase_39 AC 181 ms
65,360 KB
testcase_40 AC 212 ms
65,424 KB
testcase_41 AC 261 ms
68,344 KB
testcase_42 AC 243 ms
68,092 KB
testcase_43 AC 352 ms
69,444 KB
testcase_44 AC 152 ms
63,400 KB
testcase_45 AC 328 ms
72,028 KB
testcase_46 AC 421 ms
70,784 KB
testcase_47 AC 158 ms
65,476 KB
testcase_48 AC 206 ms
68,152 KB
testcase_49 AC 413 ms
69,484 KB
testcase_50 AC 168 ms
65,688 KB
testcase_51 AC 212 ms
65,428 KB
testcase_52 AC 325 ms
69,096 KB
testcase_53 AC 283 ms
68,816 KB
testcase_54 AC 216 ms
67,368 KB
testcase_55 AC 242 ms
67,764 KB
testcase_56 AC 231 ms
68,628 KB
testcase_57 AC 347 ms
69,152 KB
testcase_58 AC 200 ms
66,196 KB
testcase_59 AC 150 ms
65,612 KB
testcase_60 AC 237 ms
67,964 KB
testcase_61 AC 154 ms
63,348 KB
testcase_62 AC 303 ms
69,128 KB
testcase_63 AC 234 ms
67,620 KB
testcase_64 AC 259 ms
71,252 KB
testcase_65 AC 222 ms
67,532 KB
testcase_66 AC 150 ms
63,684 KB
testcase_67 AC 243 ms
68,088 KB
testcase_68 AC 293 ms
68,616 KB
testcase_69 AC 195 ms
67,372 KB
testcase_70 AC 177 ms
65,940 KB
testcase_71 AC 152 ms
63,868 KB
testcase_72 AC 149 ms
63,448 KB
testcase_73 AC 244 ms
68,872 KB
testcase_74 AC 263 ms
68,932 KB
testcase_75 AC 368 ms
71,276 KB
testcase_76 AC 225 ms
65,868 KB
testcase_77 AC 248 ms
68,792 KB
testcase_78 AC 151 ms
63,180 KB
testcase_79 AC 392 ms
71,912 KB
testcase_80 AC 230 ms
69,208 KB
testcase_81 AC 162 ms
64,476 KB
testcase_82 AC 261 ms
66,064 KB
testcase_83 AC 144 ms
65,116 KB
testcase_84 AC 230 ms
66,500 KB
testcase_85 AC 334 ms
69,752 KB
testcase_86 AC 144 ms
65,344 KB
testcase_87 AC 266 ms
68,528 KB
testcase_88 AC 283 ms
70,604 KB
testcase_89 AC 394 ms
70,884 KB
testcase_90 AC 229 ms
66,380 KB
testcase_91 AC 217 ms
66,668 KB
testcase_92 AC 290 ms
70,532 KB
testcase_93 AC 160 ms
63,364 KB
testcase_94 AC 245 ms
68,944 KB
testcase_95 AC 316 ms
68,756 KB
testcase_96 AC 160 ms
65,116 KB
testcase_97 AC 391 ms
70,440 KB
testcase_98 AC 329 ms
71,168 KB
testcase_99 AC 153 ms
65,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    private static final int[] dr = { -1, 1, 0, 0 };
    private static final int[] dc = { 0, 0, -1, 1 };
    private static final String dir = "UDLR";
    private static final double eps = 1e-9;

    private static final int grid_width = 20;
    private static final int max_trial = 1001;
    private double[][] h = new double[grid_width][grid_width + 1];
    private double[][] v = new double[grid_width + 1][grid_width];
    private int H;
    private int W;
    private int p;

    public static Random rng = new Random(System.nanoTime());

    public static void main(String[] args) {
        new Main().run();
    }

    private void run() {
        try (Scanner in = new Scanner(System.in)) {
            H = in.nextInt();
            W = in.nextInt();
            p = in.nextInt();
            Utils.debug("H", H, "W", W, "p", p);

            for (int r = 0; r < grid_width; r++) {
                for (int c = 0; c < grid_width + 1; c++) {
                    h[r][c] = -1;
                }
            }
            for (int r = 0; r < grid_width + 1; r++) {
                for (int c = 0; c < grid_width; c++) {
                    v[r][c] = -1;
                }
            }

            for (int trial = 1; trial <= max_trial; trial++) {

                String path = path();
                System.out.println(path);
                System.out.flush();

                int res = in.nextInt();
                if (res == -1) {
                    Utils.debug("trial", trial, "res", res, "p", p, "length", path.length());
                    break;
                }
                update(path, res);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void update(String path, int length) {
        int r = 0;
        int c = 0;
        for (int i = 0; i < length; i++) {
            char ch = path.charAt(i);
            int d = direction(ch);

            if (d == 0) {
                v[r][c] = 0;
            } else if (d == 1) {
                v[r + 1][c] = 0;
            } else if (d == 2) {
                h[r][c] = 0;
            } else if (d == 3) {
                h[r][c + 1] = 0;
            }

            r += dr[d];
            c += dc[d];
        }
        char ch = path.charAt(length);
        int d = direction(ch);
        if (d == 0) {
            if (v[r][c] < 0) {
                v[r][c] = eps;
            }
            if (v[r][c] > 0.5 * eps) {
                v[r][c] += 1e-2 * p;
            }
        } else if (d == 1) {
            if (v[r + 1][c] < 0) {
                v[r + 1][c] = eps;
            }
            if (v[r + 1][c] > 0.5 * eps) {
                v[r + 1][c] += 1e-2 * p;
            }
        } else if (d == 2) {
            if (h[r][c] < 0) {
                h[r][c] = eps;
            }
            if (h[r][c] > 0.5 * eps) {
                h[r][c] += 1e-2 * p;
            }
        } else if (d == 3) {
            if (h[r][c + 1] < 0) {
                h[r][c + 1] = eps;
            }
            if (h[r][c + 1] > 0.5 * eps) {
                h[r][c + 1] += 1e-2 * p;
            }
        }
    }

    private String path() {
        boolean[][] used = new boolean[grid_width][grid_width];
        used[0][0] = true;
        PriorityQueue<Node> q = new PriorityQueue<>((o1, o2) -> {
            if (o1.cost < o2.cost) {
                return -1;
            }
            if (o1.cost > o2.cost) {
                return 1;
            }

            if (o1.r + o1.c > o2.r + o2.c) {
                return -1;
            }
            if (o1.r + o1.c < o2.r + o2.c) {
                return 1;
            }

            if (o1.random < o2.random) {
                return -1;
            }
            if (o1.random > o2.random) {
                return 1;
            }

            return 0;
        });
        q.add(new Node(null, 0, 0, 0, 0));
        while (!q.isEmpty()) {
            final Node p = q.poll();
            int r = p.r;
            int c = p.c;
            double cost = p.cost;
            double random = p.random;

            if (r == grid_width - 1 && c == grid_width - 1) {
                return reverse(toList(p)).stream().map(node -> "" + dir.charAt(direction(node.parent.r, node.parent.c, node.r, node.c))).reduce("", (sum, s) -> sum + s);
            }

            for (int d = 0; d < dr.length; d++) {
                int nr = r + dr[d];
                int nc = c + dc[d];
                if (!Utils.isValid(nr, 0, grid_width) || !Utils.isValid(nc, 0, grid_width)) {
                    continue;
                }
                if (!used[nr][nc]) {
                    used[nr][nc] = true;
                    q.add(new Node(p, nr, nc, cost + (d <= 1 ? v[r + (d == 1 ? 1 : 0)][c] : h[r][c + (d == 3 ? 1 : 0)]), random + rng.nextDouble()));
                }
            }
        }

        throw new AssertionError();
    }

    private <T> ArrayList<T> reverse(ArrayList<T> list) {
        for (int l = 0, r = list.size() - 1; l < r; l++, r--) {
            list.set(r, list.set(l, list.get(r)));
        }
        return list;
    }

    private ArrayList<Node> toList(Node node0) {
        ArrayList<Node> res = new ArrayList<>();
        for (Node current = node0; current.parent != null; current = current.parent) {
            res.add(current);
        }
        return res;
    }

    private int direction(char c) {
        int d = -1;
        for (int i = 0; i < 4; i++) {
            if (c == dir.charAt(i))
                d = i;
        }
        return d;
    }

    private int direction(int r1, int c1, int r2, int c2) {
        if (r1 > r2) {
            return 0;
        }
        if (r1 < r2) {
            return 1;
        }
        if (c1 > c2) {
            return 2;
        }
        if (c1 < c2) {
            return 3;
        }
        throw new AssertionError();
    }

}

final class Utils {
    private Utils() {
    }

    public static final void debug(Object... o) {
        System.err.println(toString(o));
    }

    public static final String toString(Object... o) {
        return Arrays.deepToString(o);
    }

    public static boolean isValid(int v, int min, int minUpper) {
        return v >= min && v < minUpper;
    }

}

class Node {
    Node parent;
    int r;
    int c;
    double cost;
    double random;

    Node(Node parent, int r, int c, double cost, double random) {
        this.parent = parent;
        this.r = r;
        this.c = c;
        this.cost = cost;
        this.random = random;
    }
}
0