USACO section 1.5.1 Number Triangles

1. 动态规划,f[i,j]=Max{f[i+1,j],f[i+1,j+1]}+a[i,j] (1<=i<=n-1,1<=j<=i)复杂度是o(n^2)。

2. 以下是代码:

/*
ID: dollar4
PROG: numtri
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cstring>

using namespace std;
long long num[1010][1010];
long max(long a, long b)
{
    if (a > b)
        return a;
    else return b;
}
int main()
{
    ofstream fout ("numtri.out");
    ifstream fin ("numtri.in");

    int n, i, j;
    fin >> n;
    for (i = 0; i < n; i++)
        for (j = 0; j <= i; j++)
            fin >> num[i][j];
    for (i = n - 2; i >= 0; i--)
        for (j = 0; j <= i; j++)
            num[i][j] += max(num[i+1][j], num[i+1][j+1]);
    fout << num[0][0] << endl;
    return 0;
}


3. 第一次用了BFS做,果断超时,下边是BFS代码:

/*
ID: dollar4
PROG: numtri
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;
int num[1010][1010], ans[1010][1010];
int n;
struct Node
{
    int x, y;
} node;
queue<Node> q;
void bfs(Node nod)
{
    q.push(nod);
    Node next1, next2, temp;
    while (!q.empty())
    {
        temp = q.front();
        q.pop();
        if (temp.y + 1 < n)
        {
            next1.x = temp.x + 1;
            next1.y = temp.y;
            next2.x = temp.x + 1;
            next2.y = temp.y + 1;
            q.push(next1);
            q.push(next2);
            if (ans[next1.x][next1.y] < ans[temp.x][temp.y] + num[next1.x][next1.y])
                ans[next1.x][next1.y] = ans[temp.x][temp.y] + num[next1.x][next1.y];
            if (ans[next2.x][next2.y] = ans[temp.x][temp.y] + num[next2.x][next2.y])
                ans[next2.x][next2.y] = ans[temp.x][temp.y] + num[next2.x][next2.y];
        }
        else break;
    }
    return;
}

int main()
{
    ofstream fout ("numtri.out");
    ifstream fin ("numtri.in");
    memset(num, 0, sizeof(num));
    int i, j;
    fin >> n;
    for (i = 0; i < n; i++)
        for (j = 0; j <= i; j++)
            fin >> num[i][j];
    Node start;
    start.x = 0;
    start.y = 0;
    ans[0][0] = num[0][0];
    bfs(start);
    for (i = 0; i < n; i++)
    {
        for (j = 0; j <= i; j++)
            cout << ans[i][j] << ' ';
        cout << endl;
    }


    int output = ans[0][0];
    for (i = 0; i < n; i++)
        for (j = 0; j <= i; j++)
        {
            if (output < ans[i][j])
                output = ans[i][j];
        }
    fout << output << endl;
    return 0;
}

4. 官方参考代码

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


原文地址:https://www.cnblogs.com/dollarzhaole/p/3188911.html