(记忆化搜索 )The Triangle--hdu --1163

http://poj.org/problem?id=1163

Description

73   88   1   02   7   4   44   5   2   6   5

(Figure 1)

Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers 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.

Input

Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.

Output

Your program is to write to standard output. The highest sum is written as an integer.

Sample Input

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

Sample Output

30

单纯的递归, 但是会超时
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

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

int a[N][N];

int DFS(int x, int y, int n)
{
    if(x>n || y>n)
        return 0;

    if(x==n && y==n)
        return a[x][y];

    return a[x][y]+ max(DFS(x+1,y, n), DFS(x+1, y+1, n));
}

int main()
{
    int n;

    while(scanf("%d", &n)!=EOF)
    {
        int i, j;

        memset(a, 0, sizeof(a));

        for(i=1; i<=n; i++)
        for(j=1; j<=i; j++)
            scanf("%d", &a[i][j]);

        printf("%d\n", DFS(1,1,n));
    }
    return 0;
}

用上记忆化搜索后, 不会超时了

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

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

int a[N][N], dp[N][N];

int  DFS(int x, int y, int n)
{
    if(x>n || y>n)
        return 0;

    if(dp[x][y]!=-1)
        return dp[x][y];
    else
    {
        if(x==n && y==n)
        return a[x][y];

        dp[x+1][y] = DFS(x+1, y, n);
        dp[x+1][y+1] = DFS(x+1, y+1, n);

        return a[x][y]+ max(dp[x+1][y], dp[x+1][y+1]);
    }
}

int main()
{
    int n;

    while(scanf("%d", &n)!=EOF)
    {
        int i, j;

        memset(a, 0, sizeof(a));
        memset(dp, -1, sizeof(dp));

        for(i=1; i<=n; i++)
        for(j=1; j<=i; j++)
            scanf("%d", &a[i][j]);

        printf("%d\n", DFS(1, 1, n));
    }
    return 0;
}
				
时间: 2024-12-08 13:59:59

(记忆化搜索 )The Triangle--hdu --1163的相关文章

C语言记忆化搜索___Play Game(Hdu 4597)

Play Game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 822    Accepted Submission(s): 474 Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N card

HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加字符的个数. 题解1(LCS): 很神奇的做法. 先求s和s的反串的LCS,也就是原串中已经满足回文性质的字符个数. 然后要变成回文串的话,只需要为剩下的每个落单的字符,相应地插入一个和它相同的字符即可. 所以答案是:s.size()-LCS(s,rev(s)) 另外,求LCS时只会用到lcs[i-

HDU 1142 A Walk Through the Forest (Dijkstra + 记忆化搜索 好题)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6350    Accepted Submission(s): 2332 Problem Description Jimmy experiences a lot of stress at work these days, especial

HDU 4597 Play Game (记忆化搜索)

题意:有两堆n张的卡片,每张卡片有一个得分,Alice和Bob轮流在两堆卡片的两端取卡片 问Alice先手,取得分数最多为多少: #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

HDU 1078 记忆化搜索

FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4575 Accepted Submission(s): 1829 Problem Description FatMouse has stored some cheese in a city. The city can be considered as a

HDU 1078 FatMouse and Cheese(记忆化搜索DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1078 题目大意:一个n*n的图,每个点都有奶酪,老鼠从(0,0)开始走,每次最多只能走k步就要停下来,停下的这个位置的奶酪数只能比上一个停留的位置大,并获取其奶酪,每次只能水平或垂直走,问最多能得到的奶酪. 解题思路:记忆化搜索,这方面还是写的太少,还要看别人才会,这个就当个例子参考吧. 1 #include<cstdio> 2 #include<cstring> 3 #include

hdu 1142(迪杰斯特拉+记忆化搜索)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7330    Accepted Submission(s): 2687 Problem Description Jimmy experiences a lot of stress at work these days, especiall

HDU 4597(记忆化搜索 dfs 参考)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4597 Problem Description Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from

HDU 4960 Another OCD Patient(记忆化搜索)

HDU 4960 Another OCD Patient 题目链接 记忆化搜索,由于每个碎片值都是正数,所以每个前缀和后缀都是递增的,就可以利用twopointer去找到每个相等的位置,然后下一个区间相当于一个子问题,用记忆化搜索即可,复杂度接近O(n^2) 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3f3f3f