kwan's note

[c++] 프로그래머스 - 행렬 테두리 회전하기 본문

Algorithm/c++

[c++] 프로그래머스 - 행렬 테두리 회전하기

kwan's note 2022. 6. 12. 01:14
반응형

먼저 map을 1부터 nxn까지 만들고

말그대로 돌리면 된다.

복잡할 수 있지만 시키는 대로 하면 되는 구현 문제.

 

#include <iostream>
#include <vector>

using namespace std;

vector<vector<int>> map;
vector<int> qMap;

vector<int> solution(int rows, int columns, vector<vector<int>> queries) {
    vector<int> answer;
    int queries_row_len = queries.size();

    for (int i = 0; i < rows; i++)
    {
        vector<int> theRows;
        for (int j = 1; j < columns + 1; j++)
        {
            theRows.push_back(i * columns + j);
        }
        map.push_back(theRows);
    }


    for (int i = 0; i < queries_row_len; i++)
    {
        int startR = queries[i][0];
        int endR = queries[i][2];
        int startC = queries[i][1];
        int endC = queries[i][3];
        int min = 100000;
        for (int j = startC; j <= endC; j++)
        {
            min = map[startR-1][j-1] > min ? min : map[startR-1][j-1];
            qMap.push_back(map[startR-1][j-1]);
        }
        for (int j = startR+1; j < endR; j++)
        {
            min = map[j - 1][endC-1] > min ? min : map[j - 1][endC - 1];
            qMap.push_back(map[j - 1][endC - 1]);
        }
        for (int j = endC; j >= startC; j--)
        {
            min = map[endR - 1][j - 1] > min ? min : map[endR - 1][j - 1];
            qMap.push_back(map[endR-1][j-1]);
        }
        for (int j = endR-1; j > startR; j--)
        {
            min = map[j - 1][startC - 1] > min ? min : map[j - 1][startC - 1];
            qMap.push_back(map[j - 1][startC - 1]);
        }
        answer.push_back(min);
        qMap.insert(qMap.begin(), qMap.back());
        qMap.pop_back();
        for (int j = startC; j <= endC; j++)
        {
            map[startR - 1][j - 1] = qMap.front();
            qMap.erase(qMap.begin());
        }
        for (int j = startR + 1; j < endR; j++)
        {
            map[j - 1][endC - 1] = qMap.front();
            qMap.erase(qMap.begin());
        }
        for (int j = endC; j >= startC; j--)
        {
            map[endR-1][j-1] = qMap.front();
            qMap.erase(qMap.begin());
        }
        for (int j = endR - 1; j > startR; j--)
        {
            map[j - 1][startC - 1] = qMap.front();
            qMap.erase(qMap.begin());
        }
    }
    
    return answer;
}
반응형