First walk with Dfs enumeration in each case, the second walk with DP to find the remaining maximum value
Sets a point set Q is used to save the value of the point, sort, at the end of the add an end point: X=m+1,y=m+1,v=0//m is the matrix length and width
Because v=0 points are meaningless, ignore them and use Q for DFS
The subscript for the current point in Q is P, and the accumulated score is score:
for (int i=p+1; i<=n+1; i++) { if(q[i].y>=q[p].y) {// a[q[i].x][q[i].y]=0; DFS (I,score+q[i].v); A[Q[I].X][Q[I].Y]=q[i].v; }}
When P=n+1, the end point (n is the number of points of value, n+1 is the last added end)
In the incomplete a DP find the second maximum value, update ans can
The code is as follows:
#include <iostream>#include<algorithm>using namespacestd;structpoint{intx, y; intv;} q[ the];BOOLCNT (Point C,point D) {returnc.x<d.x | | (C.x==d.x && c.y<d.y);}intN,m;//m is the edge length, n is the number of points with valuesinta[ the][ the];intd[ the][ the];intans=0; intdp () { for(intI=1; i<=m;i++){ for(intj=1; j<=m;j++) {D[i][j]=max (d[i-1][j],d[i][j-1])+A[i][j]; } } returnd[m][m];}voidDfsintPintscore) { if(p==n+1){ intC=DP (); if(Ans<c+score) ans=c+score; return; } for(inti=p+1; i<=n+1; i++){ if(Q[I].Y>=Q[P].Y) {//can be reacheda[q[i].x][q[i].y]=0; DFS (I,score+q[i].v); A[Q[I].X][Q[I].Y]=q[i].v; } }}intMain () {CIN>>m; for(n=1;true; n++) {cin>>q[n].x>>q[n].y>>q[n].v; if(q[n].x==0) {n--; Break;} A[Q[N].X][Q[N].Y]=q[n].v; } sort (Q+1, q+n+1, CNT); Q[n+1].x=m+1; q[n+1].y=m+1; q[n+1].v=0;//EndDfs0,0); cout<<ans<<Endl; return 0;}
If you look at that a and q a bit repetitive, you can try to directly on the Q DP, so you can save a.
Ps:code with operator why compile not past!! Solving!!
code1043 the number of squares