N x N 행렬이 주어질 때,
시계 방향으로 90도, 180도, 270도 회전한 모양을 출력하라.
[제약 사항]
N은 3 이상 7 이하이다.
[입력]
가장 첫 줄에는 테스트 케이스의 개수 T가 주어지고, 그 아래로 각 테스트 케이스가 주어진다.
각 테스트 케이스의 첫 번째 줄에 N이 주어지고,
다음 N 줄에는 N x N 행렬이 주어진다.
[출력]
출력의 첫 줄은 '#t'로 시작하고,
다음 N줄에 걸쳐서 90도, 180도, 270도 회전한 모양을 출력한다.
입력과는 달리 출력에서는 회전한 모양 사이에만 공백이 존재함에 유의하라.
(t는 테스트 케이스의 번호를 의미하며 1부터 시작한다.)
class Solution
{
public static void main(String args[]) throws Exception
{
System.setIn(new FileInputStream("input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int T;
T = Integer.parseInt(br.readLine());
for(int test_case = 1; test_case <= T; test_case++)
{
n = Integer.parseInt(br.readLine());
int[][] data = new int[n][n];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < n; j++) {
data[i][j] = Integer.parseInt(st.nextToken());
}
}
int[][] arr_90 = rotation(data);
int[][] arr_180 = rotation(arr_90);
int[][] arr_270 = rotation(arr_180);
// 결과출력
System.out.println("#" + test_case);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr_90[i][j]);
}
System.out.print(" ");
for (int j = 0; j < n; j++) {
System.out.print(arr_180[i][j]);
}
System.out.print(" ");
for (int j = 0; j < n; j++) {
System.out.print(arr_270[i][j]);
}
System.out.println();
}
}
}
static int n; // 배열 길이
//시계 방향 90도 회전함수
public static int[][] rotation(int[][] arr) {
int[][] result = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result[i][j] = arr[n - 1 - j][i];
}
}
return result;
}
}
3x3 행렬일 때 90도 180도 270 회전했을 때는
90도 회전 x2 90도 회전 x3 이므로, 90도 회전하는 함수를 만들어서 그 함수를 호출하는 것이 효율적
for (int i = 0; i < arrLength; i++) {
for (int j = 0; j < arrLength; j++) {
result[i][j] = arr[arrLength - 1 - j][i];
}
}