您的位置:首页 > 其它

南邮 OJ 1998 GRID

2015-08-08 09:55 405 查看


GRID

时间限制(普通/Java) : 2500 MS/ 7500 MS 运行内存限制 : 65536 KByte

总提交 : 105 测试通过 : 28

比赛描述

I think most ACMers are familiar with grid map.

In a grid map. Each grid has its own value vij.

At the beginning you are on (0,0) and you have to go to (M,N)

You have two ways to move (0,1) and (1,0)

(0,1) means when you stand on(x,y) you move to (x,y+1)

(1,0) means when you stand on(x,y) you move to (x+1,y)

Now you have to find a way from (0,0) to (M,N) to make the combined value of all the grid you have passed is the largest.

Output the maximum combined value.



输入

In the first line there is an integer T. means there are T cases.

In the first line of each case there are 2 integers M,N<1000.

On the following M line each line has N integers;

The yth number on the ith line is vij <5000, which represents the value of grid (i,j).

输出

For each case you only to print an integer in a single line. The integer is the maximum combined value of the grids you
have passed.

样例输入

1

1 1

1

样例输出

1

提示

null

题目来源

3E

/* Wrong Answer at Test 1
#include<iostream>
using namespace std;

#define MAX_N 1001
int a[MAX_N][MAX_N];

int main(){
int t,n,m,i,j,sum;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
for(sum=1;sum<=n+m;sum++){
for(i=max(1,sum-m);i<=n && sum-i>=1;i++){
j = sum-i;
a[i][j] += max(a[i-1][j],a[i][j-1]);
}
}
printf("%d\n",a
[m]);
}
}
*/

/* AC 828MS
#include<iostream>
using namespace std;

#define MAX_N 1001
int a[MAX_N][MAX_N];

int main(){
int t,n,m,i,j,sum;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
for(i=2;i<=n;i++){
a[i][1] += a[i-1][1];
}
for(j=2;j<=n;j++){
a[1][j] += a[1][j-1];
}
for(sum=2;sum<=n+m;sum++){
for(i=max(2,sum-m);i<=n && sum-i>=2;i++){
j = sum-i;
a[i][j] += max(a[i-1][j],a[i][j-1]);
}
}
printf("%d\n",a
[m]);
}
}
*/

// 234MS
#include<iostream>
using namespace std;

#define MAX_N 1001
int a[MAX_N][MAX_N];

int getnum(){
int t,r;
bool flag=0;
while((t=getchar())<'0' || t>'9'){
if(t=='-'){
flag = 1;
}
}
r = t-'0';
while((t=getchar())>='0' && t<='9'){
r = r*10 + t-'0';
}
if(flag){
r = -r;
}
return r;
}

int main(){
int t,n,m,i,j,sum;
//	scanf("%d",&t);
t = getnum();
while(t--){
//		scanf("%d%d",&n,&m);
n = getnum();
m = getnum();
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
//				scanf("%d",&a[i][j]);
a[i][j] = getnum();
}
}
for(i=2;i<=n;i++){
a[i][1] += a[i-1][1];
}
for(j=2;j<=n;j++){
a[1][j] += a[1][j-1];
}
for(sum=2;sum<=n+m;sum++){
for(i=max(2,sum-m);i<=n && sum-i>=2;i++){
j = sum-i;
a[i][j] += max(a[i-1][j],a[i][j-1]);
}
}
printf("%d\n",a
[m]);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: