혼자 정리

14500: 테트로미노 본문

알고리즘 문풀/백준

14500: 테트로미노

tbonelee 2021. 8. 4. 10:53

문제링크

C++

#include <iostream>

using namespace std;

int N, M;
int map[500][500];
int ret;

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> N >> M;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cin >> map[i][j];
        }
    }
    int tmp;

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (i + 3 < N) {
                tmp = map[i][j] + map[i + 1][j] + map[i + 2][j] + map[i + 3][j];
                if (tmp > ret) {
                    ret = tmp;
                }
            }
            if (j + 3 < M) {
                tmp = map[i][j] + map[i][j + 1] + map[i][j + 2] + map[i][j + 3];
                if (tmp > ret) {
                    ret = tmp;
                }
            }
            if (i + 1 < N && j + 1 < M) {
                tmp = map[i][j] + map[i + 1][j] + map[i + 1][j + 1] + map[i][j + 1];
                if (tmp > ret) {
                    ret = tmp;
                }
            }
            if (i + 2 < N && j + 1 < M) {
                tmp = map[i][j] + map[i + 1][j] + map[i + 2][j] + map[i + 2][j + 1];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i + 1][j] + map[i + 1][j + 1] + map[i + 2][j + 1];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i + 1][j] + map[i + 2][j] + map[i + 1][j + 1];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i + 1][j] + map[i + 2][j] + map[i][j + 1];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i][j + 1] + map[i + 1][j + 1] + map[i + 2][j + 1];
                if (tmp > ret) {
                    ret = tmp;
                }
            }
            if (i + 2 < N && j - 1 >= 0) {
                tmp = map[i][j] + map[i + 1][j] + map[i + 2][j] + map[i + 2][j - 1];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i + 1][j] + map[i + 1][j - 1] + map[i + 2][j - 1];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i + 1][j] + map[i + 2][j] + map[i + 1][j - 1];
                if (tmp > ret) {
                    ret = tmp;
                }
            }
            if (i + 1 < N && j + 2 < M) {
                tmp = map[i][j] + map[i][j + 1] + map[i][j + 2] + map[i + 1][j];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i][j + 1] + map[i][j + 2] + map[i + 1][j + 2];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i + 1][j] + map[i + 1][j + 1] + map[i + 1][j + 2];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i][j + 1] + map[i + 1][j + 1] + map[i + 1][j + 2];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i][j + 1] + map[i][j + 2] + map[i + 1][j + 1];
                if (tmp > ret) {
                    ret = tmp;
                }
            }
            if (i + 1 < N && j - 2 >= 0) {
                tmp = map[i][j] + map[i + 1][j] + map[i + 1][j - 1] + map[i + 1][j - 2];
                if (tmp > ret) {
                    ret = tmp;
                }
                tmp = map[i][j] + map[i][j - 1] + map[i + 1][j - 1] + map[i + 1][j - 2];
                if (tmp > ret) {
                    ret = tmp;
                }
            }
            if (i + 1 < N && j - 1 >= 0 && j + 1 < M) {
                tmp = map[i][j] + map[i + 1][j] + map[i + 1][j - 1] + map[i + 1][j + 1];
                if (tmp > ret) {
                    ret = tmp;
                }
            }

        }
    }
    cout << ret << "\n";
}

Python

import sys
input = sys.stdin.readline

N, M = map(int, input().split())

mymap = []

for _ in range(N):
    mymap.append(list(map(int, input().split())))

ret = 0

for i in range(N):
    for j in range(M):
        if i + 3 < N:
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 2][j] + mymap[i + 3][j]
            if tmp > ret:
                ret = tmp
        if j + 3 < M:
            tmp = mymap[i][j] + mymap[i][j + 1] + mymap[i][j + 2] + mymap[i][j + 3]
            if tmp > ret:
                ret = tmp
        if i + 1 < N and j + 1 < M:
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 1][j + 1] + mymap[i][j + 1]
            if tmp > ret:
                ret = tmp
        if i + 2 < N and j + 1 < M:
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 2][j] + mymap[i + 2][j + 1]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 1][j + 1] + mymap[i + 2][j + 1]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 2][j] + mymap[i + 1][j + 1]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 2][j] + mymap[i][j + 1]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i][j + 1] + mymap[i + 1][j + 1] + mymap[i + 2][j + 1]
            if tmp > ret:
                ret = tmp
        if i + 2 < N and j - 1 >= 0:
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 2][j] + mymap[i + 2][j - 1]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 1][j - 1] + mymap[i + 2][j - 1]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 2][j] + mymap[i + 1][j - 1]
            if tmp > ret:
                ret = tmp
        if i + 1 < N and j + 2 < M:
            tmp = mymap[i][j] + mymap[i][j + 1] + mymap[i][j + 2] + mymap[i + 1][j]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i][j + 1] + mymap[i][j + 2] + mymap[i + 1][j + 2]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 1][j + 1] + mymap[i + 1][j + 2]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i][j + 1] + mymap[i + 1][j + 1] + mymap[i + 1][j + 2]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i][j + 1] + mymap[i][j + 2] + mymap[i + 1][j + 1]
            if tmp > ret:
                ret = tmp
        if i + 1 < N and j - 2 >= 0:
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 1][j - 1] + mymap[i + 1][j - 2]
            if tmp > ret:
                ret = tmp
            tmp = mymap[i][j] + mymap[i][j - 1] + mymap[i + 1][j - 1] + mymap[i + 1][j - 2]
            if tmp > ret:
                ret = tmp
        if i + 1 < N and j - 1 >= 0 and j + 1 < M:
            tmp = mymap[i][j] + mymap[i + 1][j] + mymap[i + 1][j - 1] + mymap[i + 1][j + 1]
            if tmp > ret:
                ret = tmp

print(ret)
  • 브루트포스

'알고리즘 문풀 > 백준' 카테고리의 다른 글

14888번: 연산자 끼워넣기  (0) 2021.08.05
14502번: 연구소  (0) 2021.08.05
13458: 시험 감독  (0) 2021.08.04
14496번: 주사위 굴리기  (0) 2021.08.03
11726번: 2×n 타일링  (0) 2021.07.19