閱讀708 返回首頁    go 技術社區[雲棲]


[usaco] Number Triangles

Number Triangles

Consider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be 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.

          7

        3   8

      8   1   0

    2   7   4   4

  4   5   2   6   5

In the sample above, the route from 7 to 3 to 8 to 7 to 5 produces the highest sum: 30.

PROGRAM NAME: numtri

INPUT FORMAT

The first line contains R (1 <= R <= 1000), the number of rows. Each subsequent line contains the integers for that particular row of the triangle. All the supplied integers are non-negative and no larger than 100.

SAMPLE INPUT (file numtri.in)

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

OUTPUT FORMAT

A single line containing the largest sum using the traversal specified.

SAMPLE OUTPUT (file numtri.out)

30

 

解體的要點在於,把數據轉化成二叉樹。然後自底向上計算每個節點的高度。但是注意,由於每個節點同時作為兩個節點的子節點(一個是左子節點,一個是右子節點)。

這樣可能會造成每個節點計算兩次,於是設計每個幾點的高度初始值為-1. 當判斷高度不會-1時,就不必在計算該節點的高度了。之所以初始值不為0,是因為某個test case全為0.

這樣還是會造成重複的遞歸。

我的解法:

/*
ID: yunleis2
PROG: numtri
LANG: C++
*/

#include<iostream>
#include<fstream>
#include<queue>
using namespace std;

#define max(a,b) (a>b?a:b)

class node
{
public:
 node * left;
 node * right;
 int value;
 int length;
 node(node * l,node * r)
 {
  left=l;
  right=r;
 }
 node ()
 {left=NULL;right=NULL;value=0;length=-1;}

};
int  search( node *root);
void print(node *);
int main()
{
 fstream fin("numtri.in",ios::in);
 int line;
 fin>>line;
 int size=(line+1)*line/2;
 node * root=new node();

 queue<node *> q;
 fin>>root->value;
 q.push(root);
 for(int i=1;i<line;i++)
 {
  node * last=NULL;
  for(int j=1;j<=i;j++)
  {
   node * ptr=q.front();
   q.pop();
   if(j==1)
   {
    ptr->left=new node();
    fin>>ptr->left->value;
    q.push(ptr->left);
   }
   else
   {
    ptr->left=last->right;
   }
   ptr->right=new node();
   fin>>(ptr->right)->value;
   last=ptr;
   
   q.push(ptr->right);
  }
 }
 while(!q.empty())
  q.pop();
 //print(root);
 int length=search(root);
 fstream fout("numtri.out",ios::out);
 fout<<root->length<<endl;
 //system("pause");
 
}
void print(node * root)
{
 if(root!=NULL)
 {
  cout<<root->value;
  print(root->left);
  print(root->right);
 }
}
int  search( node *root)
{
 if(root==NULL)
  return 0;
 if(root->length!=-1)
  return root->length;
 root->length=max(search(root->right),search(root->left))+root->value;
 return root->length;
}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

usaco的解法 Number Triangles
Russ Cox

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);
}

 

 

 

最後更新:2017-04-02 06:51:48

  上一篇:go Android UI學習 - Menu菜單
  下一篇:go Intent的setFlags()方法