Test instructions: give you a n*m character matrix, ask you to walk from (to n,m) How many methods are passed by the string is a palindrome, can only go right or down
DP[][I][J] I J stands for the two points that are currently going to the horizontal axis so that (i,y1) (n,m)--(J,Y2) Get the same number of methods
#include <bits/stdc++.h>using namespacestd;Const Long LongMoD = 1e9 +7;Charp[510][510];Long Longdp[2][510][510];intdir[4][2]={1,0,0,1,0,-1,-1,0};intn,m;voidAddLong Long&a,Long Longb) {a+=b; if(A>=mod) a-=MoD;}BOOLJudgeintXinty) { if(x>=0&&x<n&&y>=0&&Y<M)return 1; return 0;}Long Longsolve () {if(p[0][0]!=p[n-1][m-1])return 0; if(n==1&&m==1)return 1; intCur,nex; Memset (DP,0,sizeof(DP)); dp[0][0][n-1]=1;//Dp[0][i][j] Enumerates the horizontal axis of two points intnum; if((n+m-1) &1) num= (n+m-2)/2; ElseNum= (n+m-1)/2-1; for(intI=0; i<num;i++) {cur=i&1; NEX=cur^1; for(intj=0; j<n;j++) for(intk=0; k<n;k++)if(Dp[cur][j][k]) {intX1,y1,x2,y2; X1=j;y1=i-J; X2=k;y2=m-1-(I-(n1-k)); for(intIi=0;ii<2; ii++){ intxx1=x1+dir[ii][0]; intyy1=y1+dir[ii][1]; if(!judge (XX1,YY1))Continue; for(intjj=2;jj<4; jj++){ intxx2=x2+dir[jj][0]; intyy2=y2+dir[jj][1]; if(!judge (Xx2,yy2))Continue; if(xx1>xx2| | YY1>YY2)Continue; if(p[xx1][yy1]==P[xx2][yy2]) Add (dp[nex][xx1][xx2],dp[cur][j][k]); }} Dp[cur][j][k]=0; } } Long Longans=0; for(intI=0; i<n;i++) for(intj=0; j<n;j++) Add (ans,dp[num&1][i][j]); returnans;}intMain () { while(cin>>n>>m) { for(intI=0; i<n;i++) scanf ("%s", P[i]); printf ("%lld\n", Solve ()); }}
View Code
Codeforces Round #316 (Div. 2) E