• 문제 링크
10164번: 격자상의 경로
입력의 첫째 줄에는 격자의 행의 수와 열의 수를 나타내는 두 정수 N과 M(1 ≤ N, M ≤ 15), 그리고 ○로 표시된 칸의 번호를 나타내는 정수 K(K=0 또는 1 < K < N×M)가 차례로 주어지며, 각 값은 공백으
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
static int[][] mat;
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read(), m = read(), k = read();
mat = new int[n][m];
fill(0, 0, 1);
if (k != 0) {
int y = (k - 1) / m, x = (k - 1) % m;
memoization(1, 1, y + 1, x + 1);
fill(y, x, mat[y][x]);
memoization(y + 1, x + 1, n, m);
} else memoization(1, 1, n, m);
bw.write(String.valueOf(mat[n - 1][m - 1]));
bw.flush();
}
private static void fill(int y, int x, int val) {
for (int i = x; i < mat[0].length; i++) mat[y][i] = val;
for (int i = y; i < mat.length; i++) mat[i][x] = val;
}
private static void memoization(int sy, int sx, int dy, int dx) {
for (int i = sy; i < dy; i++) for (int j = sx; j < dx; j++) mat[i][j] = mat[i - 1][j] + mat[i][j - 1];
}
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' 카테고리의 다른 글
[백준] 5705 Hexagonal Tiles - Dynamic Programming / Java (0) | 2023.12.03 |
---|---|
[백준] 6245 Cow Solitaire - Dynamic Programming / Java (0) | 2023.12.02 |
[백준] 22342 계산 로봇 - Dynamic Programming / Java (0) | 2023.11.30 |
[백준] 18323 Photoshoot - Brute Force / Java (0) | 2023.11.29 |
[백준] 27162 Yacht Dice - Brute Force / Java (0) | 2023.11.28 |
댓글