• 문제 링크
5991번: Papaya Jungle
Bessie has wandered off the farm into the adjoining farmer's land. He raises delicious papaya fruit, which is a delicacy for cows. The papaya jungle is partitioned into a grid of squares with R rows and C columns (1 <= R <= 40, 1 <= C <= 40), as is popular
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
static int r, c, cnt;
static int[][] mat, dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
mat = new int[r = read()][c = read()];
for (int i = 0; i < r; i++) for (int j = 0; j < c; j++) mat[i][j] = read();
dfs(0, 0);
bw.write(String.valueOf(cnt));
bw.flush();
}
private static void dfs(int y, int x) {
cnt += mat[y][x];
mat[y][x] = -1;
if (y == r - 1 && x == c - 1) return;
int ny = 0, nx = 0;
for (int[] d : dir) {
int ty = y + d[0], tx = x + d[1];
if (ty < 0 || ty >= r || tx < 0 || tx >= c || mat[ty][tx] == -1) continue;
if (mat[ny][nx] < mat[ty][tx]) {
ny = ty;
nx = tx;
}
}
dfs(ny, nx);
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 10263 Opening Ceremony - Greedy / Java (0) | 2024.01.12 |
---|---|
[백준] 10774 저지 - Greedy / Java (0) | 2024.01.11 |
[백준] 26999 Satellite Photographs - Graph Theory / Java (0) | 2024.01.09 |
[백준] 6018 Tea Time - Graph Theory / Java (0) | 2024.01.08 |
[백준] 6188 Clear Cold Water - Graph Theory / Java (0) | 2024.01.07 |
댓글