https://www.acmicpc.net/problem/14620
풀긴풀었다만 매우불쾌한문제;;
N의 범위가 그리 크지 않아서
점 세개를 정하는 경우의 수를 브루트포싱해도 된다.
문제는 점 세개의 좌표를 정해주기 위해서
무려 "6중 포문" 이 필요하다는건데 ㅠㅠ
x,y 좌표를 x*N+y로 뭉쳐서 3중포문으로 압축할 수 있다지만
일단 길고 장황하게 구현해봤다.
꽃들이 겹쳤는지 확인하기 위해서
tmp라는 배열에 꽃들을 직접 그려보고,
꽃으로 표시된 부분(1이다) 의 넓이의 합이 15가 되는지 체크해보는 방법으로
확인했다.
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
int cost[12][12] = {0};
int N;
int dx[] = {1,-1,0,0,0};
int dy[] = {0,0,0,1,-1};
int add(int x1,int y1,int x2,int y2,int x3,int y3){
bool tmp[12][12] = {0};
int chk = 0;
int ans = 0;
for(int i=0; i<5; i++){
int nx = x1+dx[i];
int ny = y1+dy[i];
tmp[nx][ny] = 1;
ans += cost[nx][ny];
}
for(int i=0; i<5; i++){
int nx = x2+dx[i];
int ny = y2+dy[i];
tmp[nx][ny] = 1;
ans += cost[nx][ny];
}
for(int i=0; i<5; i++){
int nx = x3+dx[i];
int ny = y3+dy[i];
tmp[nx][ny] = 1;
ans += cost[nx][ny];
}
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
chk += tmp[i][j];
}
}
if(chk != 15) return -1;
return ans;
}
int main(){
cin >> N;
for(int i=1; i<=N; i++){
for(int j=1; j<=N; j++){
cin >> cost[i][j];
}
}
int ans = 998244353;
for(int x1=2; x1<N; x1++){
for(int y1=2; y1<N; y1++){
for(int x2=2; x2<N; x2++){
for(int y2=2; y2<N; y2++){
if(x1 == x2 && y1 == y2) continue;
for(int x3=2; x3<N; x3++){
for(int y3=2; y3<N; y3++){
if(x1 == x3 && y1 == y3) continue;
if(x2 == x3 && y2 == y3) continue;
int tmp = add(x1,y1,x2,y2,x3,y3);
if(tmp == -1) continue;
ans = min(ans, tmp);
}
}
}
}
}
}
cout << ans;
return 0;
}
문제에 실망했다기보단
내 소스코드가 매우 가독성이 안좋아서 불쾌한....
반응형