結果

問題 No.460 裏表ちわーわ
ユーザー 明智重蔵明智重蔵
提出日時 2016-12-23 07:33:07
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 6,367 bytes
コンパイル時間 2,418 ms
コンパイル使用メモリ 110,016 KB
実行使用メモリ 30,104 KB
最終ジャッジ日時 2023-08-21 06:01:05
合計ジャッジ時間 6,909 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
23,940 KB
testcase_01 AC 65 ms
19,904 KB
testcase_02 AC 66 ms
22,004 KB
testcase_03 AC 66 ms
21,832 KB
testcase_04 AC 65 ms
21,992 KB
testcase_05 AC 66 ms
22,000 KB
testcase_06 AC 66 ms
21,936 KB
testcase_07 AC 246 ms
28,052 KB
testcase_08 AC 67 ms
24,060 KB
testcase_09 AC 66 ms
23,880 KB
testcase_10 AC 74 ms
24,120 KB
testcase_11 AC 67 ms
21,832 KB
testcase_12 AC 108 ms
28,156 KB
testcase_13 AC 116 ms
27,948 KB
testcase_14 AC 122 ms
27,944 KB
testcase_15 AC 99 ms
28,004 KB
testcase_16 AC 69 ms
21,884 KB
testcase_17 AC 108 ms
26,024 KB
testcase_18 AC 109 ms
27,960 KB
testcase_19 AC 150 ms
25,840 KB
testcase_20 AC 69 ms
21,820 KB
testcase_21 AC 118 ms
30,104 KB
testcase_22 AC 89 ms
26,028 KB
testcase_23 AC 118 ms
27,892 KB
testcase_24 AC 67 ms
19,968 KB
testcase_25 AC 67 ms
21,948 KB
testcase_26 AC 64 ms
19,900 KB
testcase_27 AC 67 ms
22,064 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 = "InputX";

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

        if (InputPattern == "Input1") {
            WillReturn.Add("5 5");
            WillReturn.Add("0 0 1 1 1");
            WillReturn.Add("0 0 1 1 1");
            WillReturn.Add("1 1 0 1 1");
            WillReturn.Add("1 1 1 0 0");
            WillReturn.Add("1 1 1 0 0");
            //2
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4 4");
            WillReturn.Add("0 0 0 0");
            WillReturn.Add("0 1 1 0");
            WillReturn.Add("0 1 1 0");
            WillReturn.Add("0 0 0 0");
            //16
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("8 5");
            WillReturn.Add("0 0 1 1 1");
            WillReturn.Add("0 0 1 1 1");
            WillReturn.Add("0 1 0 0 1");
            WillReturn.Add("1 0 0 1 0");
            WillReturn.Add("1 1 1 0 0");
            WillReturn.Add("1 0 1 0 1");
            WillReturn.Add("0 1 0 0 1");
            WillReturn.Add("0 0 1 1 1");
            //5
        }
        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 CurrX;
        internal int CurrY;
        internal int Cost;
        internal bool[,] MasuArr;
    }

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

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(X => int.Parse(X)).ToArray();

        SplitAct(InputList[0]);

        int M = wkArr[0], N = wkArr[1];
        UB_X = N - 1;
        UB_Y = M - 1;

        bool[,] mQuestionArr = new bool[UB_X + 1, UB_Y + 1];

        for (int I = 1; I <= InputList.Count - 1; I++) {
            SplitAct(InputList[I]);
            for (int J = 0; J <= wkArr.GetUpperBound(0); J++) {
                mQuestionArr[J, I - 1] = (wkArr[J] == 1);
            }
        }

        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.CurrX = WillPush.CurrY = 0;
        WillPush.Cost = 0;
        WillPush.MasuArr = mQuestionArr;
        Stk.Push(WillPush);

        int AnswerCost = int.MaxValue;
        bool FoundAnswer = false;

        while (Stk.Count > 0) {
            JyoutaiDef Popped = Stk.Pop();

            if (IsOK(Popped.MasuArr)) {
                FoundAnswer = true;
                AnswerCost = Popped.Cost;
                continue;
            }

            //下限値枝切り
            if (AnswerCost <= Popped.Cost) continue;

            if (Popped.CurrY == UB_Y + 1) {
                continue;
            }

            //次の座標を設定
            WillPush.CurrY = Popped.CurrY;
            WillPush.CurrX = Popped.CurrX + 1;
            if (WillPush.CurrX == UB_X + 1) {
                WillPush.CurrY++;
                WillPush.CurrX = 0;
            }

            bool WillPushHanten = false; //反転させるケース
            bool WillPushNonHanten = false; //反転させないケース

            if (Popped.CurrY == 0) { //Y座標が0の場合
                WillPushHanten = WillPushNonHanten = true;
            }
            else { //Y座標が1以上の場合
                var wkList = new List<bool>();

                //X=UB_Xなら、真上と左上
                if (Popped.CurrX == UB_X) {
                    wkList.AddRange(DeriveBoolList(Popped.MasuArr, Popped.CurrX, Popped.CurrY - 1));
                    wkList.AddRange(DeriveBoolList(Popped.MasuArr, Popped.CurrX - 1, Popped.CurrY - 1));
                }
                else { //X<UB_Xなら、左上
                    wkList.AddRange(DeriveBoolList(Popped.MasuArr, Popped.CurrX - 1, Popped.CurrY - 1));
                }

                if (wkList.Count == 0) {
                    WillPushHanten = WillPushNonHanten = true;
                }
                else if (wkList.TrueForAll(A => A)) { //全部1の場合
                    WillPushHanten = true;
                }
                else if (wkList.TrueForAll(A => A == false)) { //全部0の場合
                    WillPushNonHanten = true;
                }
            }

            if (WillPushHanten) { //反転させるケースをPush
                WillPush.Cost = Popped.Cost + 1;
                WillPush.MasuArr = (bool[,])Popped.MasuArr.Clone();
                HantenSyori(WillPush.MasuArr, Popped.CurrX, Popped.CurrY);
                Stk.Push(WillPush);
            }

            if (WillPushNonHanten) { //反転させないケースをPush
                WillPush.Cost = Popped.Cost;
                WillPush.MasuArr = Popped.MasuArr;
                Stk.Push(WillPush);
            }
        }

        if (FoundAnswer)
            Console.WriteLine(AnswerCost);
        else Console.WriteLine("Impossible");
    }

    //終了判定
    static bool IsOK(bool[,] pMasuArr)
    {
        foreach (bool EachBool in pMasuArr) {
            if (EachBool) return false;
        }
        return true;
    }

    //指定した座標のBool型のListを返す
    static List<bool> DeriveBoolList(bool[,] pMasuArr, int pX, int pY)
    {
        if (pX < 0 || UB_X < pX) return new List<bool>();
        if (pY < 0 || UB_Y < pY) return new List<bool>();
        return new List<bool>() { pMasuArr[pX, pY] };
    }

    //押したマスと8近傍の、座標を反転
    static void HantenSyori(bool[,] pMasuArr, int pX, int pY)
    {
        MasuHanten(pMasuArr, pX - 1, pY - 1);
        MasuHanten(pMasuArr, pX - 1, pY);
        MasuHanten(pMasuArr, pX - 1, pY + 1);
        MasuHanten(pMasuArr, pX, pY - 1);
        MasuHanten(pMasuArr, pX, pY);
        MasuHanten(pMasuArr, pX, pY + 1);
        MasuHanten(pMasuArr, pX + 1, pY - 1);
        MasuHanten(pMasuArr, pX + 1, pY);
        MasuHanten(pMasuArr, pX + 1, pY + 1);
    }

    //指定マスの0と1の反転
    static void MasuHanten(bool[,] pMasuArr, int pX, int pY)
    {
        if (pX < 0 || UB_X < pX) return;
        if (pY < 0 || UB_Y < pY) return;

        pMasuArr[pX, pY] = !pMasuArr[pX, pY];
    }
}
0