#K80777. Palindromic Grid Paths
Palindromic Grid Paths
Palindromic Grid Paths
You are given two integers m and n representing the number of rows and columns in a grid. The grid is indexed from (1,1) at the top-left corner to (m,n) at the bottom-right corner. Starting from cell (1,1), you can only move either right or down to reach cell (m,n).
Your task is to calculate the number of unique palindromic paths from the starting cell to the ending cell. A path is said to be palindromic if the sequence of moves (where 'R' represents a move to the right and 'D' represents a move down) reads the same forwards and backwards. In other words, if a path is represented by the string S, then the condition for it being palindromic is:
$$S = S^R$$
Note: A necessary condition for the existence of a palindromic path is that the total number of moves, which is $$m+n-2$$, must be even. If $$m+n-2$$ is odd, then no palindromic path exists.
Use an efficient dynamic programming approach to solve the problem.
inputFormat
A single line containing two space-separated integers, m and n, where m is the number of rows and n is the number of columns of the grid.
outputFormat
Output a single integer: the number of unique palindromic paths from cell (1,1) to cell (m,n).## sample
2 2
2