WOJ1026-Game On Checkerboard

来源:互联网 发布:淘宝新店铺如何运营 编辑:程序博客网 时间:2024/06/06 16:49

Suppose that you are given an <img src="../upload/image/1026/g1.gif" /> checkerboard and a checker. Let?s have a game on the checkerboard right now. Here, you shouldmove the checker from the first row of the board to the last row of the board according to the following rule. At each step you may move the 

checker to one of three squares:
1. the square immediately below.
2. the square that is one below and one to the left(but only if the checker is not already in the leftmost column).
3. the square that is one below and one to the right(but only if the checker is not already in the rightmost column).
Consider each square of the checkerboard is placed p(i, j) dollars, where i is the row number and j is the column number of the square.
You can move the checker from any square of the first row to any square of the last row, and pick up the dollars placed in the square you have
passed. What is the maximum number of dollars you can gather along the way?

输入格式

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of

test cases.
For each test case, the first line contains an integer N (1 <= N <= 1000), which denotes the checkerboard dimension. The following N lines contain N non-negative integers each, separated by a single blank space. The j-th element of the i-th row denotes the number of dollars
placed in the square of i-th row, j-th column. The first row is Row 1 and the last row is Row n.

输出格式

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from

  1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
    For each test case, output a single line containing an integer which is the maximum number of dollars you can gather along the way.

样例输入

131 3 57 0 84 0 0

样例输出

Case 1:14

#include<iostream>#include<cstdio>#include<cstring>using namespace std;int board[1010][1010],dp[1010][1010];int max3(int a,int b,int c){int max=a>b?a:b;max=max>c?max:c;return max;}int main(){int t,ti,n,i,j,ans=0;cin>>t;for(ti=1;ti<=t;ti++){cin>>n;memset(dp,0,sizeof(dp));for(i=1;i<=n;i++)for(j=1;j<=n;j++)cin>>board[i][j];for(j=1;j<=n;j++)dp[1][j]=board[1][j];for(i=2;i<=n;i++)for(j=1;j<=n;j++)dp[i][j]=max3(dp[i-1][j-1],dp[i-1][j],dp[i-1][j+1])+board[i][j];for(j=1;j<=n;j++)if(ans<dp[n][j])ans=dp[n][j]; if(ti!=1)puts("");printf("Case %d:\n",ti);cout<<ans<<endl;}return 0;}