結果

問題 No.5006 Hidden Maze
ユーザー RisenRisen
提出日時 2022-06-12 23:21:14
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 799 ms / 2,000 ms
コード長 8,932 bytes
コンパイル時間 6,943 ms
実行使用メモリ 69,556 KB
スコア 80,355
平均クエリ数 197.45
最終ジャッジ日時 2022-06-12 23:21:54
合計ジャッジ時間 35,746 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 170 ms
59,844 KB
testcase_01 AC 132 ms
52,628 KB
testcase_02 AC 127 ms
50,920 KB
testcase_03 AC 107 ms
46,092 KB
testcase_04 AC 110 ms
47,832 KB
testcase_05 AC 141 ms
55,436 KB
testcase_06 AC 144 ms
53,944 KB
testcase_07 AC 137 ms
50,024 KB
testcase_08 AC 140 ms
52,048 KB
testcase_09 AC 177 ms
61,944 KB
testcase_10 AC 120 ms
49,132 KB
testcase_11 AC 174 ms
60,996 KB
testcase_12 AC 188 ms
63,120 KB
testcase_13 AC 108 ms
47,196 KB
testcase_14 AC 767 ms
68,708 KB
testcase_15 AC 130 ms
50,316 KB
testcase_16 AC 118 ms
47,720 KB
testcase_17 AC 130 ms
49,284 KB
testcase_18 AC 191 ms
67,040 KB
testcase_19 AC 105 ms
46,008 KB
testcase_20 AC 118 ms
49,156 KB
testcase_21 AC 127 ms
50,792 KB
testcase_22 AC 110 ms
47,920 KB
testcase_23 AC 179 ms
60,340 KB
testcase_24 AC 167 ms
58,324 KB
testcase_25 AC 129 ms
47,552 KB
testcase_26 AC 528 ms
68,328 KB
testcase_27 AC 168 ms
56,192 KB
testcase_28 AC 109 ms
47,184 KB
testcase_29 AC 279 ms
68,200 KB
testcase_30 AC 118 ms
49,084 KB
testcase_31 AC 799 ms
68,944 KB
testcase_32 AC 141 ms
54,868 KB
testcase_33 AC 135 ms
50,500 KB
testcase_34 AC 108 ms
46,120 KB
testcase_35 AC 134 ms
51,112 KB
testcase_36 AC 121 ms
49,652 KB
testcase_37 AC 354 ms
68,608 KB
testcase_38 AC 111 ms
49,692 KB
testcase_39 AC 113 ms
50,100 KB
testcase_40 AC 130 ms
51,492 KB
testcase_41 AC 135 ms
50,968 KB
testcase_42 AC 135 ms
52,340 KB
testcase_43 AC 121 ms
52,772 KB
testcase_44 AC 107 ms
48,316 KB
testcase_45 AC 676 ms
69,112 KB
testcase_46 AC 470 ms
68,364 KB
testcase_47 AC 109 ms
48,592 KB
testcase_48 AC 110 ms
49,664 KB
testcase_49 AC 454 ms
68,484 KB
testcase_50 AC 110 ms
46,724 KB
testcase_51 AC 128 ms
53,264 KB
testcase_52 AC 146 ms
54,400 KB
testcase_53 AC 151 ms
55,328 KB
testcase_54 AC 124 ms
48,548 KB
testcase_55 AC 173 ms
54,972 KB
testcase_56 AC 135 ms
50,740 KB
testcase_57 AC 294 ms
68,260 KB
testcase_58 AC 129 ms
53,252 KB
testcase_59 AC 119 ms
49,556 KB
testcase_60 AC 115 ms
47,904 KB
testcase_61 AC 106 ms
49,856 KB
testcase_62 AC 145 ms
56,180 KB
testcase_63 AC 123 ms
49,268 KB
testcase_64 AC 148 ms
53,892 KB
testcase_65 AC 108 ms
48,476 KB
testcase_66 AC 121 ms
51,520 KB
testcase_67 AC 115 ms
47,620 KB
testcase_68 AC 178 ms
63,916 KB
testcase_69 AC 131 ms
51,484 KB
testcase_70 AC 112 ms
49,880 KB
testcase_71 AC 116 ms
50,204 KB
testcase_72 AC 107 ms
45,908 KB
testcase_73 AC 146 ms
54,504 KB
testcase_74 AC 119 ms
53,392 KB
testcase_75 AC 133 ms
53,268 KB
testcase_76 AC 116 ms
48,256 KB
testcase_77 AC 198 ms
68,308 KB
testcase_78 AC 227 ms
68,792 KB
testcase_79 AC 116 ms
47,976 KB
testcase_80 AC 109 ms
48,072 KB
testcase_81 AC 150 ms
55,020 KB
testcase_82 AC 116 ms
47,836 KB
testcase_83 AC 106 ms
46,136 KB
testcase_84 AC 130 ms
52,900 KB
testcase_85 AC 620 ms
69,556 KB
testcase_86 AC 109 ms
46,664 KB
testcase_87 AC 135 ms
50,992 KB
testcase_88 AC 192 ms
58,860 KB
testcase_89 AC 571 ms
68,732 KB
testcase_90 AC 123 ms
48,952 KB
testcase_91 AC 126 ms
54,080 KB
testcase_92 AC 114 ms
49,584 KB
testcase_93 AC 123 ms
47,244 KB
testcase_94 AC 136 ms
52,488 KB
testcase_95 AC 126 ms
52,468 KB
testcase_96 AC 104 ms
45,988 KB
testcase_97 AC 235 ms
68,648 KB
testcase_98 AC 107 ms
47,588 KB
testcase_99 AC 156 ms
56,100 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  Determining projects to restore...
  Restored /home/judge/data/code/main.csproj (in 115 ms).
