[ 문제 ]
크기가 N×M인 배열이 있을 때, 배열을 돌려보려고 한다.
배열은 다음과 같이 반시계 방향으로 돌려야 한다.
예를 들어, 아래와 같은 배열을 2번 회전시키면 다음과 같이 변하게 된다.
배열과 정수 R이 주어졌을 때, 배열을 R번 회전시킨 결과를 구해보자.
[ 입력 ]
- 첫째 줄에 배열의 크기 N, M과 수행해야 하는 회전의 수 R이 주어진다.
- 둘째 줄부터 N개의 줄에 배열 A의 원소 Aij가 주어진다.
[ 출력 ]
- 입력으로 주어진 배열을 R번 회전시킨 결과를 출력한다.
[ 예제 입력 ]
5 4 7
1 2 3 4
7 8 9 10
13 14 15 16
19 20 21 22
25 26 27 28
[ 예제 출력 ]
28 27 26 25
22 9 15 19
16 8 21 13
10 14 20 7
4 3 2 1
import java.util.*;
import java.io.*;
public class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int R = Integer.parseInt(st.nextToken());
int[][] arr = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++)
arr[i][j] = Integer.parseInt(st.nextToken());
}
int count = Math.min(N, M) / 2; // 회전하는 라인 개수
for (int r = 0; r < R; r++) {
for (int i = 0; i < count; i++) {
int temp = arr[i][i];
for (int j = i+1; j < M-i; j++) arr[i][j-1] = arr[i][j]; // 라인의 위쪽 부분
for (int j = i+1; j < N-i; j++) arr[j-1][M-1-i] = arr[j][M-1-i]; // 라인의 오른쪽 부분
for (int j = M-2-i; j >= i; j--) arr[N-1-i][j+1] = arr[N-1-i][j]; // 라인의 아래쪽 부분
for (int j = N-2-i; j >= i; j--) arr[j+1][i] = arr[j][i]; // 라인의 왼쪽 부분
arr[i+1][i] = temp;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
}
}
'Algorithm > 백준+프로그래머스+SWEA+정올+구름' 카테고리의 다른 글
[Algorithm] 백준 3040 백설공주와 일곱난쟁이 (0) | 2021.08.12 |
---|---|
[Algorithm] SWEA 1974 스도쿠 검증 (0) | 2021.08.12 |
[Algorithm] 백준 1158 요세푸스 문제 (0) | 2021.08.10 |
[Algorithm] 백준 1427 소트인사이드 (0) | 2021.08.10 |
[Algorithm] SWEA 1210 Ladder1 (0) | 2021.08.10 |