#include <iostream>
#include <algorithm>
using namespace std;
const int N = 100;
int tiles[N][N];
int sums[N][N];
int r,c;
int totalsum;
void computeMax(int i, int j) {
int s1, s2, s3;
if (j == 0) {
s1 = -1;
} else {
s1 = sums[i-1][j-1] + tiles[i][j];
}
s2 = sums[i-1][j] + tiles[i][j];
if (j == (c-1)) {
s3 = -1;
} else {
s3 = sums[i-1][j+1] + tiles[i][j];
}
sums[i][j] = max(max(s1,s2),s3);
if (sums[i][j] > totalsum) {
totalsum = sums[i][j];
}
}
void doCompute() {
totalsum = 0;
cin >> r >> c;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> tiles[i][j];
}
}
for (int i = 0; i < c; i++) {
sums[0][i] = tiles[0][i];
}
for (int i = 1; i < r; i++) {
for (int j = 0; j < c; j++) {
computeMax(i,j);
}
}
cout << totalsum << endl;
}
int main() {
int t;
cin >> t;
while(t-->0) {
doCompute();
}
}
SPOJ BYTESM2
最新推荐文章于 2019-03-07 07:53:00 发布