๐Ÿ™‡โ€โ™€๏ธ๋ฐฐ์—ด ํšŒ์ „

1์ฐจ์› ๋ฐฐ์—ด ํšŒ์ „

1์ฐจ์› ๋ฐฐ์—ด ํšŒ์ „๊ณผ 2์ฐจ์› ๋ฐฐ์—ด ํšŒ์ „์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

๐Ÿš€1์ฐจ์› ๋ฐฐ์—ด ํšŒ์ „

์ฒซ๋ฒˆ์งธ๋Š” rotate()๋ฅผ ์ด์šฉํ•œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

ForwardIterator rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last);

rotate์—๋Š” ํšŒ์ „ ๊ตฌ๊ฐ„์ธ [first,last)๋ฅผ ๋„ฃ๊ณ  ์–ผ๋งˆ๋‚˜ ํšŒ์ „ํ• ์ง€ ๊ฐ’์„ ๋„ฃ์œผ๋ฉด ๋œ๋‹ค.

๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ ํšŒ์ „ ์˜ˆ์‹œ

#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> v = {1, 2, 3, 4, 5, 6};
rotate(v.begin(), v.begin() + 1, v.end());
for(int i : v) cout << i << ' ';
}
/*
2 3 4 5 6 1
*/

2์นธ ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ ํšŒ์ „ ์˜ˆ์‹œ

#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main(){
vector<int> v = {1, 2, 3, 4, 5, 6};
rotate(v.begin(), v.begin() + 2, v.end());
for(int i : v) cout << i << ' ';
}
// 3 4 5 6 1 2

์ค‘๊ฐ„ ์ง€์  ํšŒ์ „ ์˜ˆ์‹œ

#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main(){
vector<int> v = {1, 2, 3, 4, 5, 6};
rotate(v.begin() + 1, v.begin() + 2, v.begin() + 5);
for(int i : v) cout << i << ' ';
}
// 1 3 4 5 2 6

๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ ํšŒ์ „์€ begin, end๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์‹œ๊ณ„ ๋ฐฉํ–ฅ ํšŒ์ „์€ rbegin, rend๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> v = {1, 2, 3, 4, 5, 6};
rotate(v.rbegin(), v.rbegin() + 1, v.rend());
for(int i : v) cout << i << ' ';
}
/*
6 1 2 3 4 5
*/

begin,end,rbegin,rend iterator

rotate()๋Š” [first, last) ์˜์—ญ์˜ ์š”์†Œ ์ค‘ [first, middel) ์„ [middle, last) ๋’ค์— ๋†“๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

๐Ÿš€2์ฐจ์› ๋ฐฐ์—ด ํšŒ์ „

2์ฐจ์›๋ฐฐ์—ด ํšŒ์ „

2์ฐจ์› ๋ฐฐ์—ด ํšŒ์ „์€ ์ง์ ‘ ๊ตฌํ˜„ํ•ด์„œ ์‚ฌ์šฉํ•œ๋‹ค.

์™ผ์ชฝ 90๋„ ํšŒ์ „ ํ•จ์ˆ˜.

void rotate_left_90(vector<vector<int>> &key){
int n = key.size();
int m = key[0].size();
vector<vector<int>> temp(m, vector<int>(n, 0));
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
temp[i][j] = key[j][m - i - 1];
}
}
key.resize(m);
key[0].resize(n);
key = temp;
return;
}

์˜ค๋ฅธ์ชฝ 90๋„ ํšŒ์ „ ํ•จ์ˆ˜.

void rotate_right_90(vector<vector<int>> &key){
int n = key.size();
int m = key[0].size();
vector<vector<int>> temp(m, vector<int>(n, 0));
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
temp[i][j] = key[n - j - 1][i];
}
}
key.resize(m);
key[0].resize(n);
key = temp;
return;
}

์œ„์˜ ๊ฒฝ์šฐ๋Š” n, m์˜ ๊ฐ’์ด ๋‹ค๋ฅผ ๊ฒฝ์šฐ์— ํ•ด๋‹นํ•œ๋‹ค.

n, m์ด ๊ฐ™๋‹ค๋ฉด resizeํ•ด์ฃผ๋Š” ๋ถ€๋ถ„์ด ์‚ฌ๋ผ์ง€๋ฏ€๋กœ ์กฐ๊ธˆ ๋” ๊ฐ„๊ฒฐํ•ด์ง„๋‹ค.

n, m์ด ๊ฐ™์€ ๊ฒฝ์šฐ ์˜ค๋ฅธ์ชฝ 90๋„ ํšŒ์ „ ํ•จ์ˆ˜.

void rotate_right_90(vector<vector<int>> &key){
vector<vector<int>> temp(n, vector<int>(n, 0));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
temp[i][j] = key[n - j - 1][i]; // ์ด ๋ถ€๋ถ„์„ ๊ณต์‹์ฒ˜๋Ÿผ ์™ธ์šฐ์ž.
}
}
key = temp;
return;
}

๐Ÿš€2์ฐจ์› ๋ฐฐ์—ด ๋Œ€์นญ

๋งŒ์•ฝ 2์ฐจ์› ๋ฐฐ์—ด์„ ๋Œ€์นญํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊นŒ?

๊ฐ™์€ ํฌ๊ธฐ์˜ ๋ฐฐ์—ด์„ ์ค€๋น„ํ•œ ํ›„, ij๋ฅผ ๋ฐ”๊พธ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค.

for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
b[j][i] = v[i][j];
}
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: