USCAO section 1.4 Number Triangles(DP)
2012-08-09 20:24
381 查看
NumberTriangles
Considerthenumbertriangleshownbelow.Writeaprogramthatcalculatesthehighestsumofnumbersthatcanbepassedonaroutethatstartsatthetopandendssomewhereonthebase.Eachstepcangoeitherdiagonallydowntotheleftordiagonallydowntotheright. [code]7 38 810 2744 45265
Inthesampleabove,theroutefrom7to3to8to7to5producesthehighestsum:30.
PROGRAMNAME:numtri
INPUTFORMAT
ThefirstlinecontainsR(1<=R<=1000),thenumberofrows.Eachsubsequentlinecontainstheintegersforthatparticularrowofthetriangle.Allthesuppliedintegersarenon-negativeandnolargerthan100.SAMPLEINPUT(filenumtri.in)
5 7 38 810 2744 45265
OUTPUTFORMAT
Asinglelinecontainingthelargestsumusingthetraversalspecified.SAMPLEOUTPUT(filenumtri.out)
30
[/code]
我是用DP做的,从底部往上推:状态方程f[i][j]=max(f[i+1][j],f[i+1][j+1])+map[i][j];
从顶部往下也行,就是麻烦点;
ID:nealgav1
PROG:numtri
LANG:C++
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#defineN1234
usingnamespacestd;
intf
;
intmap
;
voiddp(intm)
{
for(inti=1;i<=m;i++)//初始化
f[m][i]=map[m][i];
for(inti=m-1;i>=1;i--)
for(intj=1;j<=i;j++)
f[i][j]=max(f[i+1][j],f[i+1][j+1])+map[i][j];
}
intmain()
{
intm;
freopen("numtri.in","r",stdin);
freopen("numtri.out","w",stdout);
while(scanf("%d",&m)!=EOF)
{
for(inti=1;i<=m;i++)
for(intj=1;j<=i;j++)
scanf("%d",&map[i][j]);
dp(m);
printf("%d\n",f[1][1]);
}
}
[/code]
USER:NealGavinGavin[nealgav1]
TASK:numtri
LANG:C++
Compiling...
Compile:OK
Executing...
Test1:TESTOK[0.000secs,15240KB]
Test2:TESTOK[0.011secs,15240KB]
Test3:TESTOK[0.000secs,15240KB]
Test4:TESTOK[0.011secs,15240KB]
Test5:TESTOK[0.011secs,15240KB]
Test6:TESTOK[0.032secs,15240KB]
Test7:TESTOK[0.086secs,15240KB]
Test8:TESTOK[0.022secs,15240KB]
Test9:TESTOK[0.637secs,15240KB]
AlltestsOK.
YOURPROGRAM('numtri')WORKEDFIRSTTIME!That'sfantastic
--andararething.Pleaseacceptthesespecialautomated
congratulations.
NumberTriangles
RussCoxWekeeptrack(inthe"best"array)oftotalforthebestpathendinginagivencolumnofthetriangle.Viewingtheinput,apaththroughthetrianglealwaysgoesdownordownandtotheright.Toprocessanewrow,thebestpathtotalendingatagivencolumnisthemaximumofthebestpathtotalendingatthatcolumnortheonetoitsleft,plusthenumberinthenewrowatthatcolumn.Wekeeponlythebesttotalsforthecurrentrow(in"best")andthepreviousrow(in"oldbest").
[code]#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
#defineMAXR1000
int
max(inta,intb)
{
returna>b?a:b;
}
void
main(void)
{
intbest[MAXR],oldbest[MAXR];
inti,j,r,n,m;
FILE*fin,*fout;
fin=fopen("numtri.in","r");
assert(fin!=NULL);
fout=fopen("numtri.out","w");
assert(fout!=NULL);
fscanf(fin,"%d",&r);
for(i=0;i<MAXR;i++)
best[i]=0;
for(i=1;i<=r;i++){
memmove(oldbest,best,sizeofoldbest);
for(j=0;j<i;j++){
fscanf(fin,"%d",&n);
if(j==0)
best[j]=oldbest[j]+n;
else
best[j]=max(oldbest[j],oldbest[j-1])+n;
}
}
m=0;
for(i=0;i<r;i++)
if(best[i]>m)
m=best[i];
fprintf(fout,"%d\n",m);
exit(0);
}
[/code]
相关文章推荐
- USCAO section 1.4 Number Triangles(DP)
- USCAO-Section 1.4 Arithmetic Progressions
- USCAO section 1.4 The Clocks
- USCAO section 1.4 Arithmetic Progressions(枚举)
- USCAO section 1.4 Packing Rectangles
- USCAO section 1.4 The Clocks
- USCAO section 1.4 Arithmetic Progressions(枚举)
- USCAO section 1.4 Packing Rectangles
- USCAO section 2,2 Preface Numbering(算是DP吧)
- USCAO section 2,2 Preface Numbering(算是DP吧)
- USCAO section 4.1 Fence Loops(最短路,最小环,5级)
- USCAO section 1.2 Dual Palindromes
- USCAO-Section1.1 Broken Necklace(DP版)
- USCAO-Section 1.3 Prime Cryptarithm
- USCAO Section 1.1 Milking Cows
- USCAO section 4.1 Fence Loops(最短路,最小环,5级)
- USCAO section 1.2 Dual Palindromes
- USCAO Section 1.2 Palindromic Squares
- USCAO Section 1.1 Milking Cows
- USCAO Section 1.2 Dual Palindromes