code1169 Pass the note

Source: Internet
Author: User

From: http://www.cnblogs.com/DSChan/p/4862019.html

The topic said to find two non-intersecting paths, in fact, can also be equivalent to (from) to (N,m) the two disjoint path.

If you are looking for only one, then return to the most classic dp[i][j] = max (dp[i-1][j],dp[i][j-1]) + a[i][j].

Now find two, you can start the array to four-dimensional.

Dp[x1][y1][x2][y2] = max{

DP[X1][Y1-1][X2][Y2-1],

Dp[x1-1][y1][x2-1][y2],

DP[X1-1][Y1][X2][Y2-1],

DP[X1][Y1-1][X2-1][Y2]

}

+ a[x1][y1] + a[x2][y2]

To two paths can not intersect, that is, dp[x][y][x][y] is an illegal state, judge can not go through this state.

The simplest method is to make x2>x1, because Dp[x1][y1][x2][y2] is equivalent to dp[x2][y2][x1][y1].

Since M and N are up to 50, the four-dimensional array is too large, and there is a lot of repetition and waste.

Brain repair, two notes at the same time, they pass the same number of people at the same time, according to this can be reduced dimension.

D[I][X1][X2] indicates that the x-coordinate of the two strips of step i is X1 and X2 respectively, then y1=i-x1+2,y2=i-x2+2.

So we can get the DP equation.

D[I][X1][X2] = max{d[i-1][x1][x2], d[i-1][x1-1][x2], d[i-1][x1][x2-1], d[i-1][x1-1][x2-1]} + a[x1][i-x1+2] + a[x2][i-x2 +2]

Note that the answer D[n+m-2][n][n] is to be counted (as this is "illegal state")

  1. #include <iostream>
  2. #include <cassert>
  3. #include <cstdio>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <string>
  8. #include <iterator>
  9. #include <cstdlib>
  10. #include <vector>
  11. #include <stack>
  12. #include <map>
  13. #include <set>
  14. Using namespace std;
  15. #define REP (i,f,t) for (int i = (f), _end_= (t); I <= _end_; ++i)
  16. #define REP2 (I,F,T) for (int i = (f), _end_= (t); i < _end_; ++i)
  17. #define DEP (I,F,T) for (int i = (f), _end_= (t); I >= _end_; i.)
  18. #define DEP2 (I,F,T) for (int i = (f), _end_= (t); i > _end_; i.)
  19. #define CLR (c, X) memset (c, X, sizeof (c))
  20. typedef Long long int64;
  21. const int INF = 0x5f5f5f5f;
  22. Const Double eps = 1e-8;
  23. //*****************************************************
  24. int d[100][55][55];
  25. int a[55][55];
  26. inline int max (int i,int J,int k,int l)
  27. {
  28. return Max (Max (i,j), Max (k,l));
  29. }
  30. int main ()
  31. {
  32. int n,m;
  33. scanf ("%d%d", &n,&m);
  34. Rep (I,1,n) Rep (j,1,m) scanf ("%d", &a[i][j]);
  35. Rep (I,1,n+m-2) Rep (X1,max (1,i+2-m), Min (n,i+1))
  36. {
  37. int y1 = i-x1 + 2;
  38. Rep (X2,max (x1+1,i+2-m), Min (n,i+1))
  39. {
  40. int y2 = i-x2 + 2;
  41. D[I][X1][X2] = max (d[i-1][x1-1][x2-1], d[i-1][x1][x2-1],
  42. D[I-1][X1-1][X2],D[I-1][X1][X2])
  43. + a[x1][y1] + a[x2][y2];
  44. }
  45. }
  46. D[n+m-2][n][n] = D[n+m-3][n-1][n];
  47. cout<<d[n+m-2][n][n]<<endl; ///Direct output D[N+M-3][N-1][N]
  48. return 0;
  49. }



code1169 Pass the note

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.