백준 [14500] "테트로미노"

Kimbab1004·2024년 3월 3일
0

Algorithm

목록 보기
22/102

구현 방식을 생각하지 못해 그냥 하나씩 모두 찾아가는 방식으로 풀어보았다. 상당히 노가다 였는데 어디서 꼬인건지 정답은 제대로 나오지 않는다.

실패 코드

#include <iostream>
#include <deque>
#include <string>
#include <sstream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <map>
#include <cstring>

int tet[550][550];

using namespace std;
int N, M;
int sltet, sStet, sZtet, sLtet, sTtet;
int start = 6;
int result = 0;
int ltet(int a, int b) {
	int z = 0; int x = 0; int c = 0; int v = 0;
	z += tet[a][b];
	z += tet[a - 1][b];
	z += tet[a - 2][b];
	z += tet[a - 3][b];

	x += tet[a][b];
	x += tet[a][b + 1];
	x += tet[a][b + 2];
	x += tet[a][b + 3];

	c += tet[a][b];
	c += tet[a - 1][b];
	c += tet[a - 2][b];
	c += tet[a - 3][b];

	v += tet[a][b];
	v += tet[a][b - 1];
	v += tet[a][b - 2];
	v += tet[a][b - 3];

	return max(z, (x, (c, v)));

}
int Stet(int a, int b) {
	int z = 0; int x = 0; int c = 0; int v = 0;
	z += tet[a][b];
	z += tet[a - 1][b];
	z += tet[a - 1][b-1];
	z += tet[a][b + 1];

	x += tet[a][b];
	x += tet[a][b + 1];
	x += tet[a - 1][b + 1];
	x += tet[a - 1][b];

	c += tet[a][b];
	c += tet[a - 1][b];
	c += tet[a - 1][b - 1];
	c += tet[a][b - 1];

	v += tet[a][b];
	v += tet[a][b - 1];
	v += tet[a+1][b - 1];
	v += tet[a - 1][b];

	return max(z, (x, (c, v)));
}
int Ztet(int a, int b) {
	int z = 0; int x = 0; int c = 0; int v = 0;
	z += tet[a][b];
	z += tet[a - 1][b];
	z += tet[a - 1][b + 1];
	z += tet[a - 2][b + 1];

	x += tet[a][b];
	x += tet[a][b + 1];
	x += tet[a-1][b + 1];
	x += tet[a-1][b + 2];

	c += tet[a][b];
	c += tet[a - 1][b];
	c += tet[a - 1][b -1];
	c += tet[a - 2][b - 1];

	v += tet[a][b];
	v += tet[a][b - 1];
	v += tet[a - 1][b - 1];
	v += tet[a - 1][b - 2];

	return max(z, (x, (c, v)));
}
int Ltet(int a, int b) {
	int z = 0; int x = 0; int c = 0; int v = 0;
	z += tet[a][b];
	z += tet[a - 1][b];
	z += tet[a - 2][b];
	z += tet[a - 2][b + 1];

	x += tet[a][b];
	x += tet[a][b + 1];
	x += tet[a][b + 2];
	x += tet[a + 1][b + 2];

	c += tet[a][b];
	c += tet[a + 1][b];
	c += tet[a + 2][b];
	c += tet[a + 2][b - 1];

	v += tet[a][b];
	v += tet[a][b - 1];
	v += tet[a][b - 2];
	v += tet[a - 1][b - 2];

	return max(z, (x, (c, v)));
}
int Ttet(int a, int b) {
	int z = 0; int x = 0; int c = 0; int v = 0;
	z += tet[a][b];
	z += tet[a][b + 1];
	z += tet[a][b + 2];
	z += tet[a - 1][b + 1];

	x += tet[a][b];
	x += tet[a-1][b];
	x += tet[a-2][b];
	x += tet[a - 1][b + 1];

	c += tet[a][b];
	c += tet[a][b-1];
	c += tet[a][b-2];
	c += tet[a + 1][b - 1];

	v += tet[a][b];
	v += tet[a - 1][b];
	v += tet[a - 2][b];
	v += tet[a - 1][b - 1];

	return max(z, (x, (c, v)));
}

int main(void) {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	cin >> N >> M;

	for (int i = start; i < N+start; i++) {
		for (int j = start; j < M+ start; j++) {
			cin >> tet[i][j];
		}
	}

	for (int i = start; i < N + start; i++) {
		for (int j = start; j < M + start; j++) {
			sltet = ltet(i, j);
			sTtet = Ttet(i, j);
			sLtet = Ltet(i, j);
			sZtet = Ztet(i, j);
			sStet = Stet(i, j);
			result = max(result, (sLtet, (sTtet, (sLtet, (sZtet, sStet)))));
		}
	}

	cout << result;

	return 0;

}

