POJ 3176-Cow Bowling (DP | memory-based search)
Cow Bowling
Time Limit:1000 MS |
|
Memory Limit:65536 K |
Total Submissions:14210 |
|
Accepted:9432 |
Description
The cows don't use actual bowling ballwhen they go bowling. they each take a number (in the range 0 .. 99), though, and line up in a standard bowling-pin-like triangle like this:
7 3 8 8 1 0 2 7 4 4 4 5 2 6 5
Then the other cows traverse the triangle starting from its tip and moving "down" to one of the two diagonally adjacent cows until the "bottom" row is reached. the cow's score is the sum of the numbers of the cows visited along the way. the cow with the highest score wins that frame.
Given a triangle with N (1 <= N <= 350) rows, determine the highest possible sum achievable.
Input
Line 1: A single integer, N
Lines 2. N + 1: Line I + 1 contains I space-separated integers that represent row I of the triangle.
Output
Line 1: The largest sum achievable using the traversal rules
Sample Input
573 88 1 02 7 4 44 5 2 6 5
Sample Output
30
Number triangle problem .. Dp [I] [j] = ma [I] [j] + max (dp [I + 1] [j], dp [I + 1] [j + 1])
Jushui .. I don't know what I did when I was confused about dp six months ago .. It's a sad story ..
#include #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long#define maxn 360#define pp pair
#define INF 0x3f3f3f3f#define max(x,y) ( ((x) > (y)) ? (x) : (y) )#define min(x,y) ( ((x) > (y)) ? (y) : (x) )using namespace std;int n,dp[maxn][maxn],ma[maxn][maxn];void solve(){for(int i=0;i
=0;i--)for(int j=0;j<=i;j++)dp[i][j]=max(ma[i][j]+dp[i+1][j],ma[i][j]+dp[i+1][j+1]);printf("%d\n",dp[0][0]);}int main(){while(~scanf("%d",&n)){for(int i=0;i
You can also perform a top-down Memory search .. Then the meaning of the status array is similar .. I personally think that searching is better to write...
#include #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long#define maxn 360#define pp pair
#define INF 0x3f3f3f3f#define max(x,y) ( ((x) > (y)) ? (x) : (y) )#define min(x,y) ( ((x) > (y)) ? (y) : (x) )using namespace std;int n,dp[maxn][maxn],ma[maxn][maxn];int dfs(int x,int y){if(x==n-1)return ma[x][y];if(dp[x][y]>=0)return dp[x][y];dp[x][y]=0;dp[x][y]+=(ma[x][y]+max(dfs(x+1,y),dfs(x+1,y+1)));return dp[x][y];}int main(){while(~scanf("%d",&n)){for(int i=0;i