結果
問題 | No.1916 Making Palindrome on Gird |
ユーザー |
![]() |
提出日時 | 2022-05-01 18:06:35 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 397 ms / 3,000 ms |
コード長 | 1,735 bytes |
コンパイル時間 | 371 ms |
コンパイル使用メモリ | 82,380 KB |
実行使用メモリ | 210,808 KB |
最終ジャッジ日時 | 2024-06-30 20:04:57 |
合計ジャッジ時間 | 8,324 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 30 |
ソースコード
#!/usr/bin/env PyPy3 from collections import Counter, defaultdict, deque import itertools import re import math from functools import reduce import operator import bisect from heapq import * import functools mod=10 ** 9 + 7 import sys input=sys.stdin.readline h,w=map(int,input().split()) if h == w == 1: print(1) exit() s=[input().rstrip() for _ in range(h)] dp = [[[0] * h for _ in range(h)] for _ in range(h + w - 1)] if s[0][0] == s[-1][-1]: dp[0][0][h-1] = 1 ans = 0 for i in range(1,(h+w-1) // 2 + 1): for sh in range(h): sw = i - sh if 0 <= sh < h and 0 <= sw < w: for gh in range(h): gw = h + w - 2 - i - gh if 0 <= gh < h and 0 <= gw < w: if s[sh][sw] == s[gh][gw]: tmp1 = 0 if sh > 0 and gh < h - 1: tmp1 += dp[i-1][sh - 1][gh + 1] if sh > 0 and gw < w - 1: tmp1 += dp[i-1][sh-1][gh] tmp1 %= mod if sw > 0 and gh < h - 1: tmp1 += dp[i-1][sh][gh + 1] tmp1 %= mod if sw > 0 and gw < w - 1: tmp1 += dp[i-1][sh][gh] tmp1 %= mod if gh >= sh and gw >= sw and gh + gw - sh - sw <= 1: ans += tmp1 ans %= mod else: dp[i][sh][gh] = tmp1 print(ans) #print(dp) else: print(0)