USACO section 1.5.1 Number Triangles

本文探讨了利用动态规划解决数三角形路径优化问题,并提供了两种方法:一种是传统的动态规划方法,复杂度为O(n^2),另一种是使用广度优先搜索(BFS),但在实际应用中由于其复杂度较高而被动态规划方法取代。

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值