정답 코드

#include <iostream>
using namespace std;
int map[555][555];
int n, k;
void input();
void solve();
int poly(int a, int b);
void except();
int main(){
    input();
    solve();
}
void input(){
    scanf("%d", &n);
    scanf("%d", &k);
    for(int j = 1; j <= n; j++){
        for(int i = 1; i <= k; i++) {
            scanf("%d", &map[j][i]);
        }
    }
}
void solve(){
    int ans = 0;
    for(int j = 1; j <= n; j++){
        for(int i = 1; i <= k; i++) {
            ans = max(ans, poly(j, i));
        }
    }
    printf("%d", ans);
}
int poly(int a, int b){ // 단순히 모든 테트로미노를 체크해본다
    int res = 0;
    if (b + 3 <= k) res = max(res, map[a][b] + map[a][b + 1] + map[a][b + 2] + map[a][b + 3]);
    if (a + 3 <= n) res = max(res, map[a][b] + map[a + 1][b] + map[a + 2][b] + map[a + 3][b]);
    
    if (a + 1 <= n and b + 1 <= k) res = max(res, map[a][b] + map[a][b + 1] + map[a + 1][b] + map[a + 1][b + 1]);
    
    if (a + 2 <= n and b + 1 <= k) res = max(res, map[a][b] + map[a + 1][b] + map[a + 2][b] + map[a][b + 1]);
    if (a + 2 <= n and b - 1 >= 1) res = max(res, map[a][b] + map[a + 1][b] + map[a + 2][b] + map[a][b - 1]);
    if (a - 1 >= 1 and b + 2 <= k) res = max(res, map[a][b] + map[a][b + 1] + map[a][b + 2] + map[a - 1][b]);
    if (a + 1 <= n and b + 2 <= k) res = max(res, map[a][b] + map[a][b + 1] + map[a][b + 2] + map[a + 1][b]);
    if (a - 2 >= 1 and b - 1 >= 1) res = max(res, map[a][b] + map[a - 1][b] + map[a - 2][b] + map[a][b - 1]);
    if (a - 2 >= 1 and b + 1 <= k) res = max(res, map[a][b] + map[a - 1][b] + map[a - 2][b] + map[a][b + 1]);
    if (a + 1 <= n and b - 2 >= 1) res = max(res, map[a][b] + map[a][b - 1] + map[a][b - 2] + map[a + 1][b]);
    if (a - 1 >= 1 and b - 2 >= 1) res = max(res, map[a][b] + map[a][b - 1] + map[a][b - 2] + map[a - 1][b]);
 
    
    if (a - 1 >= 1 and a + 1 <= n and b - 1 >= 1) res = max(res, map[a][b] + map[a - 1][b] + map[a][b - 1] + map[a + 1][b - 1]);
    if (a - 1 >= 1 and a + 1 <= n and b - 1 >= 1) res = max(res, map[a][b] + map[a + 1][b] + map[a][b - 1] + map[a - 1][b - 1]);
    if (a + 1 <= n and b + 1 <= k and b - 1 >= 1) res = max(res, map[a][b] + map[a + 1][b] + map[a][b + 1] + map[a + 1][b - 1]);
    if (a + 1 <= n and b + 1 <= k and b - 1 >= 1) res = max(res, map[a][b] + map[a + 1][b] + map[a][b - 1] + map[a + 1][b + 1]);
    
    if (a - 1 >= 1 and a + 1 <= n and b + 1 <= k) res = max(res, map[a][b] + map[a + 1][b] + map[a - 1][b] + map[a][b + 1]);
    if (a - 1 >= 1 and a + 1 <= n and b - 1 >= 1) res = max(res, map[a][b] + map[a + 1][b] + map[a - 1][b] + map[a][b - 1]);
    if (b - 1 >= 1 and b + 1 <= k and a - 1 >= 1) res = max(res, map[a][b] + map[a - 1][b] + map[a][b + 1] + map[a][b - 1]);
    if (b - 1 >= 1 and b + 1 <= k and a + 1 <= n) res = max(res, map[a][b] + map[a + 1][b] + map[a][b + 1] + map[a][b - 1]);
    
    return res;
}

0개의 댓글