[백준] P11048

동민·2021년 3월 11일
0
import java.util.Scanner;

public class P11048 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), m = sc.nextInt();
		int[][] matrix = new int[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				matrix[i][j] = sc.nextInt();
			}
		}
		System.out.println(solution(matrix));
		sc.close();
	}

	private static int solution(int[][] matrix) {
		for (int i = matrix.length - 1; i >= 0; i--) {
			for (int j = matrix[0].length - 1; j >= 0; j--) {
				if(i == matrix.length - 1 && j == matrix[0].length - 1) continue;
				else if(i == matrix.length - 1 && j < matrix[0].length - 1) {
					matrix[i][j] += matrix[i][j+1];
				}else if(i < matrix.length - 1 && j == matrix[0].length - 1) {
					matrix[i][j] += matrix[i+1][j];
				}else if(i < matrix.length - 1 && j < matrix[0].length - 1){
					matrix[i][j] += Math.max(matrix[i + 1][j + 1], Math.max(matrix[i + 1][j], matrix[i][j + 1]));
				}
			}
		}
		return matrix[0][0];
	}
}
profile
BE Developer

0개의 댓글