Editing
2015 Open Bronze Problem 4 Palindromic Paths (Bronze)
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Solution == The solution to the problem involves using dynamic programming and depth-first search. The key insight to solve this problem is that any palindromic path from (1,1) to (N,M) must pass through the center of the grid. So, we start from (1,1) and (N,M), and move towards the center of the grid simultaneously. This way, we ensure that the sequence of numbers in the cells we pass through is always a palindrome. We use a 4-dimensional array to store our dynamic programming state. The first two dimensions represent the current positions (r1,c1) and (r2,c2) that we are at while moving from (1,1) and (N,M) respectively. The last dimension represents the distance that we have moved from (1,1) towards the center of the grid. For each state, we check if the numbers at (r1,c1) and (r2,c2) are the same. If they are not, we cannot continue along this path because it would not form a palindrome. If they are the same, we recursively consider all possible ways to move from (r1,c1) and (r2,c2) towards the center of the grid, and add up the number of palindromic paths. Finally, we return the total number of palindromic paths from the initial state (1,1) and (N,M). The time complexity of this solution is O(N^2 * M^2) because for each state, we consider four possible next states, and there are N^2 * M^2 possible states.
Summary:
Please note that all contributions to Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
My wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information