AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC256-C Filling 3x3 array


問題へのリンク


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 4 6 3 3 7");
            //1
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("3 4 5 6 7 8");
            //0
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("5 13 10 6 13 9");
            //120
        }
        else if (InputPattern == "Input4") {
            WillReturn.Add("20 25 30 22 29 24");
            //30613
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static int mH1;
    static int mH2;
    static int mH3;
    static int mW1;
    static int mW2;
    static int mW3;

    const int UB = 2;

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        mH1 = wkArr[0];
        mH2 = wkArr[1];
        mH3 = wkArr[2];
        mW1 = wkArr[3];
        mW2 = wkArr[4];
        mW3 = wkArr[5];

        ExecDFS();
    }

    struct JyoutaiDef
    {
        internal int[,] BanArr;
        internal int Curr_X;
        internal int Curr_Y;
    }

    static void ExecDFS()
    {
        var Stk = new Stack<JyoutaiDef>();
        JyoutaiDef WillPush;
        WillPush.BanArr = new int[3, 3];
        WillPush.Curr_X = 0;
        WillPush.Curr_Y = 0;
        Stk.Push(WillPush);

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

            for (int I = 1; I <= 28; I++) {
                WillPush.BanArr = (int[,])Popped.BanArr.Clone();
                WillPush.BanArr[Popped.Curr_X, Popped.Curr_Y] = I;
                WillPush.Curr_X = Popped.Curr_X + 1;
                WillPush.Curr_Y = Popped.Curr_Y;
                if (WillPush.Curr_X > UB) {
                    WillPush.Curr_X = 0;
                    WillPush.Curr_Y++;
                }

                if (Popped.Curr_X == 1 && Popped.Curr_Y == 0) {
                    int Sum = WillPush.BanArr[0, 0] + WillPush.BanArr[1, 0];
                    if (Sum > mW1) break;
                }
                if (Popped.Curr_X == 0 && Popped.Curr_Y == 1) {
                    int Sum = WillPush.BanArr[0, 1] + WillPush.BanArr[1, 1];
                    if (Sum > mH1) break;
                }
                if (Popped.Curr_X == 1 && Popped.Curr_Y == 1) {
                    int Sum1 = WillPush.BanArr[1, 0] + WillPush.BanArr[1, 1];
                    if (Sum1 > mH2) break;

                    int Sum2 = WillPush.BanArr[0, 1] + WillPush.BanArr[1, 1];
                    if (Sum2 > mW2) break;
                }

                if (Popped.Curr_X == 2 && Popped.Curr_Y == 1) {
                    int Sum = WillPush.BanArr[2, 0] + WillPush.BanArr[2, 1];
                    if (Sum > mH3) break;
                }

                if (Popped.Curr_X == 2 && Popped.Curr_Y == 0) {
                    if (IsValidYoko(0, WillPush.BanArr) == false) continue;
                }
                if (Popped.Curr_X == 2 && Popped.Curr_Y == 1) {
                    if (IsValidYoko(1, WillPush.BanArr) == false) continue;
                }
                if (Popped.Curr_X == 2 && Popped.Curr_Y == 2) {
                    if (IsValidYoko(2, WillPush.BanArr) == false) continue;
                    if (IsValidTate(2, WillPush.BanArr) == false) continue;
                }

                if (Popped.Curr_X == 0 && Popped.Curr_Y == 2) {
                    if (IsValidTate(0, WillPush.BanArr) == false) continue;
                }
                if (Popped.Curr_X == 1 && Popped.Curr_Y == 2) {
                    if (IsValidTate(1, WillPush.BanArr) == false) continue;
                }

                if (Popped.Curr_X == 2 && Popped.Curr_Y == 2) {
                    Answer++;
                    continue;
                }

                Stk.Push(WillPush);
            }
        }
        Console.WriteLine(Answer);
    }

    // Y座標を引数として横計のチェック
    static bool IsValidYoko(int pY, int[,] pBanArr)
    {
        int Sum = 0;
        for (int X = 0; X <= UB; X++) {
            Sum += pBanArr[X, pY];
        }
        if (pY == 0) return Sum == mW1;
        if (pY == 1) return Sum == mW2;
        return Sum == mW3;
    }

    // X座標を引数として縦計のチェック
    static bool IsValidTate(int pX, int[,] pBanArr)
    {
        int Sum = 0;
        for (int Y = 0; Y <= UB; Y++) {
            Sum += pBanArr[pX, Y];
        }
        if (pX == 0) return Sum == mH1;
        if (pX == 1) return Sum == mH2;
        return Sum == mH3;
    }
}


解説

枝切りを頑張るDFSで解いてます。