結果

問題 No.5006 Hidden Maze
ユーザー inani_waoninani_waon
提出日時 2022-06-12 19:19:58
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 473 ms / 2,000 ms
コード長 9,775 bytes
コンパイル時間 9,459 ms
実行使用メモリ 57,520 KB
スコア 4,900
平均クエリ数 952.00
最終ジャッジ日時 2022-06-12 19:20:59
合計ジャッジ時間 51,725 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 447 ms
57,520 KB
testcase_01 AC 415 ms
53,216 KB
testcase_02 AC 364 ms
53,064 KB
testcase_03 AC 429 ms
52,936 KB
testcase_04 AC 427 ms
53,028 KB
testcase_05 AC 384 ms
52,736 KB
testcase_06 AC 457 ms
53,160 KB
testcase_07 AC 432 ms
53,172 KB
testcase_08 AC 390 ms
52,940 KB
testcase_09 AC 458 ms
52,964 KB
testcase_10 AC 419 ms
53,004 KB
testcase_11 AC 405 ms
53,560 KB
testcase_12 AC 408 ms
52,924 KB
testcase_13 AC 441 ms
53,148 KB
testcase_14 AC 423 ms
52,840 KB
testcase_15 AC 407 ms
53,344 KB
testcase_16 AC 424 ms
53,184 KB
testcase_17 AC 440 ms
57,416 KB
testcase_18 AC 377 ms
53,004 KB
testcase_19 AC 437 ms
57,300 KB
testcase_20 AC 401 ms
53,212 KB
testcase_21 AC 381 ms
52,808 KB
testcase_22 AC 429 ms
53,160 KB
testcase_23 AC 445 ms
57,428 KB
testcase_24 AC 368 ms
52,824 KB
testcase_25 AC 397 ms
52,688 KB
testcase_26 AC 351 ms
52,884 KB
testcase_27 AC 364 ms
52,772 KB
testcase_28 AC 444 ms
53,444 KB
testcase_29 AC 435 ms
53,056 KB
testcase_30 AC 442 ms
56,804 KB
testcase_31 AC 412 ms
53,184 KB
testcase_32 AC 392 ms
52,632 KB
testcase_33 AC 426 ms
52,700 KB
testcase_34 AC 422 ms
52,944 KB
testcase_35 AC 463 ms
57,144 KB
testcase_36 AC 436 ms
53,336 KB
testcase_37 AC 416 ms
52,800 KB
testcase_38 AC 384 ms
52,768 KB
testcase_39 AC 76 ms
45,232 KB
testcase_40 AC 386 ms
52,928 KB
testcase_41 AC 427 ms
53,256 KB
testcase_42 AC 403 ms
53,140 KB
testcase_43 AC 411 ms
53,068 KB
testcase_44 AC 471 ms
57,420 KB
testcase_45 AC 417 ms
53,136 KB
testcase_46 AC 384 ms
53,324 KB
testcase_47 AC 473 ms
56,780 KB
testcase_48 AC 391 ms
53,044 KB
testcase_49 AC 387 ms
53,140 KB
testcase_50 AC 387 ms
53,224 KB
testcase_51 AC 462 ms
53,016 KB
testcase_52 AC 401 ms
53,276 KB
testcase_53 AC 406 ms
52,668 KB
testcase_54 AC 77 ms
45,524 KB
testcase_55 AC 416 ms
53,292 KB
testcase_56 AC 434 ms
53,448 KB
testcase_57 AC 423 ms
53,044 KB
testcase_58 AC 446 ms
53,432 KB
testcase_59 AC 440 ms
52,656 KB
testcase_60 AC 418 ms
53,216 KB
testcase_61 AC 72 ms
44,336 KB
testcase_62 AC 435 ms
53,156 KB
testcase_63 AC 393 ms
53,536 KB
testcase_64 AC 406 ms
53,292 KB
testcase_65 AC 450 ms
56,784 KB
testcase_66 AC 380 ms
53,172 KB
testcase_67 AC 433 ms
53,224 KB
testcase_68 AC 363 ms
52,696 KB
testcase_69 AC 440 ms
56,968 KB
testcase_70 AC 455 ms
53,212 KB
testcase_71 AC 464 ms
53,296 KB
testcase_72 AC 419 ms
52,852 KB
testcase_73 AC 376 ms
52,884 KB
testcase_74 AC 396 ms
53,136 KB
testcase_75 AC 377 ms
53,360 KB
testcase_76 AC 439 ms
57,060 KB
testcase_77 AC 445 ms
56,548 KB
testcase_78 AC 459 ms
53,584 KB
testcase_79 AC 376 ms
52,860 KB
testcase_80 AC 451 ms
57,120 KB
testcase_81 AC 97 ms
46,036 KB
testcase_82 AC 469 ms
57,040 KB
testcase_83 AC 77 ms
45,068 KB
testcase_84 AC 398 ms
53,392 KB
testcase_85 AC 447 ms
56,748 KB
testcase_86 AC 428 ms
52,988 KB
testcase_87 AC 427 ms
52,832 KB
testcase_88 AC 384 ms
52,840 KB
testcase_89 AC 355 ms
52,880 KB
testcase_90 AC 444 ms
53,240 KB
testcase_91 AC 423 ms
53,020 KB
testcase_92 AC 443 ms
57,020 KB
testcase_93 AC 435 ms
53,484 KB
testcase_94 AC 458 ms
52,860 KB
testcase_95 AC 438 ms
52,852 KB
testcase_96 AC 431 ms
53,004 KB
testcase_97 AC 361 ms
53,160 KB
testcase_98 AC 435 ms
53,276 KB
testcase_99 AC 444 ms
52,980 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  Determining projects to restore...
  Restored /home/judge/data/code/main.csproj (in 139 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.Linq;
using System.Text;

class YukicoS5006
{
    public static void Main()
    {
        var nums = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var solver = new YukicoS5006Solver(nums[2]);
        while (true)
        {
            var cmd = solver.GetCommand();
            Console.WriteLine(cmd);
            var res = int.Parse(Console.ReadLine());
            if (res == -1) break;
            solver.UpdateWall(cmd, res);
        }
    }
}

class YukicoS5006Solver
{
    const int N = 20;
    const int NN = N * N;
    const double InitWallRate = 150.0 / ((N - 1) * N * 2);
    readonly int _p;
    readonly double[] wallRateV = Enumerable.Repeat(InitWallRate, NN).ToArray();
    readonly double[] wallRateH = Enumerable.Repeat(InitWallRate, NN).ToArray();
    readonly Grid _grid;

    public YukicoS5006Solver(int p)
    {
        _p = p;
        _grid = new Grid(N, N);
    }

    public string GetCommand()
    {
        var wallCost = 10000.0;

        var costs = GetRouteCost(wallCost);
        return GetRoute(costs);
    }

    double[] GetRouteCost(double wallCost)
    {
        var priorityQueue = new PriorityQueue<int, double>();
        priorityQueue.Enqueue(0, 0.0);

        var costs = Enumerable.Repeat(double.MaxValue, NN).ToArray();
        var visited = new bool[NN];

        while (priorityQueue.TryDequeue(out int pos, out double currentCost))
        {
            if (visited[pos]) continue;

            // その頂点への最短経路のコストを更新
            costs[pos] = currentCost;
            visited[pos] = true;

            var cell = _grid.Cells[pos];
            if (cell.Y > 0)
            {
                var wallRate = wallRateV[cell.EdgeIds[Direction.Up]];
                var cost = 1 + Math.Pow(wallCost, wallRate);
                priorityQueue.Enqueue(pos - N, currentCost + cost);
            }
            if (cell.Y < N - 1)
            {
                var wallRate = wallRateV[cell.EdgeIds[Direction.Down]];
                var cost = 1 + Math.Pow(wallCost, wallRate);
                priorityQueue.Enqueue(pos + N, currentCost + cost);
            }
            if (cell.X > 0)
            {
                var wallRate = wallRateH[cell.EdgeIds[Direction.Left]];
                var cost = 1 + Math.Pow(wallCost, wallRate);
                priorityQueue.Enqueue(pos - 1, currentCost + cost);
            }
            if (cell.X < N - 1)
            {
                var wallRate = wallRateH[cell.EdgeIds[Direction.Right]];
                var cost = 1 + Math.Pow(wallCost, wallRate);
                priorityQueue.Enqueue(pos + 1, currentCost + cost);
            }
        }

        return costs;
    }

    string GetRoute(double[] costs)
    {
        var sb = new StringBuilder();
        var pos = 19 * N + 19;

        while (pos != 0)
        {
            var bestDir = 'X';
            var bestNext = -1;
            var bestCost = double.MaxValue;

            var cell = _grid.Cells[pos];
            if (cell.Y > 0)
            {
                var next = pos - N;
                var cost = costs[next];
                if (cost < bestCost)
                {
                    bestCost = cost;
                    bestNext = next;
                    bestDir = 'D';
                }
            }
            if (cell.Y < N - 1)
            {
                var next = pos + N;
                var cost = costs[next];
                if (cost < bestCost)
                {
                    bestCost = cost;
                    bestNext = next;
                    bestDir = 'U';
                }
            }
            if (cell.X > 0)
            {
                var next = pos - 1;
                var cost = costs[next];
                if (cost < bestCost)
                {
                    bestCost = cost;
                    bestNext = next;
                    bestDir = 'R';
                }
            }
            if (cell.X < N - 1)
            {
                var next = pos + 1;
                var cost = costs[next];
                if (cost < bestCost)
                {
                    bestCost = cost;
                    bestNext = next;
                    bestDir = 'L';
                }
            }

            sb.Insert(0, bestDir);
            pos = bestNext;
        }

        return sb.ToString();
    }

    public void UpdateWall(string route, int moveCount)
    {
        int x = 0, y = 0;

        // 通過できた場所は確実に壁が無い
        for (var i = 0; i < moveCount; i++)
        {
            var pos = y * N + x;
            var c = route[i];

            int edgeId;
            switch (c)
            {
                case 'U':
                    edgeId = _grid.Cells[pos].EdgeIds[Direction.Up];
                    wallRateV[edgeId] = 0.0;
                    y--;
                    break;
                case 'D':
                    edgeId = _grid.Cells[pos].EdgeIds[Direction.Down];
                    wallRateV[edgeId] = 0.0;
                    y++;
                    break;
                case 'L':
                    edgeId = _grid.Cells[pos].EdgeIds[Direction.Left];
                    wallRateH[edgeId] = 0.0;
                    x--;
                    break;
                case 'R':
                    edgeId = _grid.Cells[pos].EdgeIds[Direction.Right];
                    wallRateH[edgeId] = 0.0;
                    x++;
                    break;
            }
        }

        // 通過できなかった場所は壁がある、(正しく壁にぶつかったケース)
        // もしくは経路上のいずれか1箇所以上に壁がある(壁を誤認したケース)
        // (過去に通過したのにぶつかった場合は誤認であることが分かる)

        // 暫定で通過できなかった場所だけ壁率を上げる(計算も雑)
        {
            var pos = y * N + x;
            var c = route[moveCount];
            int edgeId;
            switch (c)
            {
                case 'U':
                    edgeId = _grid.Cells[pos].EdgeIds[Direction.Up];
                    if (wallRateV[edgeId] != 0.0) wallRateV[edgeId] = (wallRateV[edgeId] + (100 - _p) / 100.0) / 2;
                    y--;
                    break;
                case 'D':
                    edgeId = _grid.Cells[pos].EdgeIds[Direction.Down];
                    if (wallRateV[edgeId] != 0.0) wallRateV[edgeId] = (wallRateV[edgeId] + (100 - _p) / 100.0) / 2;
                    y++;
                    break;
                case 'L':
                    edgeId = _grid.Cells[pos].EdgeIds[Direction.Left];
                    if (wallRateH[edgeId] != 0.0) wallRateH[edgeId] = (wallRateV[edgeId] + (100 - _p) / 100.0) / 2;
                    x--;
                    break;
                case 'R':
                    edgeId = _grid.Cells[pos].EdgeIds[Direction.Right];
                    if (wallRateH[edgeId] != 0.0) wallRateH[edgeId] = (wallRateV[edgeId] + (100 - _p) / 100.0) / 2;
                    x++;
                    break;
            }
        }

    }

}

public class Grid
{
    public int Height;
    public int Width;
    public int AreaSize;
    public Cell[] Cells;

    public Grid(int height, int width)
    {
        Height = height;
        Width = width;
        AreaSize = height * width;

        InitCells();
        Add4Connect();
        SetEdgeId();
    }

    void InitCells()
    {
        Cells = new Cell[AreaSize];
        for (var y = 0; y < Height; y++)
        {
            for (var x = 0; x < Width; x++)
            {
                var pos = y * Width + x;
                Cells[pos] = new Cell() { X = x, Y = y, Pos = pos, RelationCells = new List<Cell>() };
            }
        }
    }

    /// <summary>関係セルに4近傍の連結セルを追加する</summary>
    /// <param name="mask">立っているビットの位置のみ生成する 1:up 2:left 4:down 8:right</param>
    public void Add4Connect(int mask = 15)
    {
        for (var y = 0; y < Height; y++)
        {
            for (var x = 0; x < Width; x++)
            {
                var pos = y * Width + x;
                var baseCell = Cells[pos];
                var relation = new List<Cell>();
                baseCell.RelationCells = relation;
                // 上下左右でループしないケース用
                if ((mask & 1) != 0 && y > 0) relation.Add(Cells[pos - Width]);            // up
                if ((mask & 2) != 0 && x > 0) relation.Add(Cells[pos - 1]);                // left
                if ((mask & 4) != 0 && y < Height - 1) relation.Add(Cells[pos + Width]);   // down
                if ((mask & 8) != 0 && x < Width - 1) relation.Add(Cells[pos + 1]);        // right
            }
        }
    }

    void SetEdgeId()
    {
        for (var y = 0; y < Height; y++)
        {
            for (var x = 0; x < Width; x++)
            {
                var pos = y * Width + x;
                var cell = Cells[pos];
                cell.EdgeIds[Direction.Up] = x * (Height - 1) + y;
                cell.EdgeIds[Direction.Down] = x * (Height - 1) + y + 1;
                cell.EdgeIds[Direction.Left] = y * (Width - 1) + x;
                cell.EdgeIds[Direction.Right] = y * (Width - 1) + x + 1;
            }
        }
    }

}

public class Direction
{
    public const int Up = 0;
    public const int Down = 1;
    public const int Left = 2;
    public const int Right = 3;
}

public class Cell
{
    public int X;
    public int Y;
    public int Pos;
    /// <summary>関係セル [index]</summary>
    public List<Cell> RelationCells;
    public int[] EdgeIds = new int[4];
}
0