Notice
Recent Posts
Recent Comments
Link
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 11048. 이동하기 본문

acmicpc

[BAEKJOON] 11048. 이동하기

코드와이 2021. 3. 24. 15:11

 

문제링크

www.acmicpc.net/problem/11048

 

11048번: 이동하기

준규는 N×M 크기의 미로에 갇혀있다. 미로는 1×1크기의 방으로 나누어져 있고, 각 방에는 사탕이 놓여져 있다. 미로의 가장 왼쪽 윗 방은 (1, 1)이고, 가장 오른쪽 아랫 방은 (N, M)이다. 준규는

www.acmicpc.net

 

package acmicpc.Silver1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class 이동하기 {

	static int n, m;
	static int dp[][];
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		
		dp = new int[n][m];
		
		for(int i = 0 ; i < n ; i++) {
			st = new StringTokenizer(br.readLine());
			for(int j = 0 ; j < m ; j++) {
				int x = Integer.parseInt(st.nextToken());
				
				dp[i][j] = x;
				if(i == 0 && j == 0) continue;
				
				if(i == 0) dp[i][j] += dp[i][j-1];
				else if(j == 0) dp[i][j] += dp[i-1][j];
				else dp[i][j] += Math.max(Math.max(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]);
			}
		}
		System.out.println(dp[n-1][m-1]);
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 1600. 말이 되고픈 원숭이  (0) 2021.03.24
[BAEKJOON] 2096. 내려가기  (0) 2021.03.24
[BAEKJOON] 2839. 설탕 배달(DP)  (0) 2021.03.23
[BAEKJOON] 1753. 최단경로  (0) 2021.03.23
[BAEKJOON] 1149. RGB 거리  (0) 2021.03.23