code1043 the number of squares

Source: Internet
Author: User

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&GT;=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

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.