/* This problem to solve two questions 1) state and State Equation 2) How to ensure that each step, the resulting path does not intersect, to ensure that the final generation of the complete path does not intersect.(1) Status:DP[I][J][K][L] = The small yuan transmits the note to [i][j] position, the small Xuan passes the note to [k][l] position, the kindness degree and the maximum value.(2) state equation:See the topic, the direct idea is: Press test instructions, Obuchi from the upper left corner, the lower right corner, small Xuan from the lower right corner, the upper left corner. However, the path in the walk, how to ensure that the path does not intersect, I can not figure out the way ... Think carefully, the title requires: two path of the same head and tail coordinates (starting point selection is different), but actually choose the first or the end of which point as the starting point, are irrelevant. So let two of the path from the point [1][1], this approach has a relatively simple way to ensure that the path does not intersect. DP[I][J][K][L] = Arr[i][j] + arr[k][l] +max{dp[i-1][j][k-1][l], dp[i-1][j][k][l-1], dp[i][j-1][k-1][l], dp[i][j-1][k][ L-1]}(3) control conditions (guaranteed to obtain disjoint paths):Two paths from the point [1][1], let two path parallel to go, and ensure that Obuchi go to the point [I][j], the small Xuan go to [k][l], the formation of the path does not intersect: Key: 1) Parallel (i+j) = (k+l) 2) disjoint if (i==k) && (j==l) return false;(4) Get the answer:Two path to reach Point [M][n], then two paths select the point to reach the destination is necessarily selected: [M][n-1],[m-1][n] (because there are only two actions to reach the destination: down, to the left): ans = max (dp[m][n-1][m-1][n], dp[m-1] N [M] [N-1]) */
1 #define_crtdbg_map_alloc2#include <stdlib.h>3#include <crtdbg.h>4 voidFnExit1 (void)5 {6 _CrtDumpMemoryLeaks ();7 }8 9 #define_crt_secure_no_warningsTen #defineHOME One A#include <iostream> -#include <cstdlib> -#include <cstdio> the#include <cstddef> -#include <iterator> -#include <algorithm> -#include <locale> +#include <cmath> -#include <vector> +#include <cstring> A using namespacestd; at Const intINF =0x3f3f3f3f; - Const intMAXN = -; - Const intMax = -; - - intm, N; - intArr[max][max]; in intDp[max][max][max][max]; - intstep[2][2] = { {-1,0}, {0, -1} };//down, left to + BOOLCheck (intX1,intY1,intX2,inty2) - { the //prevent cross-border * if((X1 <=0) || (Y1 <=0) || (X2 <=0) || (Y2 <=0)) $ {Panax Notoginseng return false; - } the //two paths cannot intersect + if(x1 = = x2) && (y1 = =y2)) A { the return false; + } - return true; $ } $ - voidSolve () - { the intx1, y1, x2, y2; - for(inti =1; I <= m; ++i)Wuyi { the for(intj =1; J <= N; ++j) - { Wu for(intK =1; K <= m; ++k) - { About $ //two path and walk, then: (i+j) = (k+l) - if(i + j-k <=0) Break; - intL = i + J-K; - if(! Check (I, J, K, L))Continue; A intMaxval =0; + for(intp =0; P <2; ++p) the { - for(intQ =0; Q <2; ++q) $ { theX1 = i + step[p][0]; theY1 = j + step[p][1]; thex2 = k + step[q][0]; they2 = l + step[q][1]; - if(Check (x1, y1, x2, y2)) in { theMaxval =Max (Maxval, Dp[x1][y1][x2][y2]); the } About } the } theDP[I][J][K][L] = max (Dp[i][j][k][l], Maxval + arr[i][j] +arr[k][l]); the } + } - } the //two path to reach Point [M][n], then the point of the two path must choose: [M][n-1],[m-1][n]Bayi intans = max (Dp[m][n-1][m-1][n], Dp[m-1][n][m][n-1]); the //for (int i = 1; I <= m; ++i) the //{ - //For (int j = 1; j <= N; ++j) - // { the //for (int k = 1; k <= m; ++k) the // { the //for (int l = 1; l <= N; ++l) the // { - // //cout << "(" << I << "," << J << "," << k << "," << l << ")" << ":" << dp[i][j][k][l] << Endl; the //cout << Dp[i][j][k][l] << "--" << dp[k][l][i][j] << Endl; the /// * Two path from the same starting point, do not distinguish between two paths, that is, Obuchi and small Xuan How to allocate these two paths can be, the //so Dp[i][j][k][l]=dp[k][l][i][j]94 // */ the // } the // } the // }98 //} Aboutcout << ans <<Endl; - //cout << dp[m][n-1][m-1][n] << "<< dp[m-1][n][m][n-1] << Endl;//Two values are the same101 }102 103 intMain ()104 { the 106 #ifdef HOME107 //freopen ("in", "R", stdin);108 //freopen ("Out", "w", stdout);109 //atexit (fnExit1); the #endif111 theMemset (DP,0,sizeof(0));113CIN >> M >>N; the for(inti =1; I <= m; ++i) the { the for(intj =1; J <= N; ++j)117 {118CIN >>Arr[i][j];119 } - }121 Solve ();122 123 124 #ifdef HOME theCerr <<"Time Elapsed:"<< clock ()/Clocks_per_sec <<"Ms"<<Endl;126 _CrtDumpMemoryLeaks ();127System"Pause"); - #endif129 return 0; the}
Code[vs of the dynamic planning of checkerboard Type] 1169 notes 2008 NOIP National League Improvement Group