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

 

17406번: 배열 돌리기 4

크기가 N×M 크기인 배열 A가 있을때, 배열 A의 값은 각 행에 있는 모든 수의 합 중 최솟값을 의미한다. 배열 A가 아래와 같은 경우 1행의 합은 6, 2행의 합은 4, 3행의 합은 15이다. 따라서, 배열 A의

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <iomanip>
using namespace std;
void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
struct operation{
    int r, c, s;
};
int array_map_original[51][51];
int array_map_copy[51][51];
int N, M, K;
vector<operation> operation_list;
bool operation_check[6];
operation choose_oper[6];
// 오른쪽 아래 왼쪽 위쪽
int dx[4= { 0,1,0,-1 };
int dy[4= { 1,0,-1,0 };
int min_result = 987654321;
void cal_array_value() {
    for (int i = 1; i <= N; i++) {
        int total = 0;
        for (int j = 1; j <= M; j++) {
            total += array_map_copy[i][j];
        }
        min_result = min(min_result, total);
    }
}
void turn(const operation& oper) {
    for (int i = 1; i <= oper.s; i++) {
        deque<int> temp;
        int x = oper.r - i;
        int y = oper.c - i;
        temp.push_back(array_map_copy[x][y]);
        for (int j = 0; j < 4; j++) {
            for (int k = 0; k < i * 2; k++) {
                x += dx[j];
                y += dy[j];
                temp.push_back(array_map_copy[x][y]);
            }
        }    
        temp.pop_back();
        array_map_copy[x][y] = temp.back();
        temp.pop_back();
        for (int j = 0; j < 4; j++) {
            for (int k = 0; k < i * 2; k++) {
                if (temp.empty()) {
                    continue;
                }
                x += dx[j];
                y += dy[j];
                array_map_copy[x][y] = temp.front();
                temp.pop_front();
            }
        }
    }
 
}
void choose(int count) {
    if (count == K) {
        memcpy(array_map_copy, array_map_original, sizeof(array_map_copy));
        for (int i = 0; i < K; i++) {
            //cout << choose_oper[i].r << " " << choose_oper[i].c << " " << choose_oper[i].s;
            turn(choose_oper[i]);
        }
        cal_array_value();
        return;
    }
    for (int i = 0; i < K; i++) {
        if (!operation_check[i]) {
            operation_check[i] = true;
            choose_oper[count] = operation_list[i];
            choose(count + 1);
            operation_check[i] = false;
        }
    }
}
void input() {
    cin >> N >> M >> K;
    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= M; j++)
            cin >> array_map_original[i][j];
    for (int i = 1; i <= K; i++) {
        int r, c, s;
        cin >> r >> c >> s;
        operation_list.push_back({ r, c, s });
    }
}
int main() {
    init();
    input();
    choose(0);
    cout << min_result << endl;
}
cs

+ Recent posts