본문 바로가기
  • 소소한 개발자 이야기
Algorithm Study/BOJ with C++

(BAEKJOON) 17779번: 게리맨더링 2

by Siwan_Min 2020. 7. 8.
728x90

https://www.acmicpc.net/problem/17779

 

17779번: 게리맨더링 2

재현시의 시장 구재현은 지난 몇 년간 게리맨더링을 통해서 자신의 당에게 유리하게 선거구를 획정했다. 견제할 권력이 없어진 구재현은 권력을 매우 부당하게 행사했고, 심지어는 시의 이름��

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <cstring>
#include <algorithm>
 
#define MAX 20
using namespace std;
int n, sum; 
int map[MAX][MAX];
int ans = 987987987;
int arr[5];
 
void getSection(int y, int x, int d1, int d2){
 
    for(int i = 0; i < 5; i++) arr[i] = 0;
 
    int k =0;
 
    for(int i = 0; i < y + d1; i++){
        if(i >= y) k++;
        for(int j = 0; j <= x -k; j++)
            arr[0+= map[i][j];
    }
 
    k = 1;
    for(int i = 0; i <= y + d2; i++){
        if(i > y) k++;
        for(int j = x +k; j < n; j++)
            arr[1+= map[i][j];
    }
 
    k = 0;
    for(int i = y + d1; i <n; i++){
        for(int j =0; j < x - d1 + k; j++)
            arr[2+= map[i][j];
        if(i < y + d1 + d2) k++;
    }
 
   k = 0;
   for(int i = y + d2 + 1; i <n; i++){
       for(int j = x + d2 - k; j <n; j++)
            arr[3+= map[i][j];
       if(i <= y +d1 + d2) k++;
   }
 
   arr[4= sum - (arr[0+ arr[1+ arr[2+ arr[3]);
   int min = 987987987int max = 0;
 
   for(int i = 0; i < 5; i++){
       if(arr[i] > max) max = arr[i];
       if(arr[i] < min) min = arr[i];
   }
   if(ans > max - min) ans = max - min;
}
 
void getArea(){
 
    for(int y = 0; y < n-2; y++){ // y 시작점
        for(int x = 1; x < n-1; x++){ // x 시작점
            for(int d1 = 1; y+d1 < n-1 && x-d1 >=0 ;d1++){
                for(int d2 = 1; y+d1+d2 < n && x + d2 - d1 < n-1; d2++ ){
 
                    getSection(y, x, d1, d2);
                }
            }
        }
    }
}
 
int main(){
    cin>>n;
 
    for(int i = 0; i<n; i++){
        for(int j = 0; j<n; j++){
            cin>>map[i][j];
            sum += map[i][j];
        }
    }
    getArea();
    cout << ans <<endl;
 
    return 0;
}
728x90

댓글