๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์•Œ๊ณ ๋ฆฌ์ฆ˜ PS] ๋ฐฑ์ค€ 14500๋ฒˆ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ์ž๋ฐ” ๋ฌธ์ œํ’€์ด

by Wonit 2021. 2. 15.

๋ฌธ์ œ

ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ๋ฐฑ์ค€์˜ 14500๋ฒˆ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ์˜ ์ ‘๊ทผ๊ณผ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•œ ๊ธ€ ์ž…๋‹ˆ๋‹ค.
์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•˜๋Š”์ง€๋ฅผ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณด์ž.

๋ฌธ์ œ ์ ‘๊ทผ

์ด๋ฒˆ ๋ฌธ์ œ๋„ ์™„์ „ ํƒ์ƒ‰์œผ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค.

์–ด๋–ป๊ฒŒ ๋‚˜๋Š” ์™„์ „ ํƒ์ƒ‰์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ์„๊นŒ??

์šฐ์„  ์ฃผ์–ด์ง„ ๋ฌธ์ œ์—์„œ ์›ํ•˜๋Š” ๋‹ต์„ ์ฐพ๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ ํ•˜๋‚˜๋ฅผ ๋†“์•„์„œ ์ตœ๋Œ€์˜ ํ•ฉ์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค.

 

๊ทธ๋Ÿผ ๋‹น์žฅ ์ƒ๊ฐ๋‚˜๋Š” ๊ฒƒ์€ n by m ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ํ•˜๋‚˜์”ฉ ๊ฒ€์‚ฌํ•˜๋ฉด์„œ ์ตœ๋Œ€ ํ•ฉ์„ ๊ตฌํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ ๋‹ค.


์ด์ œ ์—ฌ๊ธฐ์„œ ์ด ๋ฌธ์ œ๋ฅผ ์™„์ „ ํƒ์ƒ‰์œผ๋กœ ํ’€ ๊ฒƒ์ธ๊ฐ€, ์ด๋ถ„ ํƒ์ƒ‰์œผ๋กœ ํ’€ ๊ฒƒ์ธ๊ฐ€๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์™„ํƒ์ด๋‚˜ ์ด๋ถ„ ํƒ์ƒ‰(ํ˜น์€ ๋‹ค๋ฅธ ํƒ์ƒ‰ ๊ธฐ๋ฒ•)์„ ๊ตฌ๋ถ„์ง“๋Š” ๊ฒƒ์€ ๋ฐ”๋กœ ์‹œ๊ฐ„ ๋ณต์žก์„ฑ์˜ ํ†ต๊ณผ์ด๋‹ค.

 

์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋ณด๋ฉด ์šฐ๋ฆฌ๊ฐ€ ์—ฐ์‚ฐํ•ด์•ผํ•  ์ตœ์•…์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๊ฐ€ ๋‚˜์˜จ๋‹ค.

 

n * m ํ–‰๋ ฌ์—์„œ ๊ฐ ์›์†Œ๋งˆ๋‹ค ์–ด๋–ค ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๋ฅผ ๋†“์•˜๋Š”์ง€ ๊ฒ€์‚ฌ๋ฅผ ํ•œ๋‹ค๊ณ  ์น˜๋ฉด ์ตœ์•…์˜ ๊ฒฝ์šฐ 4 * 500์ธ 2000๋ฒˆ์˜ ๊ฒฝ์šฐ๊ฐ€ ๋‚˜๊ฒŒ ๋œ๋‹ค.

 

๊ทธ๋ฆฌ๊ณ  ๊ฐ๊ฐ์˜ 5๊ฐ€์ง€ ์ข…๋ฅ˜์˜ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๋ฅผ ๋ณด์ž.

 


