본문 바로가기
Problem Solving/Baekjoon

[백준] 14767 Flow Shop - Dynamic Programming / Java

by graycode 2024. 2. 20.

 문제 링크

 

14767번: Flow Shop

There is only one test case in each file. It begins with a single line containing N and M (1 ≤ N, M ≤ 1000), the number of swathers and stages (respectively). Following this are N lines, each with M integers. The j’th integer of the i’th line is Pi

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringBuilder sb = new StringBuilder();

        int n = read(), m = read();
        int[][] cache = new int[n + 1][m + 1];

        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++) cache[i][j] = read();

        for (int i = 1; i <= n; sb.append(cache[i++][m]).append(" "))
            for (int j = 1; j <= m; j++) cache[i][j] += Math.max(cache[i - 1][j], cache[i][j - 1]);

        bw.write(sb.toString());
        bw.flush();
    }

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

}

댓글