fork download
  1. import java.util.LinkedList;
  2. import java.util.Queue;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Main {
  7. static int map[][];
  8. static int N;
  9. static int M;
  10. static int cnt;
  11. public static void main(String[] args) {
  12. Scanner sc = new Scanner(System.in);
  13.  
  14. N = sc.nextInt();
  15. M = sc.nextInt();
  16. map = new int[N][M];
  17. for(int i =0 ; i<N; i++) {
  18. for(int j=0; j<M; j++) {
  19. map[i][j]=sc.nextInt();
  20. }
  21. }
  22. cnt=0;
  23. bfs(0,0);
  24. System.out.println(cnt);
  25.  
  26. }
  27. static int[][] dir = {{1,0},{0,1},{1,1}};
  28. private static void bfs(int x,int y) {
  29. Queue<Pair> q = new LinkedList<>();
  30. q.add(new Pair(x,y));
  31. int max[][]= new int[N][M];
  32. max[0][0]=map[0][0];
  33. while(!q.isEmpty()) {
  34. Pair p = q.poll();
  35. for(int i=0;i<3;i++) {
  36. int tx = p.x+dir[i][0];
  37. int ty = p.y+dir[i][1];
  38. if(tx<0 || ty<0 || tx>=N || ty>=M) continue;
  39. if(map[tx][ty]+max[p.x][p.y]>max[tx][ty]) {
  40. max[tx][ty]=map[tx][ty]+max[p.x][p.y];
  41. q.add(new Pair(tx, ty));
  42. }
  43. }
  44. }
  45. cnt = max[N-1][M-1];
  46.  
  47. }
  48. static class Pair{
  49. private int y,x;
  50.  
  51. Pair(int x, int y){
  52. this.y = y;
  53. this.x = x;
  54. }
  55. }
  56. }
  57.  
Success #stdin #stdout 0.11s 35360KB
stdin
3 3
0 0 0
0 0 0
0 0 1
stdout
0