์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 12๊ฐ€์ง€์˜ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๊ฐ€ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ๋Ÿผ ์•„๊นŒ ๊ตฌํ–ˆ๋˜ ํ–‰๋ ฌ์˜ ์ตœ์•…์˜ ๊ฒฝ์šฐ 2000๋ฒˆ๊ณผ 19๊ฐ€์ง€์˜ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ๊ฐ€ ๊ฐ๊ฐ๋“ค์–ด๊ฐˆ ๊ฒฝ์šฐ๋ฅผ ๊ณฑํ•œ๋‹ค๋ฉด ๋Œ€๋žต 456000๋ฒˆ์˜ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ–์•ผ ํ•œ๋‹ค๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋„์ถœ๋œ๋‹ค.

 

์šฐ๋ฆฌ๋Š” ์ฑ„์  ์„œ๋ฒ„๊ฐ€ ๋ณด์ˆ˜์ ์œผ๋กœ ๋ดค์„ ๋•Œ 1์ดˆ์— 1์–ต๋ฒˆ ์—ฐ์‚ฐ์„ ํ•œ๋‹ค๊ณ  ๊ฐ€์ •ํ–ˆ์œผ๋‹ˆ 24000๋ฒˆ์€ ๋งค์šฐ ์ž‘์€, ํ•œ ๋ฒˆ์— ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ๋Š” ์–‘์˜ ์—ฐ์‚ฐ์ธ ์…ˆ์ด๋‹ค.

 

๊ทธ๋ž˜์„œ ์ด ๋ฌธ์ œ๋ฅผ ๊ฒฐ๊ตญ ์™„์ „ ํƒ์ƒ‰์œผ๋กœ ๋„์ถœํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™๋‹ค๋Š” ๊ฒฐ๋ก ์ด ๋‚˜์™”๋‹ค.

์˜ค๋‹ต ํ›„๋ณด

์ด๋ฒˆ ๋ฌธ์ œ๋Š” block ์ขŒํ‘œ์— ๋Œ€ํ•ด์„œ ์‹ค์ˆ˜ํ•  ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์ด ๋งค์šฐ ๋งŽ์€ ๋ฌธ์ œ์˜€๋‹ค.


์ด์  ์œ ์˜ํ•ด์„œ ์ž˜ ํ’€์–ด๋ณด์ž.

์ •๋‹ต ์ฝ”๋“œ

import java.util.*;
public class Main {
    static int[][][] block = {
        {{0,1}, {0,2}, {0,3}},
        {{1,0}, {2,0}, {3,0}},
        {{1,0}, {1,1}, {1,2}},
        {{0,1}, {1,0}, {2,0}},
        {{0,1}, {0,2}, {1,2}},
        {{1,0}, {2,0}, {2,-1}},
        {{0,1}, {0,2}, {-1,2}},
        {{1,0}, {2,0}, {2,1}},
        {{0,1}, {0,2}, {1,0}},
        {{0,1}, {1,1}, {2,1}},
        {{0,1}, {1,0}, {1,1}},
        {{0,1}, {-1,1}, {-1,2}},
        {{1,0}, {1,1}, {2,1}},
        {{0,1}, {1,1}, {1,2}},
        {{1,0}, {1,-1}, {2,-1}},
        {{0,1}, {0,2}, {-1,1}},
        {{0,1}, {0,2}, {1,1}},
        {{1,0}, {2,0}, {1,1}},
        {{1,0}, {2,0}, {1,-1}},
    };
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[][] a = new int[n][m];
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                a[i][j] = sc.nextInt();
            }
        }
        int ans = 0;
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                for (int k=0; k<19; k++) {
                    boolean ok = true;
                    int sum = a[i][j];
                    for (int l=0; l<3; l++) {
                        int x = i+block[k][l][0];
                        int y = j+block[k][l][1];
                        if (0 <= x && x < n && 0 <= y && y < m) {
                            sum += a[x][y];
                        } else {
                            ok = false;
                            break;
                        }
                    }
                    if (ok && ans < sum) {
                        ans = sum;
                    }
                }
            }
        }
        System.out.println(ans);
    }
}

์ •๋‹ต ์†Œ์Šค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜์‹œ๋ ค๋ฉด solve url ์—์„œ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

๋Œ“๊ธ€