https://www.acmicpc.net/problem/16927
16927번: 배열 돌리기 2
크기가 N×M인 배열이 있을 때, 배열을 돌려보려고 한다. 배열은 다음과 같이 반시계 방향으로 돌려야 한다. 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]
www.acmicpc.net
시간 제한이 1초다 1초
1 ≤ R ≤ 10^9
배열 돌리기 1에서 쓴
strcpy는 쓰면 안되겠다.
dx[], dy[] 배열로 구현을 하자
회전에 주기가 있으므로
r을 줄여야하는건 당연하다.
for (int i = 0; i < repeat; i++) {
int tmp = 2 * new_n + 2 * new_m - 4;
int len = r % tmp;
rotate(i, len);
new_n -= 2; //박스 안으로 들어갈때마다 가로,세로 2씩 줄어듦.
new_m -= 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
|
#include <iostream>
using namespace std;
int n, m, r;
int arr[304][304];
// 오, 아, 왼, 위
int dx[] = { 0, 1, 0, -1 };
int dy[] = { 1,0,-1,0 };
void rotate(int start, int len)
{
for (int i = 0; i < len; i++)
{
int startVal = arr[start][start];
int x = start;
int y = start;
int k = 0;
while (k < 4)
{
int nx = x + dx[k];
int ny = y + dy[k];
if (nx == start && ny == start)
break;
if (nx >= start && nx < n - start && ny >= start && ny < m - start)
{
arr[x][y] = arr[nx][ny];
x = nx;
y = ny;
}
// 배열 범위 넘어가버리면 해당 라인 변경
else
{
k++;
}
}
// 처음에 시작지점 빼놓은거 마지막에 넣는다
arr[start + 1][start] = startVal;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> r;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
}
}
int repeat = min(n, m) / 2;
int new_n = n;
int new_m = m;
for (int i = 0; i < repeat; i++) {
int tmp = 2 * new_n + 2 * new_m - 4;
int len = r % tmp;
rotate(i, len);
new_n -= 2; //박스 안으로 들어갈때마다 가로,세로 2씩 줄어듦.
new_m -= 2;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << arr[i][j] << ' ';
}
cout << '\n';
}
return 0;
}
|
cs |
'시뮬레이션과 구현 > 배열 돌리기' 카테고리의 다른 글
배열 돌리기 1 (0) | 2023.07.12 |
---|---|
배열 돌리기 3 (행렬 기초) (0) | 2023.03.02 |