poj 1163 三角形和

昨天的騰訊馬拉松複賽2013-03-31第三場做的不好,第一個簽到題跟這個題完全一樣的思路,也浪費了很多時間,哎。。。。

慢慢總結吧,多學習多積累


記憶化搜索的動態規劃,初始狀態在底部。。。

#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

///宏定義
const int  INF = 990000000;
const int MAXN = 100000;
const int maxn = 110;
///全局變量 和 函數
int max(int a, int b)
{
	return a > b ? a : b;
}


int T;
int N;
int d[maxn][maxn];
int sum[maxn][maxn];
bool vis[maxn][maxn];
int val(int y, int x)
{
	if(vis[y][x])
		return sum[y][x];
	vis[y][x] = true;
	int i, j;
	int maxm = -1;
	maxm = max(maxm, val(y + 1, x + 1));
	maxm = max(maxm, val(y + 1, x));
	return sum[y][x] = maxm + d[y][x];
}
int main()
{

	///變量定義
	int i, j, m;
//	scanf("%d", &T);
//	int cases = 1;
	while(scanf("%d", &N) != EOF)
	{
        for(i = 0; i < N; i++)
        {
            for(j = 0; j <= i; j++)
            {
                scanf("%d", &d[i][j]);
            }
        }
        memset(sum, 0, sizeof(sum));
        memset(vis, 0, sizeof(vis));

		//初始化最後一行
		for (i = 0; i < N; i++)
		{
			sum[N - 1][i] = d[N - 1][i];
			vis[N - 1][i] = true;
		}

		//記憶化搜索
		int ans = val(0, 0);
		printf("%d\n", ans);

	}

	///結束
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章