본문 바로가기
Problem Solving/Baekjoon

[백준] 10164 격자상의 경로 - Dynamic Programming / Java

by graycode 2023. 12. 1.

 문제 링크

 

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;
    }

}

댓글