典型90問    次の典型90問へ    前の典型90問へ

典型90問 079 Two by Two(★3)


問題へのリンク


C#のソース

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("3 3");
            WillReturn.Add("0 0 0");
            WillReturn.Add("0 0 0");
            WillReturn.Add("0 0 0");
            WillReturn.Add("1 1 0");
            WillReturn.Add("1 1 0");
            WillReturn.Add("0 0 0");
            //Yes
            //1
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 3");
            WillReturn.Add("0 0 0");
            WillReturn.Add("0 0 0");
            WillReturn.Add("0 0 0");
            WillReturn.Add("0 0 0");
            WillReturn.Add("0 1 0");
            WillReturn.Add("0 0 0");
            //No
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5 5");
            WillReturn.Add("6 17 18 29 22");
            WillReturn.Add("39 50 25 39 25");
            WillReturn.Add("34 34 8 25 17");
            WillReturn.Add("28 48 25 47 42");
            WillReturn.Add("27 47 24 32 28");
            WillReturn.Add("4 6 3 29 28");
            WillReturn.Add("48 50 21 48 29");
            WillReturn.Add("44 44 19 47 28");
            WillReturn.Add("4 49 46 29 28");
            WillReturn.Add("4 49 45 1 1");
            //Yes
            //140
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int H = wkArr[0];

        int[,] BanArr1 = CreateBanArr(InputList.Skip(1).Take(H));
        int UB_X = BanArr1.GetUpperBound(0);
        int UB_Y = BanArr1.GetUpperBound(1);

        int[,] BanArr2 = CreateBanArr(InputList.Skip(1 + H));

        long CostSum = 0;
        for (int LoopX = 0; LoopX <= UB_X - 1; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y - 1; LoopY++) {
                int Val1 = BanArr1[LoopX, LoopY];
                int Val2 = BanArr2[LoopX, LoopY];

                int Diff = Val2 - Val1;
                if (Diff != 0) {
                    CostSum += Math.Abs(Diff);
                    BanArr1[LoopX, LoopY] += Diff;
                    BanArr1[LoopX, LoopY + 1] += Diff;
                    BanArr1[LoopX + 1, LoopY] += Diff;
                    BanArr1[LoopX + 1, LoopY + 1] += Diff;
                }
            }
        }

        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                if (BanArr1[LoopX, LoopY] != BanArr2[LoopX, LoopY]) {
                    Console.WriteLine("No");
                    return;
                }
            }
        }
        Console.WriteLine("Yes");
        Console.WriteLine(CostSum);
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をintの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static int[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new int[0, 0];
        }

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

        SplitAct(StrList[0]);

        int UB_X = IntArr.GetUpperBound(0);
        int UB_Y = StrList.Count - 1;

        int[,] WillReturn = new int[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            SplitAct(StrList[Y]);
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = IntArr[X];
            }
        }
        return WillReturn;
    }
}


解説

Zオーダーでナイーブにチェックしてます。