복습이 필요한 알고 팁
배열 시계방향, 반시계방향 돌리기
헐랭미
2020. 10. 17. 18:23
이 문제를 풀다가 배열을 시계방향/반시계방향 시 배열의 변화를 반쯤 외워야 겠다는 생각이 들었다.
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
|
#include <iostream>
#include <vector>
#include <algorithm>
#define pii pair<int, int>
using namespace std;
int map[100][100];
int x, y, r;
/*
시계로 돌릴때 기준 방법
1. map을 copy한 tmp_map을 만든다.
2. x,y를 swap한다.
3-시계 map[i][j] = tmp_map[y-1-j][i];
3-반시계 map[i][j] = tmp_map[j][x-1-i];
*/
void rotate_clock() // 시계
{
int tmp_map[100][100];
copy(&map[0][0], &map[0][0] + 10000, &tmp_map[0][0]); // 쓸데없는 2차원 copy법.
int tmp = x;
x = y;
y = tmp;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
map[i][j] = tmp_map[y - 1 - j][i];
}
}
void rotate_rclock() // 반시계
{
int tmp_map[100][100];
copy(&map[0][0], &map[0][0] + 10000, &tmp_map[0][0]);
int tmp = x;
x = y;
y = tmp;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
map[i][j] = tmp_map[j][x - 1 - i];
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> x >> y >> r;
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
cin >> map[i][j];
}
int a;
for (int i = 0; i < r; i++)
{
cin >> a;
if (a == 3)
rotate_clock();
else if (a == 4)
rotate_rclock();
}
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
cout << map[i][j] << ' ';
cout << '\n';
}
}
|
cs |