๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 14500๋ฒˆ - ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ

by dkswnkk 2022. 9. 18.

๋ฌธ์ œ

 

14500๋ฒˆ: ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ

ํด๋ฆฌ์˜ค๋ฏธ๋…ธ๋ž€ ํฌ๊ธฐ๊ฐ€ 1×1์ธ ์ •์‚ฌ๊ฐํ˜•์„ ์—ฌ๋Ÿฌ ๊ฐœ ์ด์–ด์„œ ๋ถ™์ธ ๋„ํ˜•์ด๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•ด์•ผ ํ•œ๋‹ค. ์ •์‚ฌ๊ฐํ˜•์€ ์„œ๋กœ ๊ฒน์น˜๋ฉด ์•ˆ ๋œ๋‹ค. ๋„ํ˜•์€ ๋ชจ๋‘ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ •์‚ฌ๊ฐํ˜•์˜ ๋ณ€

www.acmicpc.net

 

์ฝ”๋“œ

#include <iostream>
using namespace std;

int map[501][501];
int N, M;
/*
    ์ด 19๊ฐœ์˜ ๋ชจ์–‘ ๊ฐ€๋Šฅ
 */

int dr1[2][3] = {{1, 2, 3}, {0, 0, 0}};
int dc1[2][3] = {{0, 0 ,0}, {1, 2, 3}};
int dr2[3] = {0, 1, 1};
int dc2[3] = {1, 0, 1};
int dr3[8][3] = {{1, 2, 2}, {0, 0, 1}, {0, 1, 2}, {1, 1, 1}, {0, -1, -2}, {0, 0, 1}, {0, 1, 2}, {0, 0, -1}};
int dc3[8][3] = {{0, 0, 1}, {1, 2, 0}, {1, 1, 1}, {0, 1, 2}, {1, 1, 1}, {1, 2, 2 }, {1,0, 0}, {1, 2, 2}};
int dr4[4][3] = {{1, 1, 2}, {0, -1, -1}, {0, -1, 1}, {0, 1, 1}};
int dc4[4][3] = {{0, 1, 1}, {1, 1, 2}, {1, 1, 0}, {1, 1, 2}};
int dr5[4][3] = {{0, 1, 0}, {0, -1, 1}, {0, -1,0}, {-1, 1, 0}};
int dc5[4][3] = {{1, 1, 2}, {1, 1, 1}, {1, 1, 2}, {0, 0, 1}};

int max_v = -1;
void go(){
    for(int i=0; i<N; i++){
        for(int k=0; k<M; k++){
            int r = i;
            int c = k;
            int cnt = 1;
            int temp = map[r][c];
            
            for(int a=0; a<2; a++){ // ์ฒซ๋ฒˆ์งธ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ
                for(int b=0; b<3; b++){
                    int nr = r + dr1[a][b];
                    int nc = c + dc1[a][b];
                    if(nr>=0 && nr<N && nc>=0 && nc<M){
                        temp += map[nr][nc];
                        cnt++;
                    }
                }
                if(cnt == 4) max_v = max(max_v, temp);
                cnt = 1;
                temp = map[r][c];
            }
            
            for(int a=0; a<3; a++){ // ๋‘๋ฒˆ์งธ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ
                int nr = r + dr2[a];
                int nc = c + dc2[a];
                if(nr>=0 && nr<N && nc>=0 && nc<M){
                    temp += map[nr][nc];
                    cnt++;
                }
                
                if(cnt == 4) max_v = max(max_v, temp);
            }
            
            cnt = 1;
            temp = map[r][c];
            
            for(int a=0; a<8; a++){ // ์„ธ๋ฒˆ์งธ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ
                for(int b=0; b<3; b++){
                    int nr = r + dr3[a][b];
                    int nc = c + dc3[a][b];
                    if(nr>=0 && nr<N && nc>=0 && nc<M){
                        temp += map[nr][nc];
                        cnt++;
                    }
                }
                if(cnt == 4) max_v = max(max_v, temp);
                cnt = 1;
                temp = map[r][c];
            }
            for(int a=0; a<4; a++){ // ๋„ค๋ฒˆ์งธ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ
                for(int b=0; b<3; b++){
                    int nr = r + dr4[a][b];
                    int nc = c + dc4[a][b];
                    if(nr>=0 && nr<N && nc>=0 && nc<M){
                        temp += map[nr][nc];
                        cnt++;
                    }
                }
                if(cnt == 4) max_v = max(max_v, temp);
                cnt = 1;
                temp = map[r][c];
            }
            for(int a=0; a<4; a++){ // ๋‹ค์„ฏ๋ฒˆ์งธ ํ…ŒํŠธ๋กœ๋ฏธ๋…ธ
                for(int b=0; b<3; b++){
                    int nr = r + dr5[a][b];
                    int nc = c + dc5[a][b];
                    if(nr>=0 && nr<N && nc>=0 && nc<M){
                        temp += map[nr][nc];
                        cnt++;
                    }
                }
                if(cnt == 4) max_v = max(max_v, temp);
                cnt = 1;
                temp = map[r][c];
            }
        }
    }
}


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    cin>>N>>M;
    
    for(int i=0; i<N; i++){
        for(int k=0; k<M; k++){
            cin>>map[i][k];
        }
    }
    
    go();
    cout<<max_v;
}

 

ํ’€์ด

๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜

ํšŒ์ „์ด๋‚˜ ๋Œ€์นญ๊นŒ์ง€ ๊ณ ๋ คํ•ด์„œ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ํ…ŒํŠธ๋ฆฌ๋ฏธ๋…ธ์˜ ๋ชจ์–‘์€ ์œ„ ์ด๋ฏธ์ง€์˜ ์ด 19๊ฐœ์˜ ๊ฒฝ์šฐ์ž…๋‹ˆ๋‹ค. 

ํ•ด๋‹น ์ด๋ฏธ์ง€์˜ ๊ทธ๋ฆผ์˜ ํ•˜๋Š˜์ƒ‰์œผ๋กœ ์ ์ด ์ฐํžŒ ๋ถ€๋ถ„์„ ๊ธฐ์ค€์ ์œผ๋กœ ์žก๊ณ , ๋‚˜๋จธ์ง€ ์„ธ ์ •์‚ฌ๊ฐํ˜•์ด N * M ๋ฒ”์œ„ ์•ˆ์— ์žˆ๋‹ค๋ฉด ์ ์ˆ˜๋ฅผ ๋”ํ•ด์„œ ์ตœ๋Œ“๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋Š” ๋‹จ์ˆœ ๊นก ๊ตฌํ˜„์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€