Uva10285-Longest run on a snowboard (memory-based search)

Source: Internet
Author: User

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 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.