• Home
  • About
    • Dochoi의 소소한 코딩 모음 photo

      Dochoi의 소소한 코딩 모음

      코딩을 하면서 느낀점들을 모은 공간입니다.

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
    • AI
    • Algorithm
    • Algorithm-Test
    • Cloud
    • Docker
    • Kubernetes
    • iOS
    • Culture
  • Projects

[백준]:17406(배열 돌리기 4)

28 Oct 2020

Reading time ~2 minutes

algorithm-test

17406(배열 돌리기 4)

//
//  main.cpp
//  algorithmtest
//
//  Created by 최동규 on 2020/09/13.
//  Copyright © 2020 최동규. All rights reserved.
//

#include <iostream>
#include <vector>
#include <queue>
#include <memory.h>
#include <cstring>
#include <algorithm>

using namespace std;

int n, m, k;
int a[50][50];
int origin[50][50];
int answer = 2147483647;

typedef struct s_command
{
    int r, c, s;
}t_command;

t_command command[6];

void print()
{
    for (int j = 0; j < n; j++)
    {
        for (int k = 0; k < m; k++)
        {
            cout << a[j][k] <<" ";
            
        }
        cout << endl;
    }
    cout << endl;
}

void max_value()
{
    for (int j = 0; j < n; j++)
    {
        int sum = 0;
        for (int k = 0; k < m; k++)
        {
            sum += a[j][k];
        }
        answer = min(answer, sum);
    }

}

void turn_right(int r, int c, int circle)
{
    int left_bound = c - circle;
    int right_bound = c + circle;
    int up_bound = r - circle;
    int down_bound = r + circle;
    int lr_value = a[up_bound][left_bound];
    for (int i = up_bound; i < down_bound; i++)
    {
        a[i][left_bound] = a[i + 1][left_bound];
    }
    for (int i = left_bound; i < right_bound; i++)
    {
        a[down_bound][i] = a[down_bound][i + 1];
    }
    for (int i = down_bound; i > up_bound; i--)
    {
        a[i][right_bound] = a[i - 1][right_bound];
    }
    for (int i = right_bound; i > left_bound; i--)
    {
        a[up_bound][i] = a[up_bound][i - 1];
    }
    a[up_bound][left_bound + 1] = lr_value;
}



int main(int argc, const char * argv[])
{
   // freopen("input", "r", stdin);
    cin >> n >> m >> k;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> a[i][j];
            origin[i][j] = a[i][j];
        }
    }
    
    int r,c,s;
    vector<int> v;
    for (int i =0  ;i < k;i++)
    {
        v.push_back(i);
            cin >> r >> c >> s;
            r--;
            c--;
        command[i].c = c;
        command[i].r = r;
        command[i].s = s;
    }
      
    do{
        
        for (int j = 0; j < k; j++)
        {
            int idx = v[j];
            for (int i = 1; i <= command[idx].s; i++)
            {
                turn_right(command[idx].r, command[idx].c, i);
            }
        }
        max_value();
        for (int i = 0; i < n; i++)
            memcpy(a[i], origin[i], sizeof(int) * m);
    }
    while(next_permutation(v.begin(), v.end()));
    cout << answer << "\n";
    return 0;
}

고찰

조합이 섞인 단순 구현문제입니다. Next_permutation을 이용하여 조합을 구현하였습니다.



Algorithm-TestImplement Share Tweet +1