結果

問題 No.367 ナイトの転身
ユーザー 明智重蔵明智重蔵
提出日時 2016-06-18 17:22:10
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 299 ms / 2,000 ms
コード長 4,563 bytes
コンパイル時間 1,209 ms
コンパイル使用メモリ 109,568 KB
実行使用メモリ 41,600 KB
最終ジャッジ日時 2024-04-18 02:51:16
合計ジャッジ時間 3,488 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
20,352 KB
testcase_01 AC 33 ms
20,352 KB
testcase_02 AC 32 ms
20,480 KB
testcase_03 AC 31 ms
20,480 KB
testcase_04 AC 31 ms
20,096 KB
testcase_05 AC 31 ms
20,352 KB
testcase_06 AC 32 ms
20,608 KB
testcase_07 AC 31 ms
20,736 KB
testcase_08 AC 32 ms
20,352 KB
testcase_09 AC 32 ms
20,352 KB
testcase_10 AC 196 ms
33,920 KB
testcase_11 AC 299 ms
41,600 KB
testcase_12 AC 50 ms
25,344 KB
testcase_13 AC 56 ms
26,368 KB
testcase_14 AC 98 ms
28,416 KB
testcase_15 AC 32 ms
20,864 KB
testcase_16 AC 96 ms
28,544 KB
testcase_17 AC 41 ms
23,176 KB
testcase_18 AC 48 ms
24,960 KB
testcase_19 AC 45 ms
24,448 KB
testcase_20 AC 35 ms
21,120 KB
testcase_21 AC 45 ms
24,832 KB
testcase_22 AC 32 ms
20,352 KB
testcase_23 AC 34 ms
20,608 KB
testcase_24 AC 33 ms
20,608 KB
testcase_25 AC 34 ms
20,480 KB
testcase_26 AC 31 ms
20,352 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

//No367 ナイトの転身
class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("4 4");
            WillReturn.Add("...G");
            WillReturn.Add(".RR.");
            WillReturn.Add(".RR.");
            WillReturn.Add("S...");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 4");
            WillReturn.Add("...G");
            WillReturn.Add("....");
            WillReturn.Add("S...");
            //3
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("3 3");
            WillReturn.Add(".R.");
            WillReturn.Add("RGR");
            WillReturn.Add("SR.");
            //-1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int UB_X;
    static int UB_Y;

    struct JyoutaiDef
    {
        internal int Level;
        internal int CurrX;
        internal int CurrY;
        internal bool IsKnight;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = InputList[0].Split(' ').Select(X => int.Parse(X)).ToArray();
        int H = wkArr[0];
        int W = wkArr[1];
        char[,] BanArr = new char[W, H];
        UB_X = W - 1;
        UB_Y = H - 1;

        int StaX = -1, StaY = -1, GoalX = -1, GoalY = -1;
        for (int X = 0; X <= UB_X; X++) {
            for (int Y = 0; Y <= UB_Y; Y++) {
                BanArr[X, Y] = InputList[Y + 1][X];

                if (BanArr[X, Y] == 'S') {
                    StaX = X; StaY = Y;
                }
                if (BanArr[X, Y] == 'G') {
                    GoalX = X; GoalY = Y;
                }
            }
        }

        var Que = new Queue<JyoutaiDef>();
        JyoutaiDef WillEnqueue;
        WillEnqueue.Level = 0;
        WillEnqueue.CurrX = StaX;
        WillEnqueue.CurrY = StaY;
        WillEnqueue.IsKnight = true;
        Que.Enqueue(WillEnqueue);

        var VisitedSet = new HashSet<int>();
        VisitedSet.Add(GetHash(WillEnqueue));
        while (Que.Count > 0) {
            JyoutaiDef Dequeued = Que.Dequeue();

            //クリア判定
            if (Dequeued.CurrX == GoalX && Dequeued.CurrY == GoalY) {
                Console.WriteLine(Dequeued.Level);
                return;
            }

            Action<int, int> EnqueueSyori = (pNewX, pNewY) =>
            {
                if (pNewX < 0 || UB_X < pNewX) return;
                if (pNewY < 0 || UB_Y < pNewY) return;

                WillEnqueue.Level = Dequeued.Level + 1;
                WillEnqueue.CurrX = pNewX;
                WillEnqueue.CurrY = pNewY;
                WillEnqueue.IsKnight = Dequeued.IsKnight;
                if (BanArr[pNewX, pNewY] == 'R') {
                    WillEnqueue.IsKnight = !WillEnqueue.IsKnight;
                }

                //再訪は不可
                if (VisitedSet.Add(GetHash(WillEnqueue)) == false)
                    return;

                Que.Enqueue(WillEnqueue);
            };

            if (Dequeued.IsKnight) {
                EnqueueSyori(Dequeued.CurrX - 1, Dequeued.CurrY - 2);
                EnqueueSyori(Dequeued.CurrX + 1, Dequeued.CurrY - 2);
                EnqueueSyori(Dequeued.CurrX - 2, Dequeued.CurrY - 1);
                EnqueueSyori(Dequeued.CurrX + 2, Dequeued.CurrY - 1);
                EnqueueSyori(Dequeued.CurrX - 2, Dequeued.CurrY + 1);
                EnqueueSyori(Dequeued.CurrX + 2, Dequeued.CurrY + 1);
                EnqueueSyori(Dequeued.CurrX - 1, Dequeued.CurrY + 2);
                EnqueueSyori(Dequeued.CurrX + 1, Dequeued.CurrY + 2);
            }
            else {
                EnqueueSyori(Dequeued.CurrX - 1, Dequeued.CurrY - 1);
                EnqueueSyori(Dequeued.CurrX + 1, Dequeued.CurrY - 1);
                EnqueueSyori(Dequeued.CurrX - 1, Dequeued.CurrY + 1);
                EnqueueSyori(Dequeued.CurrX + 1, Dequeued.CurrY + 1);
            }
        }
        Console.WriteLine(-1);
    }

    //現在位置とIsKnightからハッシュ値を求める
    static int GetHash(JyoutaiDef pJyoutai)
    {
        int Hash = pJyoutai.CurrX * 10000;
        Hash += pJyoutai.CurrY * 10;
        Hash += (pJyoutai.IsKnight ? 1 : 0);
        return Hash;
    }
}
0