1. ๋ฐฐ์ด ์ํ ๋ฐ์
1 6 2 9 8 4 → 4 2 9 3 1 8
7 2 6 9 8 2 → 9 2 3 6 1 5
1 8 3 4 2 9 → 7 4 6 2 3 1
7 4 6 2 3 1 → 1 8 3 4 2 9
9 2 3 6 1 5 → 7 2 6 9 8 2
4 2 9 3 1 8 → 1 6 2 9 8 4
<๋ฐฐ์ด> <์ฐ์ฐ ๊ฒฐ๊ณผ>
void cmd1(){ // ๋ฐฐ์ด ์ํ ๋ฐ์
int temp[101][101] = {0, };
for(int i=0; i<N; i++){
for(int k=0; k<M; k++){
temp[N-i-1][k] = map[i][k];
}
}
memcpy(map, temp, sizeof(map));
}
2. ๋ฐฐ์ด ์ข์ฐ ๋ฐ์
1 6 2 9 8 4 → 4 8 9 2 6 1
7 2 6 9 8 2 → 2 8 9 6 2 7
1 8 3 4 2 9 → 9 2 4 3 8 1
7 4 6 2 3 1 → 1 3 2 6 4 7
9 2 3 6 1 5 → 5 1 6 3 2 9
4 2 9 3 1 8 → 8 1 3 9 2 4
<๋ฐฐ์ด> <์ฐ์ฐ ๊ฒฐ๊ณผ>
void cmd2(){ // ๋ฐฐ์ด ์ข์ฐ ๋ฐ์
int temp[101][101] = {0, };
for(int i=0; i<M; i++){
for(int k=0; k<N; k++){
temp[k][M-i-1] = map[k][i];
}
}
memcpy(map, temp, sizeof(map));
}
3. ์ค๋ฅธ์ชฝ์ผ๋ก 90๋ ํ์
1 6 2 9 8 4 → 4 9 7 1 7 1
7 2 6 9 8 2 → 2 2 4 8 2 6
1 8 3 4 2 9 → 9 3 6 3 6 2
7 4 6 2 3 1 → 3 6 2 4 9 9
9 2 3 6 1 5 → 1 1 3 2 8 8
4 2 9 3 1 8 → 8 5 1 9 2 4
<๋ฐฐ์ด> <์ฐ์ฐ ๊ฒฐ๊ณผ>
void cmd3(){ // ์๊ณ๋ฐฉํฅ 90๋ ํ์
int temp[101][101] = {0, };
swap(N, M); // (6, 8) -> (8, 6)
for(int i=0; i<N; i++){
for(int k=0; k<M; k++){
temp[i][k] = map[M - k -1][i];
}
}
memcpy(map, temp, sizeof(map));
}
4. ์ผ์ชฝ์ผ๋ก 90๋ ํ์
1 6 2 9 8 4 → 4 2 9 1 5 8
7 2 6 9 8 2 → 8 8 2 3 1 1
1 8 3 4 2 9 → 9 9 4 2 6 3
7 4 6 2 3 1 → 2 6 3 6 3 9
9 2 3 6 1 5 → 6 2 8 4 2 2
4 2 9 3 1 8 → 1 7 1 7 9 4
<๋ฐฐ์ด> <์ฐ์ฐ ๊ฒฐ๊ณผ>
void cmd4(){ //์๊ณ ๋ฐ๋๋ฐฉํฅ 90๋ ํ์
int temp[101][101] = {0, };
swap(N, M); // (6, 8) -> (8, 6)
for(int i=0; i<N; i++){
for(int k=0; k<M; k++){
temp[i][k] = map[k][N-1-i];
}
}
memcpy(map, temp, sizeof(map));
}
๋ค ๊ทธ๋ฃน์ผ๋ก ๋๋ ๋ค 5-1๊ณผ 5-2์ฒ๋ผ ์์น ์ด๋
1 1 1 1 2 2 2 2
1 1 1 1 2 2 2 2
1 1 1 1 2 2 2 2
4 4 4 4 3 3 3 3
4 4 4 4 3 3 3 3
4 4 4 4 3 3 3 3
5-1 (1->2, 2->3, 3->4, 4->1)
3 2 6 3 1 2 9 7 → 2 1 3 8 3 2 6 3
9 7 8 2 1 4 5 3 → 1 3 2 8 9 7 8 2
5 9 2 1 9 6 1 8 → 4 5 1 9 5 9 2 1
2 1 3 8 6 3 9 2 → 6 3 9 2 1 2 9 7
1 3 2 8 7 9 2 1 → 7 9 2 1 1 4 5 3
4 5 1 9 8 2 1 3 → 8 2 1 3 9 6 1 8
<๋ฐฐ์ด> <์ฐ์ฐ ๊ฒฐ๊ณผ>
void cmd5_1(){
int temp[101][101] = {0, };
for(int i=0; i<N/2; i++){ // 1 -> 2
for(int k=0; k<M/2; k++){
temp[i][M/2+k] = map[i][k];
}
}
for(int i=0; i<N/2; i++){ // 2 -> 3
int idx = M/2;
for(int k=M/2; k<M; k++){
temp[N/2+i][idx++] = map[i][k];
}
}
for(int i=N/2; i<N; i++){ // 3 -> 4
int idx1 = i;
int idx2 = 0;
for(int k=M/2; k<M; k++){
temp[idx1][idx2++] = map[i][k];
}
idx1++;
}
int idx = 0;
for(int i=N/2; i<N; i++){ // 4 -> 1
for(int k=0; k<M/2; k++){
temp[idx][k] = map[i][k];
}
idx++;
}
memcpy(map, temp, sizeof(map));
}
5-2 (1->4, 4->3, 3->2, 2->1)
3 2 6 3 1 2 9 7 → 1 2 9 7 6 3 9 2
9 7 8 2 1 4 5 3 → 1 4 5 3 7 9 2 1
5 9 2 1 9 6 1 8 → 9 6 1 8 8 2 1 3
2 1 3 8 6 3 9 2 → 3 2 6 3 2 1 3 8
1 3 2 8 7 9 2 1 → 9 7 8 2 1 3 2 8
4 5 1 9 8 2 1 3 → 5 9 2 1 4 5 1 9
<๋ฐฐ์ด> <์ฐ์ฐ ๊ฒฐ๊ณผ>
void cmd5_2(){
int temp[101][101] = {0, };
for(int i=0; i<N/2; i++){ // 1 -> 2
for(int k=0; k<M/2; k++){
temp[i][k] = map[i][M/2+k];
}
}
for(int i=0; i<N/2; i++){ // 2 -> 3
int idx = M/2;
for(int k=M/2; k<M; k++){
temp[i][k] = map[N/2+i][idx++];
}
}
for(int i=N/2; i<N; i++){ // 3 -> 4
for(int k=0; k<M/2; k++){
temp[i][M/2+k] = map[i][k];
}
}
for(int i=0; i<N/2; i++){ // 4 -> 1
for(int k=0; k<M/2; k++){
temp[N/2+i][k] = map[i][k];
}
}
memcpy(map, temp, sizeof(map));
}
6. ์๊ณ ๋ฐ๋๋ฐฉํฅ์ผ๋ก ํ ์นธ์ฉ ํ์
A[1][1] ← A[1][2] ← A[1][3] ← A[1][4] ← A[1][5]
↓ ↑
A[2][1] A[2][2] ← A[2][3] ← A[2][4] A[2][5]
↓ ↓ ↑ ↑
A[3][1] A[3][2] → A[3][3] → A[3][4] A[3][5]
↓ ↑
A[4][1] → A[4][2] → A[4][3] → A[4][4] → A[4][5]
void cmd6(){ //์๊ณ ๋ฐ๋๋ฐฉํฅ
int x1 = 0, y1 = 0;
int x2 = 0, y2 = M-1;
int x3 = N-1, y3 = M-1;
int x4 = N-1, y4 = 0;
while(x1<x4 && y1<y2){
int temp = map[x1][y1];
for(int i=y1; i<y2; i++) map[x1][i] = map[x1][i+1];
for(int i=x2; i<x3; i++) map[i][y2] = map[i+1][y2];
for(int i=y3; i>y4; i--) map[x4][i] = map[x4][i-1];
for(int i=x4; i>x1; i--) map[i][y1] = map[i-1][y1];
map[x1+1][y1] = temp;
x1++; y1++;
x2++; y2--;
x3--; y3--;
x4--; y4++;
}
}
7. ์๊ณ๋ฐฉํฅ์ผ๋ก ํ์นธ์ฉ ํ์
void cmd7(){ // ์๊ณ๋ฐฉํฅ
int x1 = 0, y1 = 0;
int x2 = 0, y2 = M-1;
int x3 = N-1, y3 = M-1;
int x4 = N-1, y4 = 0;
while(x1<x4 && y1<y2){
int temp = map[x1][y1];
for(int i=x1; i<x4; i++) map[i][y1] = map[i+1][y1];
for(int i=y4; i<y3; i++) map[x3][i] = map[x3][i+1];
for(int i=x3; i>x2; i--) map[i][y2] = map[i-1][y2];
for(int i=y2; i>y1; i--) map[x1][i] = map[x1][i-1];
map[x1][y1+1] = temp;
x1++; y1++;
x2++; y2--;
x3--; y3--;
x4--; y4++;
}
}
8. ๋ฌํฝ์ด ๋ฐฐ์ด
25 | 10 | 11 | 12 | 13 |
24 | 9 | 2 | 3 | 14 |
23 | 8 | 1 | 4 | 15 |
22 | 7 | 6 | 5 | 16 |
21 | 20 | 19 | 18 | 17 |
int arr[1000][1000] = {0, };
int x=0, y=0;
int num = N*N;
int down_up_cnt = N, right_left_cnt=N-1;
for(int i=0; i<N*N; i++){
switch(i%4){
case 0: //์๋๋ฐฉํฅ
if(arr[x][y]!=0) break;
for(int k=0; k<down_up_cnt; k++){
arr[x][y]=num;
num--;
x++;
}
x--;
y++;
down_up_cnt--;
break;
case 1: //์ค๋ฅธ์ชฝ ๋ฐฉํฅ
if(arr[x][y]!=0) break;
for(int k=0; k<right_left_cnt; k++){
arr[x][y]=num;
num--;
y++;
}
y--;
x--;
right_left_cnt--;
break;
case 2: //์์ชฝ ๋ฐฉํฅ
if(arr[x][y]!=0) break;
for(int k=0; k<down_up_cnt; k++){
arr[x][y]=num;
num--;
x--;
}
x++;
y--;
down_up_cnt--;
break;
case 3: //์ผ์ชฝ ๋ฐฉํฅ
if(arr[x][y]!=0) break;
for(int k=0; k<right_left_cnt; k++){
arr[x][y]=num;
num--;
y--;
}
y++;
x++;
right_left_cnt--;
break;
}
}
'Algorithm ๐ง๐ปโ๐ป > Note' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋นํธ๋ง์คํฌ(BitMask) (0) | 2022.09.16 |
---|---|
[C++, ํ ํ๋ฆฟ] ์ ๋์จ ํ์ธ๋ (0) | 2022.09.02 |
[C++, ํ ํ๋ฆฟ] map find index (0) | 2022.08.23 |
[C++, ํ ํ๋ฆฟ] ๋ค์ต์คํธ๋ผ (0) | 2022.05.28 |
[C++, ํ ํ๋ฆฟ] ํ๋ก์ด๋-์์ฌ (0) | 2022.05.13 |
[C++, ์ ์ฉํ ๋ฌธ๋ฒ] upper_bound, lower_bound (0) | 2022.05.11 |
๋๊ธ