Jam ' s maze
Time limit:3000/1500 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 299 Accepted Submission (s): 136
Problem Description Jam got into a maze, he had to find a palindrome path from (in) to (N,n) to get out.
However he is isn't only thinking about how to get out of the The maze,
But also counting the number of ways he can get out.
The maze a n∗n grid. On all cell is a uppercase letter.
Jam is at Upper-left Corner (all) now, and the exit was at Lower-right Corner (n,n).
Jam can only go down or go right.
Considering the number of Palindrome path May is huge, you just need to output the number mod 5201314.
Input the first line was T (1≤t≤10) means T case.
For each case:
The first line is N (1≤n≤500) means the size of the maze.
Then n lines follow, each of the line have N uppercase letter.
Output for each testcase, output the number of Palindrome path mod 5201314.
Sample Input
1 4 ABCD Befe cdeb GCBA
Sample Output
Hint There was 1 solutions is "ABCDCBA" there was 1 solutions is "ABCGCBA" there was 4 solutions is "Abedeba" there was 6 Solutions is "Abefeba" problem description
Jam into a maze, he wants to get out of this maze, must find a path, make this path is a palindrome of
course he can not be bothered to go out of this maze, clever he must find out how many kinds of programs out of this maze
in a n*nn∗n size maze, The maze is made up of uppercase letters.
He will go from the upper left to the lower right corner, and then all the characters passed into a string, of course, can only go down and to the right, ask how many kinds of solutions can come out
of course the answer will be large, so the answer and 52013145201314 modulo output
Enter a description
The first line, T (1 \leq t \leq) t (1≤t≤10), represents the TT group data.
Next TT Group data:
The first behavior of each group of data n (1 \leq n \leq) n (1≤n≤500) represents the rows and columns of the matrix
next nn row nn column n*nn∗n characters
Output description
Input sample
1
4
ABCD
befe
cdeb
GCBA
Input sample
12
Output sample
There are 1 kinds of walking method is "ABCDCBA"
there are 1 kinds of way is "ABCGCBA"
there are 4 kinds of way is "Abedeba"
there are 6 kinds of walking method is "Abefeba"
so there are 12=6+4+1=1 species walk method feasible
Idea: (See the Great god: http://blog.csdn.net/chenzhenyu123456/article/details/50612893) Use the idea of interval DP, with Dp[x1][y1][x2][y2] to represent from (x1 , y1) points to (X2,y2) the number of methods between points, but because (1<=n<=500), so this is sure to explode, so the rolling array dp[g][x1][x2] represents the number of methods from X1 to X2 rows, of course, y1,y2 is to be determined, Determine the number of steps from the X1 line to the X2 row, and then you can determine the value of the y1,y2, such that an enumeration can ultimately determine the number of steps between DP[G][X1][X2]. The result is obtained by the value of the final cumulative dp[i][i].
#include <stdio.h> #include <string.h> #include <algorithm> #define INF 0x3f3f3f3f #define LL Long Long #
Define N 510 #define M 5201314 using namespace std;
Char Map[n][n];
int dp[2][n][n];
int main () {int t,n,m,i,j,k;
int s;
int x1,x2,y1,y2;
scanf ("%d", &t);
while (t--) {scanf ("%d", &n);
for (i=1;i<=n;i++) scanf ("%s", map[i]+1);
int g=0;
Memset (Dp,0,sizeof (DP));
Dp[g][1][n]= (Map[1][1]==map[n][n]);
for (s=1;s<n;s++)//number of steps {g^=1;
memset (dp[g],0,sizeof (dp[g)); for (x1=1;x1<=n;x1++) {for (x2=n;x2>=1;x2--) {if (x1-1>s| |
N-x2>s) continue;
int y1=s-(x1-1) +1;
int y2=n-(S (n-x2));
if (Map[x1][y1]!=map[x2][y2]) continue;
Dp[g][x1][x2]= (dp[g][x1][x2]+dp[g^1][x1][x2])%M;
Dp[g][x1][x2]= (dp[g][x1][x2]+dp[g^1][x1][x2+1])%M;
Dp[g][x1][x2]= (dp[g][x1][x2]+dp[g^1][x1-1][x2])%M;
Dp[g][x1][x2]= (dp[g][x1][x2]+dp[g^1][x1-1][x2+1])%M;
}}} int ans=0; for (i=1;i<=n;i++) ans= (ans+dp[g][i][i])%M;
printf ("%d\n", ans);
} return 0; }