結果

問題 No.1916 Making Palindrome on Gird
ユーザー とりゐとりゐ
提出日時 2022-04-12 12:58:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 910 ms / 3,000 ms
コード長 1,003 bytes
コンパイル時間 349 ms
コンパイル使用メモリ 87,044 KB
実行使用メモリ 146,060 KB
最終ジャッジ日時 2023-08-22 18:14:40
合計ジャッジ時間 16,816 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 69 ms
71,272 KB
testcase_01 AC 69 ms
71,392 KB
testcase_02 AC 114 ms
77,480 KB
testcase_03 AC 70 ms
71,264 KB
testcase_04 AC 99 ms
77,008 KB
testcase_05 AC 93 ms
76,444 KB
testcase_06 AC 92 ms
76,680 KB
testcase_07 AC 97 ms
77,080 KB
testcase_08 AC 107 ms
77,188 KB
testcase_09 AC 71 ms
71,300 KB
testcase_10 AC 69 ms
71,240 KB
testcase_11 AC 96 ms
77,244 KB
testcase_12 AC 86 ms
76,336 KB
testcase_13 AC 402 ms
102,360 KB
testcase_14 AC 312 ms
95,016 KB
testcase_15 AC 465 ms
109,084 KB
testcase_16 AC 360 ms
96,964 KB
testcase_17 AC 792 ms
136,196 KB
testcase_18 AC 852 ms
146,036 KB
testcase_19 AC 910 ms
146,000 KB
testcase_20 AC 845 ms
146,060 KB
testcase_21 AC 851 ms
145,884 KB
testcase_22 AC 846 ms
145,968 KB
testcase_23 AC 633 ms
144,376 KB
testcase_24 AC 612 ms
144,496 KB
testcase_25 AC 604 ms
144,688 KB
testcase_26 AC 625 ms
145,496 KB
testcase_27 AC 621 ms
144,784 KB
testcase_28 AC 825 ms
145,720 KB
testcase_29 AC 829 ms
145,888 KB
testcase_30 AC 827 ms
145,888 KB
testcase_31 AC 838 ms
146,012 KB
testcase_32 AC 824 ms
145,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod=10**9+7

h,w=map(int,input().split())
s=[]
for i in range(h):
  s.append(list(input()))

def check(x,y):
  return 0<=x<h and 0<=y<w

dp=[[[0]*h for i in range(w)] for j in range(h)]
if (h+w)%2==0:
  for i1 in range(h):
    j1=(h+w-2)//2-i1
    if 0<=j1<w:
      dp[i1][j1][i1]=1
else:
  for i1 in range(h):
    j1=(h+w-3)//2-i1
    if 0<=j1<w:
      for i2,j2 in [(i1+1,j1),(i1,j1+1)]:
        if check(i2,j2) and s[i1][j1]==s[i2][j2]:
          dp[i1][j1][i2]=1

dx1=[-1,0]
dy1=[0,-1]
dx2=[1,0]
dy2=[0,1]

for m in range((h+w-2)//2,0,-1):
  for i1 in range(h):
    j1=m-i1
    if not 0<=j1<w:
      continue
    for i2 in range(h):
      j2=h+w-2-m-i2
      if not 0<=j2<w:
        continue
      for di1,dj1 in zip(dx1,dy1):
        for di2,dj2 in zip(dx2,dy2):
          ni1,nj1,ni2,nj2=i1+di1,j1+dj1,i2+di2,j2+dj2
          if check(ni1,nj1) and check(ni2,nj2) and s[ni1][nj1]==s[ni2][nj2]:
            dp[ni1][nj1][ni2]+=dp[i1][j1][i2]
            dp[ni1][nj1][ni2]%=mod
  
print(dp[0][0][h-1])
0