• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 62  文章 - 96  trackbacks - 0
            <2006年4月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            常用鏈接

            留言簿(7)

            隨筆分類(66)

            隨筆檔案(62)

            文章分類(31)

            文章檔案(32)

            友情鏈接

            最新隨筆

            積分與排名

            • 積分 - 236540
            • 排名 - 108

            最新評論

            閱讀排行榜

            評論排行榜

            動態規劃即是一個重點,又是一個難點。
            今天終于做出了一題像樣的動態規劃題。
            Problem Id:1163??User Id:beyonlin_SCUT
            Memory:100K??Time:0MS
            Language:C++??Result:Accepted
            http://acm.pku.edu.cn/JudgeOnline/problem?id=1163

            The Triangle
            Time Limit:1000MS? Memory Limit:10000K
            Total Submit:3415 Accepted:1988

            Description

            7
            
            3 8
            8 1 0
            2 7 4 4
            4 5 2 6 5

            (Figure 1)


            Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.

            Input
            Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.

            Output
            Your program is to write to standard output. The highest sum is written as an integer.

            Sample Input

            5
            7
            3 8
            8 1 0 
            2 7 4 4
            4 5 2 6 5

            Sample Output
            30


            分析:
            題意簡化為:
            從第一行開始走到最后一行,每步可以向下走或右下走。
            所求即為從第一行走到最后一行經過的數總和的最大值。
            令p[][]存儲input。
            5
            7
            3 8
            8 1 0
            2 7 4 4
            |?????\ |? \
            4 5 2 6 5

            如上圖,令i為行,j為列,
            d[i][j]為從第一行走到第i行第j列的最大值。
            對于(i,j)這個點,它可以從不同方向走來,如圖' | '代表從上方走來,' \ '代表從左上方走來。

            則動態規則方程為:
            ???????????????? ?{?????d[i-1][1]+p[i][1]???(j=1)
            d[i][j]=Max{???? Max( d[i-1][j-1] , d[i-1][j] ) + p[i][j]???(1<j<i)
            ????????????????? {???? d[i-1][i-1]+p[i][i]???(j=i)

            結果為Max(d[n][j]) , (1<=j<=n)

            代碼如下:

            #include<cstdio>
            int p[100][100];
            int d[100][100];
            int Max(int a,int b)
            {return a>b?a:b;}
            int main()
            {
            	int i,n;
            	scanf("%d",&n);
            	for(i=1;i<=n;i++)
            	{
            		int j;
            		for(j=1;j<=i;j++)
            			scanf("%d",p[i]+j);
            	}
            	d[1][1]=p[1][1];
            	for(i=2;i<=n;i++)
            	{
            		int j;
            		d[i][1]=d[i-1][1]+p[i][1];
            		for(j=2;j<=i;j++)
            			d[i][j]=Max(d[i-1][j-1],d[i-1][j])+p[i][j];
            		d[i][i]=d[i-1][i-1]+p[i][i];
            	}
            	int max=0;
            	for(i=1;i<=n;i++)
            	{
            		if(d[n][i]>max)
            			max=d[n][i];
            	}
            	printf("%d\n",max);
            	return 0;
            }
            

            posted on 2006-08-28 10:31 beyonlin 閱讀(606) 評論(2)  編輯 收藏 引用 所屬分類: acm之路

            FeedBack:
            # re: 我的動態規劃啟蒙題 2006-08-28 16:02 
            嘿嘿, 這也是我的第一題動態規劃野~~~~  回復  更多評論
              
            # re: 我的動態規劃啟蒙題 2008-10-28 14:43 東·德
            祝賀!我也剛剛看懂,但是加上“一條路徑”的輸出就更好了。是吧  回復  更多評論
              
            亚洲国产香蕉人人爽成AV片久久| 久久午夜伦鲁片免费无码| 久久影院午夜理论片无码| 久久亚洲精品无码aⅴ大香| 久久精品国产精品亚洲毛片| 久久精品成人免费观看97| 久久人人爽人人爽人人爽| 久久香蕉国产线看观看乱码| 亚洲国产成人久久一区WWW| av无码久久久久不卡免费网站| 久久久久亚洲AV无码去区首| 精品久久久久久无码专区| 久久这里只有精品首页| 88久久精品无码一区二区毛片| 亚洲精品国产字幕久久不卡| 欧美国产精品久久高清| 精品久久久久久国产三级| 久久精品人人做人人爽电影蜜月| 欧美亚洲国产精品久久高清| 国产亚洲美女精品久久久| 国产精品久久一区二区三区| 亚洲精品无码久久久久| 久久大香萑太香蕉av| 人妻无码久久精品| 久久se精品一区二区影院 | 亚洲中文字幕无码久久2020| 久久久久97国产精华液好用吗| 亚洲精品高清久久| 久久九九亚洲精品| 久久香蕉综合色一综合色88| 国产精品久久99| 青青青国产成人久久111网站| 久久人人妻人人爽人人爽| 中文字幕久久精品无码| 99精品国产综合久久久久五月天 | 国内精品久久久久久99| 久久精品免费一区二区| 久久亚洲私人国产精品| 久久久国产乱子伦精品作者| a级成人毛片久久| 久久综合狠狠色综合伊人|