1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅ‡ Gold 5


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/17498


image

image


๐Ÿ“Œ ํ’€์ด


๐Ÿ“Œ Code

package BOJ.DP;

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

public class N17498 {
    static class tile {
        int value;
        int dp;

        public tile(int value) {
            this.value = value;
            this.dp = -2100000000;
        }

        void MakeDP(tile dp) {
            this.dp = Math.max(this.dp, dp.dp + this.value * dp.value);
        }
    }

    static int N;
    static int M;
    static int D;
    static tile[][] field;

    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());
        D = Integer.parseInt(st.nextToken());

        field = new tile[N][M];
        for (int i = 0; i < N; i++) {
            st = new StringTokenizer(br.readLine());
            for (int j = 0; j < M; j++) {
                field[i][j] = new tile(Integer.parseInt(st.nextToken()));
            }
        }
        FillField();
        int max = -2100000000;
        for (int i = 0; i < M; i++) {
            max = Math.max(field[N - 1][i].dp, max);
        }
        //ShowAll();
        System.out.println(max);
    }

    static void FillField() {
        for (int i = 0; i < M; i++) {
            field[0][i].dp = 0;
        }

        for (int i = 0; i < N - 1; i++) {
            for (int j = 0; j < M; j++) {
                for (int k = 1; k <= D; k++) {
                    for (int l = -D + k; l <= D - k; l++) {
                        if (i + k >= N || j + l >= M || j + l < 0) {
                            continue;
                        }
                        field[i + k][j + l].MakeDP(field[i][j]);
                    }
                }
            }
        }
    }

    static void ShowAll() {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                System.out.print(field[i][j].dp + " ");
            }
            System.out.println();
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.


๐Ÿ“Œ ์ถ”๊ฐ€ ์˜ˆ์ œ

[Input]
4 3 1
3 -5 4
2 0 0
1 -3 1
-2 9 1

[Output]
6



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