티스토리 뷰

https://programmers.co.kr/learn/courses/30/lessons/77485

 

코딩테스트 연습 - 행렬 테두리 회전하기

6 6 [[2,2,5,4],[3,3,6,6],[5,1,6,3]] [8, 10, 25] 3 3 [[1,1,2,2],[1,2,2,3],[2,1,3,2],[2,2,3,3]] [1, 1, 5, 3]

programmers.co.kr

 

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

#include <string>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> solution(int rows, int columns, vector<vector<int>> queries) {
    vector<int> answer;
    vector<vector<int>> map(rows, vector<int>(columns, 0));

    int cnt = 1;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            map[i][j] = cnt;
            cnt++;
        }
    }

    for (int n = 0; n < queries.size(); n++) {
        vector<int> q = queries[n];

        int si = q[0] - 1;
        int sj = q[1] - 1;
        int ei = q[2] - 1;
        int ej = q[3] - 1;

        int i = si;
        int j = sj;
        int before = map[i][j];
        int mn = before;
        int tmp;
        while (j < ej) {
            j++;
            mn = min(mn, map[i][j]);
            tmp = map[i][j];
            map[i][j] = before;
            before = tmp;
        }
        while (i < ei) {
            i++;
            mn = min(mn, map[i][j]);
            tmp = map[i][j];
            map[i][j] = before;
            before = tmp;
        }
        while (j > sj) {
            j--;
            mn = min(mn, map[i][j]);
            tmp = map[i][j];
            map[i][j] = before;
            before = tmp;
        }
        while (i > si) {
            i--;
            mn = min(mn, map[i][j]);
            tmp = map[i][j];
            map[i][j] = before;
            before = tmp;
        }

        answer.push_back(mn);
    }

    return answer;
}
댓글