fork(1) download
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.StringTokenizer;
  5.  
  6. class BYTESM2 {
  7.  
  8. public static void main(String[] args) throws NumberFormatException, IOException {
  9. // TODO Auto-generated method stub
  10. int t = Integer.parseInt(br.readLine());
  11. StringBuilder sb = new StringBuilder();
  12. while (t-- > 0) {
  13. StringTokenizer st = new StringTokenizer(br.readLine());
  14. int h = Integer.parseInt(st.nextToken());
  15. int w = Integer.parseInt(st.nextToken());
  16. int array[][] = new int[h][w];
  17. //int answer[][] = new int[h][w];
  18. for (int i = 0; i < h; i++) {
  19. st = new StringTokenizer(br.readLine());
  20. for (int j = 0; j < w; j++) {
  21. array[i][j] = Integer.parseInt(st.nextToken());
  22. }
  23. }
  24. //for (int i = 0; i < w; i++) {
  25. //answer[0][i] = array[0][i];
  26. //}
  27. //br.readLine();
  28. for (int i = 1; i < h; i++) {
  29.  
  30. for (int j = 0; j < w; j++) {
  31. int a = 0, b = 0, c = 0;
  32. if (j >= 1)
  33. a = array[i - 1][j - 1];
  34. if (j + 1 < w)
  35. c = array[i - 1][j + 1];
  36. b = array[i - 1][j];
  37. array[i][j] += Math.max(a, Math.max(b, c));
  38. }
  39. }
  40. int max = Integer.MIN_VALUE;
  41. for (int i = 0; i < w; i++) {
  42. if (max < array[array.length - 1][i])
  43. max = array[array.length - 1][i];
  44. }
  45. sb.append(max + "\n");
  46. }
  47. System.out.println(sb);
  48. }
  49.  
  50. }
  51.  
  52.  
Success #stdin #stdout 0.07s 380160KB
stdin
3
6 5
3 1 7 4 2
2 1 3 1 1
1 2 2 1 8
2 2 1 5 3
2 1 4 4 4
5 2 7 5 1
1 5
2 5 3 6 9
5 1
2
3
4
5
6
stdout
32
9
20