Skip to content

Commit d9c2d1c

Browse files
committed
[Gold V] Title: 배열 돌리기 2, Time: 580 ms, Memory: 30856 KB -BaekjoonHub
1 parent 1b61389 commit d9c2d1c

2 files changed

Lines changed: 32 additions & 68 deletions

File tree

백준/Gold/16927. 배열 돌리기 2/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 38096 KB, 시간: 1012 ms
7+
메모리: 30856 KB, 시간: 580 ms
88

99
### 분류
1010

1111
구현
1212

1313
### 제출 일자
1414

15-
2025년 2월 10일 15:22:21
15+
2025년 2월 10일 16:57:48
1616

1717
### 문제 설명
1818

Lines changed: 30 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,75 @@
1-
import java.io.BufferedReader;
2-
import java.io.IOException;
3-
import java.io.InputStreamReader;
1+
import java.io.*;
42

53
public class Main {
6-
74
static int[][] arr;
85
static int[] dx = {0, 1, 0, -1}; // 오른쪽, 아래, 왼쪽, 위
96
static int[] dy = {1, 0, -1, 0};
10-
static int minNum;
117

128
public static void main(String[] args) throws IOException {
139
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14-
10+
StringBuilder sb = new StringBuilder();
11+
1512
String[] input = br.readLine().split(" ");
1613
int N = Integer.parseInt(input[0]);
1714
int M = Integer.parseInt(input[1]);
1815
int R = Integer.parseInt(input[2]);
16+
1917
arr = new int[N][M];
2018

21-
// 배열 입력 받기
2219
for (int i = 0; i < N; i++) {
2320
String[] row = br.readLine().split(" ");
2421
for (int j = 0; j < M; j++) {
2522
arr[i][j] = Integer.parseInt(row[j]);
2623
}
2724
}
2825

29-
// 최소 회전 그룹의 수
30-
minNum = Math.min(N, M) / 2;
26+
int layers = Math.min(N, M) / 2;
3127

32-
// 각 그룹별 회전 수행
33-
for (int t = 0; t < minNum; t++) {
34-
// 그룹별 한 바퀴 길이 계산
35-
int perimeter = 2 * ((N - 2 * t) + (M - 2 * t) - 2);
28+
for (int t = 0; t < layers; t++) {
29+
//각 레이어마다 회전해야할 수를 구하고 회전 함수 실행
30+
int count = 2 * ((N - 2 * t) + (M - 2 * t) - 2);
31+
int rotations = R % count;
3632

37-
// 실제 회전 횟수 최적화
38-
int rotationCount = R % perimeter;
39-
40-
// 그룹 회전
41-
rotateGroup(t, rotationCount, N, M);
33+
for (int r = 0; r < rotations; r++) {
34+
rotateLayer(t, N, M);
35+
}
4236
}
4337

44-
// 결과 출력
45-
for (int i = 0; i < N; i++) {
46-
for (int j = 0; j < M; j++) {
47-
System.out.print(arr[i][j] + " ");
38+
39+
for (int[] row : arr) {
40+
for (int num : row) {
41+
sb.append(num).append(" ");
42+
// System.out.print(num + " ");
4843
}
49-
System.out.println();
44+
sb.append("\n");
45+
// System.out.println();
5046
}
47+
48+
System.out.println(sb.toString());
5149
}
5250

53-
// 특정 그룹 회전
54-
static void rotateGroup(int t, int rotationCount, int N, int M) {
55-
int perimeter = 2 * ((N - 2 * t) + (M - 2 * t) - 2);
56-
57-
// 그룹 값을 배열에 저장
58-
int[] group = new int[perimeter];
59-
int idx = 0;
60-
51+
static void rotateLayer(int t, int N, int M) {
6152
int x = t;
6253
int y = t;
54+
int temp = arr[x][y]; // 시작 위치 값 저장
6355

64-
// 그룹의 값 추출
6556
int direction = 0;
66-
while (idx < perimeter) {
67-
group[idx++] = arr[x][y];
6857

58+
while (direction < 4) {
6959
int nx = x + dx[direction];
7060
int ny = y + dy[direction];
7161

72-
// 범위를 벗어나면 방향 전환
73-
if (nx < t || nx >= N - t || ny < t || ny >= M - t) {
62+
// 범위 체크
63+
if (nx < t || ny < t || nx >= N - t || ny >= M - t) {
7464
direction++;
75-
nx = x + dx[direction];
76-
ny = y + dy[direction];
65+
continue;
7766
}
7867

68+
arr[x][y] = arr[nx][ny];
7969
x = nx;
8070
y = ny;
8171
}
8272

83-
// 그룹 회전 (R만큼 이동)
84-
int[] rotatedGroup = new int[perimeter];
85-
for (int i = 0; i < perimeter; i++) {
86-
rotatedGroup[(i + perimeter - rotationCount) % perimeter] = group[i];
87-
}
88-
89-
// 회전된 값을 다시 배열에 반영
90-
x = t;
91-
y = t;
92-
direction = 0;
93-
idx = 0;
94-
while (idx < perimeter) {
95-
arr[x][y] = rotatedGroup[idx++];
96-
97-
int nx = x + dx[direction];
98-
int ny = y + dy[direction];
99-
100-
// 범위를 벗어나면 방향 전환
101-
if (nx < t || nx >= N - t || ny < t || ny >= M - t) {
102-
direction++;
103-
nx = x + dx[direction];
104-
ny = y + dy[direction];
105-
}
106-
107-
x = nx;
108-
y = ny;
109-
}
73+
arr[t + 1][t] = temp; // 마지막 위치에 저장한 값 대입
11074
}
111-
}
75+
}

0 commit comments

Comments
 (0)