Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class palpath{
- public static void main (String [] args) throws IOException {
- BufferedReader f = new BufferedReader(new FileReader("palpath.in"));
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("palpath.out")));
- StringTokenizer sc = new StringTokenizer(f.readLine());
- int n = Integer.parseInt(sc.nextToken());
- long mod = (long)1000000007;
- //long dp[][][]= new long[n][n][n];//d[i][j][k] corresponds to the points (j,i-j) (0<=j<=i) and (n-1-k,n-1-(i-k)) under similar constraints. i ranges from 0 to n-1.
- char square[][] = new char[n][n];
- for(int i = 0;i<n;i++){
- String s = f.readLine();
- for(int j = 0;j<n;j++){
- square[i][j]=s.charAt(j);
- }
- }
- long dpp1[][] = new long[n][n];//darn memory is hard.
- long dpp2[][] = new long[n][n];
- //dp[0][0][0]=(square[0][0]==square[n-1][n-1] ? 1L : 0L);
- dpp2[0][0]=(square[0][0]==square[n-1][n-1] ? 1L : 0L);
- for(int i = 1;i<n;i++){
- for(int j = 0;j<i;j++){
- for(int k = 0;k<i;k++){
- dpp1[j][k]=dpp2[j][k];
- }
- }
- for(int j = 0;j<=i;j++){
- for(int k = 0;k<=i;k++){
- if(square[j][i-j]==square[n-1-k][n-1-i+k]){
- // System.out.println("yes at " + i + " " + j + " " + k);
- dpp2[j][k]=0L;
- dpp2[j][k]+=(j>0 ? (k>0 ? dpp1[j-1][k-1] : 0L) : 0L);
- dpp2[j][k]+=(j>0 ? (k<i ? dpp1[j-1][k] : 0L) : 0L);
- dpp2[j][k]+=(j<i ? (k>0 ? dpp1[j][k-1] : 0L) : 0L);
- dpp2[j][k]+=(j<i ? (k<i ? dpp1[j][k] : 0L) : 0L);
- dpp2[j][k]=dpp2[j][k]%mod;
- }
- else{
- dpp2[j][k]=0L;
- }
- }
- }
- }
- long answer = 0L;
- for(int i = 0;i<n;i++){
- answer+=dpp2[i][n-1-i];
- answer=answer%mod;
- }
- out.println(answer);
- out.close();
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement