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

ABC183-E Queen on Grid


問題へのリンク


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("...");
            WillReturn.Add(".#.");
            WillReturn.Add("...");
            //10
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4 4");
            WillReturn.Add("...#");
            WillReturn.Add("....");
            WillReturn.Add("..#.");
            WillReturn.Add("....");
            //84
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("8 10");
            WillReturn.Add("..........");
            WillReturn.Add("..........");
            WillReturn.Add("..........");
            WillReturn.Add("..........");
            WillReturn.Add("..........");
            WillReturn.Add("..........");
            WillReturn.Add("..........");
            WillReturn.Add("..........");
            //13701937
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    const long Hou = 1000000007;

    class JyoutaiDef
    {
        internal long PatternCnt;
        internal long GainRCnt;  // 右のマスが貰える数
        internal long GainDCnt;  // 下のマスが貰える数
        internal long GainRDCnt; // 右下のマスが貰える数
    }

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

        char[,] BanArr = CreateBanArr(InputList.Skip(1));
        int UB_X = BanArr.GetUpperBound(0);
        int UB_Y = BanArr.GetUpperBound(1);

        // 状態[X座標,Y座標]な2次元配列
        JyoutaiDef[,] JyoutaiArr = new JyoutaiDef[UB_X + 1, UB_Y + 1];
        for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
            for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
                JyoutaiArr[LoopX, LoopY] = new JyoutaiDef();
            }
        }
        JyoutaiArr[0, 0].PatternCnt = 1;
        JyoutaiArr[0, 0].GainRCnt = 1;
        JyoutaiArr[0, 0].GainDCnt = 1;
        JyoutaiArr[0, 0].GainRDCnt = 1;

        Action<int, int> GainAct = (pBaseX, pBaseY) =>
        {
            // 左から貰う
            if (pBaseX > 0) {
                long GainCnt = JyoutaiArr[pBaseX - 1, pBaseY].GainRCnt;
                JyoutaiArr[pBaseX, pBaseY].PatternCnt += GainCnt;
                JyoutaiArr[pBaseX, pBaseY].GainRCnt += GainCnt * 2;
                JyoutaiArr[pBaseX, pBaseY].GainDCnt += GainCnt;
                JyoutaiArr[pBaseX, pBaseY].GainRDCnt += GainCnt;
            }
            // 上から貰う
            if (pBaseY > 0) {
                long GainCnt = JyoutaiArr[pBaseX, pBaseY - 1].GainDCnt;
                JyoutaiArr[pBaseX, pBaseY].PatternCnt += GainCnt;
                JyoutaiArr[pBaseX, pBaseY].GainRCnt += GainCnt;
                JyoutaiArr[pBaseX, pBaseY].GainDCnt += GainCnt * 2;
                JyoutaiArr[pBaseX, pBaseY].GainRDCnt += GainCnt;
            }
            // 左上から貰う
            if (pBaseX > 0 && pBaseY > 0) {
                long GainCnt = JyoutaiArr[pBaseX - 1, pBaseY - 1].GainRDCnt;
                JyoutaiArr[pBaseX, pBaseY].PatternCnt += GainCnt;
                JyoutaiArr[pBaseX, pBaseY].GainRCnt += GainCnt;
                JyoutaiArr[pBaseX, pBaseY].GainDCnt += GainCnt;
                JyoutaiArr[pBaseX, pBaseY].GainRDCnt += GainCnt * 2;
            }
            JyoutaiArr[pBaseX, pBaseY].PatternCnt %= Hou;
            JyoutaiArr[pBaseX, pBaseY].GainRCnt %= Hou;
            JyoutaiArr[pBaseX, pBaseY].GainDCnt %= Hou;
            JyoutaiArr[pBaseX, pBaseY].GainRDCnt %= Hou;
        };

        for (int LoopY = 0; LoopY <= UB_Y; LoopY++) {
            for (int LoopX = 0; LoopX <= UB_X; LoopX++) {
                if (BanArr[LoopX, LoopY] == '#') continue;

                GainAct(LoopX, LoopY);
            }
        }

        Console.WriteLine(JyoutaiArr[UB_X, UB_Y].PatternCnt);
    }

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

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

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


解説

貰うDPで解いてます。

クイーンの効きの直線上であれば、
貰える値が、2のべき上で増えていくので
貰う方向ごとに、貰える値を管理してます。