https://www.51nod.com/Challenge/Problem.html#!#problemId=1503
dp[i][j][k][l]代表从(1,1)到(i,j)与从(n,m)到(k,l)对称相等的路径有多少
转移方程为dp[i][j][k][l]=dp[i-1][j][k+1][l]+dp[i-1][j][k][l+1]+dp[i][j-1][k+1][l]+dp[i][j-1][k][l+1]
因i-1+j-1=n-k+m-l恒成立 所以由i j k可推l 可以优化掉一维 又因为dp[i]...只与dp[i-1] dp[i]有关 所以滚动数组再优化掉一维
因为用了滚动数组 细节有点多 代码太挫
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int maxn=5e2+10;
ll dp[2][maxn][maxn];
int n,m;
char ch[maxn][maxn];
bool judge(int x1,int y1,int x2,int y2)
{
if(1<=x1&&x1<=n&&1<=y1&&y1<=m&&1<=x2&&x2<=n&&1<=y2&&y2<=m&&ch[x1][y1]==ch[x2][y2]&&x1+y1-2==n-x2+m-y2&&x1<=x2&&y1<=y2) return 1;
else return 0;
}
int main()
{
ll ans;
int i,j,k;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
scanf("%s",ch[i]+1);
}
dp[1][1][n]=1;
ans=0;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
for(k=n-i-j+2;k<=n;k++){
if(!(i==1&&j==1&&k==n)) dp[i%2][j][k]=0;
if(judge(i,j,k,n+m-i-j-k+2)){
if(judge(i-1,j,k,n+m-(i-1)-j-k+2)) dp[i%2][j][k]+=dp[(i+1)%2][j][k];
if(judge(i-1,j,k+1,n+m-i-j-k+2)) dp[i%2][j][k]+=dp[(i+1)%2][j][k+1];
if(judge(i,j-1,k,n+m-i-(j-1)-k+2)) dp[i%2][j][k]+=dp[i%2][j-1][k];
if(judge(i,j-1,k+1,n+m-i-j-k+2)) dp[i%2][j][k]+=dp[i%2][j-1][k+1];
dp[i%2][j][k]%=mod;
//if(judge(i,j,k,n+m-i-j-k+2))
if((k-i)+(n+m-i-j-k+2-j)<=1){
//printf("*%d %d %d %d %lld*\n",i,j,k,n+m-i-j-k+2,dp[i%2][j][k]);
ans=(ans+dp[i%2][j][k])%mod;
}
}
}
}
}
printf("%lld\n",ans);
return 0;
}