USACO Number Triangles

1、数字三角形问题,有最优子结构性质,明显是DP。。。一次AC~

 

/*
ID:mrxy564
PROG:numtri
LANG:C++
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int a[1010][1010],d[1010][1010];
int dp(int i,int j){
    int &ans=d[i][j];
    if(ans>=0) return ans;
    ans=a[i][j];
    ans+=max(dp(i+1,j),dp(i+1,j+1));
    return ans;
}
int main(){
    freopen("numtri.in","r",stdin);
    freopen("numtri.out","w",stdout);
    int R;
    scanf("%d",&R);
    for(int i=0;i<R;i++)
      for(int j=0;j<i+1;j++){
          scanf("%d",&a[i][j]);
      }
    memset(d,-1,sizeof(d));
    for(int j=0;j<R;j++)
       d[R-1][j]=a[R-1][j];
    printf("%d\n",dp(0,0));
    return 0;
}
官方题解:

We keep track (in the "best" array) of total for the best path ending in a given column of the triangle. Viewing the input, a path through the triangle always goes down or down and to the right. To process a new row, the best path total ending at a given column is the maximum of the best path total ending at that column or the one to its left, plus the number in the new row at that column. We keep only the best totals for the current row (in "best") and the previous row (in "oldbest").

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXR 1000

int
max(int a, int b)
{
	return a > b ? a : b;
}

void
main(void)
{
	int best[MAXR], oldbest[MAXR];
	int i, 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, sizeof oldbest);
		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);
}

Or you can always solve from the bottom up, albeit at a cost of memory as Pablo Diaz's solution demonstrates:

#include <stdio.h>

int tri[1000][1000];
int res[1000];

int max(int a, int b) { return a > b ? a : b; }

main () {
  FILE *fin  = fopen ("numtri.in", "r");
  FILE *fout = fopen ("numtri.out", "w");
  int i, j, rows;

  // Store the input triangle 
  fscanf (fin, "%d", &rows);
  for (i = 0; i < rows; i++)
    for (j = 0; j <= i; j++)
      fscanf(fin,"%d",&tri[i][j]);

  // Copy the values from the last row
  for (i = 0; i < rows; i++)
    res[i] = tri[rows-1][i];

  // Traverse triangle from the bottom upwards,
  // storing the max paths between 'leaves'
  // and the current item.
  for (i = rows-1; i > 0; i--)
    for (j = 0; j <= i; j++)
      res[j] = tri[i-1][j] + max(res[j],res[j+1]);

  fprintf(fout,"%d\n",res[0]);

  exit (0);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值