WOJ1026-Game On Checkerboard

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.

样例输入

1
3
1 3 5
7 0 8
4 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;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值