Question: uva10285-Longest run on a snowboard)
The N * n matrix is given, and a path must be found. The value on the path is decreasing, and the longest length of the path is obtained.
Solution: Memory words search. Because the longest path is required, you need to find all such paths, but the direct DFS will time out. For the same position, the longest path length starting from this point is fixed. Therefore, you need to calculate the longest path of this location and save it. At the beginning, I was worried that I would go back and keep going. However, it is not possible to go back from the right position if you can go to the right, because the value on the path must be decreased.
DP [x] [Y] indicates the longest path length that can be taken from the position X and Y.
Code:
#include
#include
const int n = 105; const int M = 4; const int dir [m] [2] = {0,-1}, {-1, 0}, {0, 1}, {1, 0 }}; int G [N] [N]; int DP [N] [N]; char name [N * 10]; int R, C; int max (const int A, const int B) {return A> B? A: B;} int dp (INT X, int y) {Int & Ans = DP [x] [Y]; If (ANS! =-1) return ans; int newx, newy; For (INT I = 0; I
= r | newy <0 | newy> = c) continue; if (G [x] [Y]> G [newx] [newy]) ans = max (ANS, dp (newx, newy) + 1 ); // The maximum length of the four directions starting from this position} If (ANS =-1) // This point cannot go out ans = 1; return ans ;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% S % d", name, & R, & C); For (INT I = 0; I
For (INT I = 0; I <r; I ++) it is possible to obtain the longest path for (Int J = 0; j <C; j ++) ans = max (ANS, dp (I, j); printf ("% s: % d \ n", name, ANS) ;}return 0 ;}