Bzoj1668: [usaco 2006 Oct] fortune in the cow pie treasures pie

Source: Internet
Author: User
1668: [usaco Oct] cow pie treasures pie fortune time limit: 3 sec memory limit: 64 MB
Submit: 459 solved: 268
[Submit] [Status] Description

Recently, cows are keen to pack gold coins in flour and then bake them into pies. The I-th Pie contains Ni (1 <= Ni <= 25) gold coins, which are prominently marked on the pie surface. The cows arranged all the cooked pies on the grass into a matrix of column C (1 <= r <= 100) (1 <= C <= 100. You are standing on the side of the pie with the coordinates of (). Of course, you can get all the gold coins in the pie. You must go from the current position to the other side of the lawn and stop walking next to the pie in the coordinates of (R, c. Every time you move, you must move to a pie in the next column, and the number of rows cannot exceed 1 (that is, if you are currently standing at the coordinates of (R, c) on the pie side, next you can go to coordinates for (R-1, C + 1), (R, C + 1), or (R + 1, C + 1) ). When you pass by a pie, you can take all the gold coins in the pie. Of course, you will not be willing to lose the gold coins at your fingertips because you leave the lawn halfway, but you will have to stop next to the pie in the coordinates of (R, c. Now, you get a table marked with a pie matrix, each Pie contains a number of gold coins. So, according to the rules, how much gold coins can you get at most? For example, the cows arrange pies in the following matrix. The numbers in the matrix indicate the number of pies containing gold coins in the position:

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

The following are valid routes:

 

 

You can get 6 + 4 + 9 + 9 + 6 + 5 + 8 = 47 gold coins by following the above route. according to the rules, a maximum of 50 gold coins can be obtained in this matrix, as shown in the route:

 

 

 

 

Input

* Row 1st: two integers separated by spaces, R and C

* Row 2nd. R + 1: Each line contains C positive integers separated by spaces, indicating the number of gold coins in each pie from left to right

Output

* Row 1st: Output a positive integer, indicating the maximum number of gold coins you can collect

Sample input3 7
6 5 3 7 9 2 7
2 4 3 5 6 8 6
4 9 9 9 1 5 8

Sample output50
Hint Source

Gold

Question:

Sbdp

Code:

 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 50214 #define maxm 500+10015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 #define for0(i,n) for(int i=0;i<=(n);i++)19 #define for1(i,n) for(int i=1;i<=(n);i++)20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)21 using namespace std;22 inline int read()23 {24     int x=0,f=1;char ch=getchar();25     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}26     while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}27     return x*f;28 }29 int n,m,f[maxn][maxn],a[maxn][maxn];30 int main()31 {32     freopen("input.txt","r",stdin);33     freopen("output.txt","w",stdout);34     n=read();m=read();35     for1(i,n)36      for1(j,m)37       a[i][j]=read();38     f[1][1]=a[1][1];  39     for2(j,2,m)  40      for1(i,n)41       {42       f[i][j]=a[i][j]+max(f[i-1][j-1],max(f[i][j-1],f[i+1][j-1]));43       if(f[i][j]==a[i][j])f[i][j]=0;44       }45     printf("%d\n",f[n][m]);  46     return 0;47 }
View code

 

Bzoj1668: [usaco 2006 Oct] fortune in the cow pie treasures pie

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.