.NET 向け Microsoft (R) Build Engine バージョン 17.0.0-preview-21470-01+cb055d28f
Copyright (C) Microsoft Corporation.All rights reserved.

  プレビュー版の .NET を使用しています。https://aka.ms/dotnet-core-preview をご覧ください
  main -> /home/judge/data/code/bin/Release/net6.0/main.dll
  main -> /home/judge/data/code/bin/Release/net6.0/publish/

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using static Constants;

public static class Constants
{
    public static readonly int N = 20;

    public static readonly Point[] Dirs =
    {
        new Point(-1, 0), // 左
        new Point(0, -1), // 上
        new Point(1, 0), // 右
        new Point(0, 1), // 下
    };
}

public class PriorityQueue<TKey, TValue>
{
    SortedDictionary<TKey, List<TValue>> dict = new SortedDictionary<TKey, List<TValue>>();
    int count;
    Random random = new Random();

    public int Count
    {
        get
        {
            return count;
        }
    }

    public void Enqueue(TKey key, TValue value)
    {
        List<TValue> set;
        if (!dict.TryGetValue(key, out set))
        {
            set = new List<TValue>();
            dict[key] = set;
        }

        set.Add(value);
        count++;
    }

    public KeyValuePair<TKey, TValue> Dequeue()
    {
        var queue = dict.First();
        if (queue.Value.Count <= 1)
        {
            dict.Remove(queue.Key);
        }

        var list = queue.Value;
        var i = random.Next(list.Count);
        var val = list[i];
        count--;
        if (list.Count > 1)
        {
            (list[i], list[list.Count - 1]) = (list[list.Count - 1], list[i]);
            list.RemoveAt(list.Count - 1);
        }

        return new KeyValuePair<TKey, TValue>(queue.Key, val);
    }
}

public struct Point
{
    public int X;
    public int Y;

    public static readonly Point Invalid = new Point(-1, -1);
    public static readonly Point Left = new Point(-1, 0);
    public static readonly Point Up = new Point(0, -1);
    public static readonly Point Right = new Point(1, 0);
    public static readonly Point Down = new Point(0, 1);

    public Point(Point p)
    {
        X = p.X;
        Y = p.Y;
    }

    public Point(int x, int y)
    {
        X = x;
        Y = y;
    }

    public static bool operator ==(Point a, Point b)
    {
        return (a.X == b.X && a.Y == b.Y);
    }

    public static bool operator !=(Point a, Point b)
    {
        return !(a == b);
    }

    //objと自分自身が等価のときはtrueを返す
    public override bool Equals(object obj)
    {
        if (!(obj is Point))
        {
            return false;
        }

        Point p = (Point)obj;
        return (X == p.X && Y == p.Y);
    }

    //Equalsがtrueを返すときに同じ値を返す
    public override int GetHashCode()
    {
        return (X << 16) ^ Y;
    }

    public Point Clone()
    {
        return new Point(X, Y);
    }

    public void Reverse()
    {
        X = -X;
        Y = -Y;
    }

    public static Point operator +(Point a, Point b)
    {
        return new Point(a.X + b.X, a.Y + b.Y);
    }

    public static Point operator -(Point a, Point b)
    {
        return new Point(a.X - b.X, a.Y - b.Y);
    }

    public static Point operator *(Point a, int i)
    {
        return new Point(a.X * i, a.Y * i);
    }

    public static Point operator /(Point a, int i)
    {
        return new Point(a.X / i, a.Y / i);
    }
    public static Point operator +(Point a)
    {
        return new Point(+a.X, +a.Y);
    }

    public static Point operator -(Point a)
    {
        return new Point(-a.X, -a.Y);
    }

    public override string ToString()
    {
        return string.Format("({0},{1})", Y, X);
    }

    public Point Sign()
    {
        return new Point(Math.Sign(X), Math.Sign(Y));
    }

    public int Distance(Point p)
    {
        return Math.Abs(this.X - p.X) + Math.Abs(this.Y - p.Y);
    }

    public char ToDirStr()
    {
        if (X < 0)
        {
            return 'L';
        }
        if (X > 0)
        {
            return 'R';
        }
        if (Y < 0)
        {
            return 'U';
        }
        return 'D';
    }

    public bool InRange()
    {
        return 0 <= X && X < N && 0 <= Y && Y < N;
    }
}

public class Solver
{
    readonly double P;
    int[,] RWall; // -1:壁はない 0以上:移動失敗回数
    int[,] DWall;
    List<Point>[,] Neighbors;

    public Solver(double p)
    {
        P = p;
        RWall = new int[N, N];
        DWall = new int[N, N];
        InitNeighbors();
    }

    void InitNeighbors()
    {
        Neighbors = new List<Point>[N, N];
        for (int y = 0; y < N; y++)
        {
            for (int x = 0; x < N; x++)
            {
                Neighbors[y, x] = new List<Point>();
                var p = new Point(x, y);
                foreach (var dir in Dirs)
                {
                    var nei = p + dir;
                    if (nei.InRange())
                    {
                        Neighbors[y, x].Add(nei);
                    }
                }
            }
        }
    }

    int GetWallValue(Point cur, Point next)
    {
        var dir = next - cur;
        if (dir.Y == 0)
        {
            return RWall[cur.Y, Math.Min(cur.X, next.X)];

        }
        return DWall[Math.Min(cur.Y, next.Y), cur.X];
    }

    double GetProb(Point cur, Point next)
    {
        const double wallProb = 150.0 / 760.0; // 100~200 / 全壁
        var dir = next - cur;
        var wall = GetWallValue(cur, next);
        if (wall == -1)
        {
            return 1;
        }

        // X:k回移動失敗としたとき,ベイズの定理より
        // P(壁|X) = P(X|壁)P(壁) / (P(X|壁)P(壁) + P(X|非壁)P(非壁))
        // ここで,P(X|壁)=1 より
        // P(壁|X) = P(壁) / (P(壁) + p^k*(1-P(壁))
        return 1 - wallProb / (wallProb + Math.Pow(P, wall) * (1 - wallProb));
    }

    public (string, List<Point>) GetAction()
    {
        var start = new Point(0, 0);
        var goal = new Point(N - 1, N - 1);
        var prev = new Dictionary<Point, Point>();
        var queue = new PriorityQueue<double, Point>(); // 確率の降順

        queue.Enqueue(-1, start);
        prev[start] = start;
        while (queue.Count > 0)
        {
            var kv = queue.Dequeue();
            var cur = kv.Value;
            var prob = -kv.Key;
            foreach (var next in Neighbors[cur.Y, cur.X])
            {
                if (prev.ContainsKey(next))
                {
                    continue;
                }

                var nextProb = prob * GetProb(cur, next) * (1 - P);
                queue.Enqueue(-nextProb, next);
                prev[next] = cur;
                if (next == goal)
                {
                    break;
                }
            }
        }

        var way = new List<Point>();
        for (var p = goal; p != start; p = prev[p])
        {
            way.Add(p);
        }
        way.Add(start);
        way.Reverse();
        var acts = new StringBuilder();
        var pr = start;
        foreach (var p in way.Skip(1))
        {
            var dir = p - pr;
            acts.Append(dir.ToDirStr());
            pr = p;
        }
        return (acts.ToString(), way);
    }

    public void SetResult(List<Point> way, int step)
    {
        for (int i = 0; i < step; i++)
        {
            var cur = way[i];
            var next = way[i + 1];
            var dir = next - cur;
            if (dir.Y == 0)
            {
                var y = cur.Y;
                var x = Math.Min(cur.X, next.X);
                RWall[y, x] = -1;
            }
            else
            {
                var y = Math.Min(cur.Y, next.Y);
                var x = cur.X;
                DWall[y, x] = -1;
            }
        }
        {
            var cur = way[step];
            var next = way[step + 1];
            var dir = next - cur;
            if (dir.Y == 0)
            {
                var y = cur.Y;
                var x = Math.Min(cur.X, next.X);
                if (RWall[y, x] >= 0)
                {
                    RWall[y, x]++;
                }
            }
            else
            {
                var y = Math.Min(cur.Y, next.Y);
                var x = cur.X;
                if (DWall[y, x] >= 0)
                {
                    DWall[y, x]++;
                }
            }
        }
    }
}

public static class Solution
{
    public static void Main()
    {
        var vals = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var h = vals[0];
        var w = vals[1];
        var p = vals[2] * 0.01;

        var solver = new Solver(p);
        try
        {
            while (true)
            {
                (var acts, var way) = solver.GetAction();
                Console.WriteLine(acts);
                var step = int.Parse(Console.ReadLine());
                if (step < 0)
                {
                    break;
                }
                solver.SetResult(way, step);
            }
        }
        catch (Exception e)
        {
            Console.Error.WriteLine(e);
        }
    }
}
0