포스트

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

문제


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

풀이


주어진 2차원 배열을 그리드 형태로 구현하고, 주어진 값 주변 원소를 이동하는 문제으로, 단순하게 주어진 방식을 구현하여 정답을 구하면 된다.

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

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

    int element = 1;

    vector<vector<int>> matrix(rows);

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            matrix[i].push_back(element);
            element++;
        }
    }

    
    for (int i = 0; i < queries.size(); i++)
    {
        int temp, temp1;

        //2,2,5,4 = 2,2 -> 2,3 -> 2,4 -> 3,4 ->4,4 -> 5,4 -> 5,3 -> 5,2 -> 4,2 -> 3,2 -> 2,2 

        int m_row = queries[i][0] - 1;
        int m_colum = queries[i][1] - 1;
        int row_cnt = 0;
        int colum_cnt = 0;

        temp1 = matrix[m_row + row_cnt][m_colum + colum_cnt];

        int cnt = 0;

        vector<int> array;

        for (int j = 0; j < queries[i][3] - queries[i][1]; j++)
        {
            if (cnt % 2 == 0)
            {
                colum_cnt++;
                temp = matrix[m_row + row_cnt][m_colum + colum_cnt];
                matrix[m_row + row_cnt][m_colum + colum_cnt] = temp1;

                array.push_back(temp1);
                cnt++;
            }

            else
            {
                colum_cnt++;
                temp1 = matrix[m_row + row_cnt][m_colum + colum_cnt];
                matrix[m_row + row_cnt][m_colum + colum_cnt] = temp;

                array.push_back(temp);
                cnt++;
            }
        }

        for (int j = 0; j < queries[i][2] - queries[i][0]; j++)
        {
            if (cnt % 2 == 0)
            {
                row_cnt++;
                temp = matrix[m_row + row_cnt][m_colum + colum_cnt];
                matrix[m_row + row_cnt][m_colum + colum_cnt] = temp1;

                array.push_back(temp1);
                cnt++;
            }

            else
            {
                row_cnt++;
                temp1 = matrix[m_row + row_cnt][m_colum + colum_cnt];
                matrix[m_row + row_cnt][m_colum + colum_cnt] = temp;

                array.push_back(temp);
                cnt++;
            }
        }

        for (int j = 0; j < queries[i][3] - queries[i][1]; j++)
        {
            if (cnt % 2 == 0)
            {
                colum_cnt--;
                temp = matrix[m_row + row_cnt][m_colum + colum_cnt];
                matrix[m_row + row_cnt][m_colum + colum_cnt] = temp1;

                array.push_back(temp1);
                cnt++;
            }

            else
            {
                colum_cnt--;
                temp1 = matrix[m_row + row_cnt][m_colum + colum_cnt];
                matrix[m_row + row_cnt][m_colum + colum_cnt] = temp;

                array.push_back(temp);
                cnt++;
            }
        }

        for (int j = 0; j < queries[i][2] - queries[i][0]; j++)
        {
            if (cnt % 2 == 0)
            {
                row_cnt--;
                temp = matrix[m_row + row_cnt][m_colum + colum_cnt];
                matrix[m_row + row_cnt][m_colum + colum_cnt] = temp1;

                array.push_back(temp1);
                cnt++;
            }

            else
            {
                row_cnt--;
                temp1 = matrix[m_row + row_cnt][m_colum + colum_cnt];
                matrix[m_row + row_cnt][m_colum + colum_cnt] = temp;

                array.push_back(temp);
                cnt++;
            }
        }

        int min = *min_element(array.begin(), array.end());

        answer.push_back(min);

    }

    return answer;
}

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.