https://github.com/ukjinlee66/BOJ/blob/master/17406.cpp

 

ukjinlee66/BOJ

baekjoon Online Judge problem. Contribute to ukjinlee66/BOJ development by creating an account on GitHub.

github.com

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

 

17406번: 배열 돌리기 4

크기가 N×M 크기인 배열 A가 있을때, 배열 A의 값은 각 행에 있는 모든 수의 합 중 최솟값을 의미한다. 배열 A가 아래와 같은 경우 1행의 합은 6, 2행의 합은 4, 3행의 합은 15이다. 따라서, 배열 A의 값은 4이다. 1 2 3 2 1 1 4 5 6 배열은 회전 연산을 수행할 수 있다. 회전 연산은 세 정수 (r, c, s)로 이루어져 있고, 가장 왼쪽 윗 칸이 (r-s, c-s), 가장 오른쪽 아랫 칸이 (r+s, c+s)인 정사각형을 시계

www.acmicpc.net

문제에서 시키는 방법대로만 구현하면 되는 문제였다. 다만 배열을 돌리기 위해서 가장 바깥 단계부터 안쪽으로 몇 단계까지 돌려야 하는지는 s라는 변수를 생각하여 코딩하였고 3번의 시도 끝에 풀게 되었는데 next_permutaion 사용 시 vector라던지 배열이 sort 되어있는 상태에서 사용되어야 한다는 것을 다시 한번 생각하는 시간이 되었다.

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
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<intint> P;
vector<pair<intpair<intint>>> turn;
int map[51][51];
int copmap[51][51];
int n, m, k;
int res = 987654321;
void print_map()
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
            cout << map[i][j] << " ";
        puts("");
    }
    puts("");
}
void recover_map()
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            map[i][j] = copmap[i][j];
}
void copy_map()
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            copmap[i][j] = map[i][j];
}
void check_minnum_sum()
{
    for (int i = 0; i < n; i++)
    {
        int num = 0;
        for (int j = 0; j < m; j++)
        {
            num += map[i][j];
        }
        res = min(res, num);
    }
}
void turning(int r, int c, int s)
{
    while (s!=0
    {
        int temp = map[r - s][c - s];
        for (int i = r - s; i < r + s; i++)
            map[i][c - s] = map[i + 1][c - s];
        for (int j = c - s; j < c + s; j++)
            map[r + s][j] = map[r + s][j + 1];
        for (int i = r + s; i > r - s; i--)
            map[i][c + s] = map[i - 1][c + s];
        for (int j = c + s; j > c - s; j--)
            map[r - s][j] = map[r - s][j - 1];
        map[r - s][c - s + 1= temp;
        s--;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
            cin >> map[i][j];
    for (int i = 0; i < k; i++)
    {
        int a, b, c; cin >> a >> b >> c;
        turn.push_back(make_pair(a-1,make_pair(b-1,c)));
    }
    copy_map();//맵을복사.
    sort(turn.begin(), turn.end());//permutation 을사용하기위한 sort.
    do 
    {
        for (int i = 0; i < turn.size(); i++)
        {
            int r = turn[i].first;
            int c = turn[i].second.first;
            int s = turn[i].second.second;
            turning(r, c, s);
        }
        check_minnum_sum();
        recover_map();//원본을가져옴.
    } while (next_permutation(turn.begin(), turn.end()));
    //벡터에담긴 회전연산의 순서를 전부돌아본다 ->next_permutation
    //각연산은 한번씩적용.
    cout << res;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

'Problem Solving > 삼성 역량테스트' 카테고리의 다른 글

BOJ-14500 테트로미노  (0) 2019.11.07
BOJ-17837 새로운 게임2  (0) 2019.11.06
BOJ-12100 2048(Easy)  (0) 2019.11.06
BOJ-17144 미세먼지 안녕  (0) 2019.11.06
BOJ-17780 새로운 게임  (0) 2019.11.05

+ Recent posts