这是代码。它适用于单行,单列以及整个零行或列测试用例。
import java.util.Scanner;
class SpojBytesMTwo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.nextLine());
while(t-->0) {
String s[] = (sc.nextLine()).split(" ");
int r=Integer.parseInt(s[0]);
int c = Integer.parseInt(s[1]);
int [][]arr = new int[r][c];
for (int i = 0; i < r; i++) {
String s1[] = (sc.nextLine()).split(" ");
for (int j = 0; j < c; j++) {
arr[i][j]= Integer.parseInt(s1[j]);
}
}
//populated the array above
if(!(r==1 || c==1)) {
// handle single row or column case separately
for (int i = r-2; i>=0; i--) {
for (int j = 0; j < c; j++) {
if(j==0) {
arr[i][j] += Math.max(arr[i+1][j],arr[i+1][j+1]);
}
else if(j==c-1) {
arr[i][j] += Math.max(arr[i+1][j-1],arr[i+1][j]);
}
else {
arr[i][j] += Math.max(arr[i+1][j-1],Math.max(arr[i+1][j],arr[i+1][j+1]));
}
}
}
}
int big = arr[0][0];
//calculating the biggest sum
//checking the first row for this
if(c>1) {
for (int j = 1; j < c; j++) {
if(arr[0][j]>big) {
big = arr[0][j];
}
}
}
else {
for(int i=1;i<r;i++)
big += arr[i][0];
}
System.out.println(big);
}
}
}
有人可以建议其他可能给我错误答案的测试用例吗?