結果

問題 No.1916 Making Palindrome on Gird
ユーザー 👑 kakel-sankakel-san
提出日時 2022-04-29 23:59:25
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 401 ms / 3,000 ms
コード長 4,402 bytes
コンパイル時間 1,197 ms
コンパイル使用メモリ 66,280 KB
実行使用メモリ 51,528 KB
最終ジャッジ日時 2023-09-11 15:44:31
合計ジャッジ時間 7,805 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
21,888 KB
testcase_01 AC 63 ms
21,724 KB
testcase_02 AC 63 ms
21,928 KB
testcase_03 AC 64 ms
23,936 KB
testcase_04 AC 63 ms
19,724 KB
testcase_05 AC 63 ms
23,952 KB
testcase_06 AC 63 ms
19,800 KB
testcase_07 AC 63 ms
23,888 KB
testcase_08 AC 62 ms
22,024 KB
testcase_09 AC 63 ms
21,940 KB
testcase_10 AC 64 ms
21,856 KB
testcase_11 AC 63 ms
21,904 KB
testcase_12 AC 64 ms
23,928 KB
testcase_13 AC 116 ms
33,004 KB
testcase_14 AC 94 ms
30,316 KB
testcase_15 AC 142 ms
36,964 KB
testcase_16 AC 115 ms
30,304 KB
testcase_17 AC 208 ms
44,532 KB
testcase_18 AC 282 ms
51,516 KB
testcase_19 AC 280 ms
51,392 KB
testcase_20 AC 280 ms
49,472 KB
testcase_21 AC 284 ms
49,420 KB
testcase_22 AC 280 ms
51,528 KB
testcase_23 AC 65 ms
21,928 KB
testcase_24 AC 65 ms
23,916 KB
testcase_25 AC 63 ms
21,872 KB
testcase_26 AC 62 ms
23,912 KB
testcase_27 AC 62 ms
23,836 KB
testcase_28 AC 393 ms
47,372 KB
testcase_29 AC 398 ms
49,568 KB
testcase_30 AC 401 ms
51,416 KB
testcase_31 AC 395 ms
47,304 KB
testcase_32 AC 395 ms
49,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static string[] SList(int n) => Enumerable.Repeat(0, n).Select(_ => ReadLine()).ToArray();
    static void Main()
    {
        var c = NList;
        var (h, w) = (c[0], c[1]);
        var map = SList(h);
        if (map[0][0] != map[h - 1][w - 1])
        {
            WriteLine(0);
            return;
        }
        if (h == 1 && w == 1)
        {
            WriteLine(1);
            return;
        }
        var mod = 1_000_000_007;
        var maxl = (h + w - 1) / 2;
        var dp = new long[maxl][][];
        dp[0] = new long[1][] { new [] { 1L } };
        for (var i = 1; i < maxl; ++i)
        {
            var size = Math.Min(i + 1, Math.Min(h, w));
            dp[i] = new long[size][];
            for (var j = 0; j < dp[i].Length; ++j) dp[i][j] = new long[size];
            for (var j = 0; j < size; ++j)
            {
                var dj = Pos(h, w, i, j);
                for (var k = 0; k < size; ++k)
                {
                    var dk = RevPos(h, w, i, k);
                    if (map[dj.h][dj.w] != map[dk.h][dk.w]) continue;
                    if (!Reachable(dj.h, dj.w, dk.h, dk.w)) continue;
                    var sub = 0L;
                    if (dj.h > 0 && dk.w + 1 < w && map[dj.h - 1][dj.w] == map[dk.h][dk.w + 1])
                    {
                        var pj = Lv(h, w, dj.h - 1, dj.w);
                        var pk = RevLv(h, w, dk.h, dk.w + 1);
                        sub += dp[i - 1][pj.i][pk.i];
                    }
                    if (dj.h > 0 && dk.h + 1 < h && map[dj.h - 1][dj.w] == map[dk.h + 1][dk.w])
                    {
                        var pj = Lv(h, w, dj.h - 1, dj.w);
                        var pk = RevLv(h, w, dk.h + 1, dk.w);
                        sub += dp[i - 1][pj.i][pk.i];
                    }
                    if (dj.w > 0 && dk.w + 1 < w && map[dj.h][dj.w - 1] == map[dk.h][dk.w + 1])
                    {
                        var pj = Lv(h, w, dj.h, dj.w - 1);
                        var pk = RevLv(h, w, dk.h, dk.w + 1);
                        sub += dp[i - 1][pj.i][pk.i];
                    }
                    if (dj.w > 0 && dk.h + 1 < h && map[dj.h][dj.w - 1] == map[dk.h + 1][dk.w])
                    {
                        var pj = Lv(h, w, dj.h, dj.w - 1);
                        var pk = RevLv(h, w, dk.h + 1, dk.w);
                        sub += dp[i - 1][pj.i][pk.i];
                    }
                    dp[i][j][k] = sub % mod;
                }
            }
        }
        var res = 0L;
        if ((h + w) % 2 == 0)
        {
            var size = Math.Min(maxl, Math.Min(h, w));
            for (var i = 0; i < size; ++i) for (var j = 0; j < size; ++j)
            {
                var pi = Pos(h, w, maxl - 1, i);
                var pj = RevPos(h, w, maxl - 1, j);
                if (!Reachable(pi.h, pi.w, pj.h, pj.w)) continue;
                if (pi.w - pi.h == pj.w - pj.h) res += dp[maxl - 1][i][j] * 2;
                else res += dp[maxl - 1][i][j];
            }
        }
        else
        {
            var size = dp.Last().Length;
            for (var i = 0; i < size; ++i) res += dp[maxl - 1][i][i];
            if (h > w) for (var i = 0; i < size - 1; ++i) res += dp[maxl - 1][i + 1][i];
            else for (var i = 0; i < size - 1; ++i) res += dp[maxl - 1][i][i + 1];
        }
        WriteLine(res % mod);
    }
    static (int h, int w) Pos(int h, int w, int lv, int i)
    {
        var rh = (lv >= w ? lv - w + 1 : 0) + i;
        return (rh, lv - rh);
    }
    static (int h, int w) RevPos(int h, int w, int lv, int i)
    {
        var sum = h + w - 2 - lv;
        var rh = Math.Max(0, sum - w + 1) + i;
        return (rh, sum - rh);
    }
    static (int lv, int i) Lv(int h, int w, int rh, int rw)
    {
        var lv = rh + rw;
        var toph = Math.Max(0, lv - w + 1);
        return (lv, rh - toph);
    }
    static (int lv, int i) RevLv(int h, int w, int rh, int rw)
    {
        var lv = Lv(h, w, rh, rw);
        return (h + w - 2 - lv.lv, lv.i);
    }
    static bool Reachable(int h1, int w1, int h2, int w2)
    {
        return h1 <= h2 && w1 <= w2;
    }
}
0