Source
Third Henan Program Design Competition
After a simple DP problem, I began to see that this question was always intended to be searched in two directions. Later I found that I could not write it, because the starting values of the two directions here are the same each time, there should be a problem with the idea, so I changed my mind and used dynamic planning for it, the idea of dynamic planning is to start from the back to ensure the optimal current position. Therefore, we directly simulate the route that KK has traveled. kk can only go to the right or down, we compare the two directions and select a maximum value. The number of current steps depends on the position of the previous step;
So we can get the state relationship, DP [I] [J] = max (DP [I] [J-1], DP [I-1] [J]) + map [I] [J]; because it is recursion starting from the back, the first step of the maximum value is upward or left;
The following is the code;
# Include <cstdio> # include <cstring> # define max (A, B) A> B? A: bint map [25] [25], DP [25] [25]; int main () {int n, m, I, J; scanf ("% d", & N, & M); for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) scanf ("% d", & map [I] [J]); for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) // DP [I] [J] = max (DP [I-1] [J] + map [I] [J], DP [I] [J-1] + map [I] [J]); // The two Recursive Relations can map [I] [J] + = max (Map [I] [J-1], map [I-1] [J]); printf ("% d \ n", map [N] [m]);}