結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー 明智重蔵明智重蔵
提出日時 2015-09-12 10:04:15
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 71 ms / 5,000 ms
コード長 5,700 bytes
コンパイル時間 2,591 ms
コンパイル使用メモリ 108,432 KB
実行使用メモリ 24,716 KB
最終ジャッジ日時 2023-09-26 11:36:16
合計ジャッジ時間 5,187 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
22,648 KB
testcase_01 AC 66 ms
24,692 KB
testcase_02 AC 68 ms
22,776 KB
testcase_03 AC 65 ms
24,588 KB
testcase_04 AC 68 ms
24,696 KB
testcase_05 AC 67 ms
22,672 KB
testcase_06 AC 67 ms
22,708 KB
testcase_07 AC 68 ms
22,636 KB
testcase_08 AC 68 ms
22,724 KB
testcase_09 AC 69 ms
22,648 KB
testcase_10 AC 67 ms
22,720 KB
testcase_11 AC 68 ms
22,648 KB
testcase_12 AC 67 ms
24,716 KB
testcase_13 AC 64 ms
24,664 KB
testcase_14 AC 64 ms
22,576 KB
testcase_15 AC 64 ms
24,672 KB
testcase_16 AC 67 ms
24,704 KB
testcase_17 AC 71 ms
22,660 KB
testcase_18 AC 63 ms
22,592 KB
testcase_19 AC 63 ms
22,644 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;

class Program
{
    static string InputPattern = "Input4";

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

        if (InputPattern == "Input1") {
            WillReturn.Add("1 2 3 4");
            WillReturn.Add("5 6 7 8");
            WillReturn.Add("9 10 11 12");
            WillReturn.Add("13 14 15 0");
            //Yes
            //初期配置がそのまま目標の配置である。
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("1 2 3 4");
            WillReturn.Add("5 6 7 8");
            WillReturn.Add("9 10 12 0");
            WillReturn.Add("13 14 11 15");
            //Yes
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("1 2 3 4");
            WillReturn.Add("5 6 7 8");
            WillReturn.Add("9 10 12 15");
            WillReturn.Add("13 14 11 0");
            //No
            //目標の配置を作るためには、15の駒を最低2回はスライドしなければならない。
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    const int UB = 4 - 1;

    //No.228 ゆきこちゃんの15パズル
    static void Main()
    {
        List<string> InputList = GetInputList();

        int[,] BanArr = new int[UB + 1, UB + 1];
        for (int Y = 0; Y <= UB; Y++) {
            int[] wkArr = InputList[Y].Split(' ').Select(X => int.Parse(X)).ToArray();
            for (int X = 0; X <= UB; X++) {
                BanArr[X, Y] = wkArr[X];
            }
        }

        Console.WriteLine(HasAnswer(BanArr) ? "Yes" : "No");
    }

    struct JyoutaiDef
    {
        internal int[,] BanArr;
        internal HashSet<int> MovedNumSet;
    }

    //深さ優先探索で解を探す
    static bool HasAnswer(int[,] pBanArr)
    {
        //正位置とのマンハッタン距離が2以上の数値があったらNo
        for (int LoopX = 0; LoopX <= UB; LoopX++) {
            for (int LoopY = 0; LoopY <= UB; LoopY++) {
                if (pBanArr[LoopX, LoopY] == 0) continue;
                if (DeriveKyori(pBanArr[LoopX, LoopY], LoopX, LoopY) >= 2)
                    return false;
            }
        }

        var stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.BanArr = pBanArr;
        WillPush.MovedNumSet = new HashSet<int>();
        stk.Push(WillPush);

        while (stk.Count > 0) {
            JyoutaiDef Popped = stk.Pop();
            //PrintBan(Popped.BanArr);

            //クリア判定
            if (IsClear(Popped.BanArr)) return true;

            int ZeroX, ZeroY;
            Derive0Zahyou(Popped.BanArr, out ZeroX, out ZeroY);

            Action<int, int> PushSyori = (pFromX, pFromY) =>
            {
                if (pFromX < 0 || UB < pFromX) return;
                if (pFromY < 0 || UB < pFromY) return;

                //1度移動した数値は移動不可
                if (Popped.MovedNumSet.Contains(pBanArr[pFromX, pFromY]))
                    return;

                //正位置とのマンハッタン距離が1の数値のみ移動可
                if (DeriveKyori(pBanArr[pFromX, pFromY], pFromX, pFromY) != 1)
                    return;

                WillPush.BanArr = (int[,])Popped.BanArr.Clone();
                int SankakuWK = WillPush.BanArr[pFromX, pFromY];
                WillPush.BanArr[pFromX, pFromY] = 0;
                WillPush.BanArr[ZeroX, ZeroY] = SankakuWK;
                WillPush.MovedNumSet = new HashSet<int>(Popped.MovedNumSet) { SankakuWK };
                stk.Push(WillPush);
            };
            PushSyori(ZeroX, ZeroY - 1);
            PushSyori(ZeroX, ZeroY + 1);
            PushSyori(ZeroX - 1, ZeroY);
            PushSyori(ZeroX + 1, ZeroY);
        }
        return false;
    }

    //数値の現座標を引数として、正位置とのマンハッタン距離を求める
    static int DeriveKyori(int pCurrNum, int pX, int pY)
    {
        int NeedNum = 1;
        for (int LoopY = 0; LoopY <= UB; LoopY++) {
            for (int LoopX = 0; LoopX <= UB; LoopX++) {
                if (pCurrNum == NeedNum++) {
                    return Math.Abs(LoopX - pX)
                         + Math.Abs(LoopY - pY);
                }
            }
        }
        return -1;
    }

    //盤面の0の座標を返す
    static void Derive0Zahyou(int[,] pBanArr, out int pX, out int pY)
    {
        pX = pY = -1;
        for (int LoopX = 0; LoopX <= UB; LoopX++) {
            for (int LoopY = 0; LoopY <= UB; LoopY++) {
                if (pBanArr[LoopX, LoopY] != 0) continue;
                pX = LoopX;
                pY = LoopY;
                return;
            }
        }
    }

    //クリア判定
    static bool IsClear(int[,] pBanArr)
    {
        int NeedNum = 1;
        for (int LoopY = 0; LoopY <= UB; LoopY++) {
            for (int LoopX = 0; LoopX <= UB; LoopX++) {
                if (LoopX == UB && LoopY == UB) {
                    if (pBanArr[LoopX, LoopY] != 0) return false;
                }
                else if (pBanArr[LoopX, LoopY] != NeedNum++) return false;
            }
        }
        return true;
    }

    //盤面を表示
    static void PrintBan(int[,] pBanArr)
    {
        Console.WriteLine(new string('■', 15));
        for (int LoopY = 0; LoopY <= UB; LoopY++) {
            for (int LoopX = 0; LoopX <= UB; LoopX++) {
                Console.Write("{0,2},", pBanArr[LoopX, LoopY]);
            }
            Console.WriteLine();
        }
    }
}
0