[백준] 배열 돌리기 (자바)

HeavyJ·2023년 5월 21일
0

백준

목록 보기
10/14

배열돌리기

배열 돌리기

문제 풀이

주 대각선 / 중간 열 / 부 대각선 / 중간 행이 회전한 배열을 출력하는 문제입니다.

  • 시계 방향으로 45도 회전하는 경우 → rotationRight() 메서드

    1. 주 대각선에 있는 모든 요소 → 중간 열로 이동
    2. 중간 열에 있는 모든 요소 → 부 대각선으로 이동
    3. 부 대각선에 있는 모든 요소 → 중간 행으로 이동
    4. 중간 행에 있는 모든 요소 → 주 대각선으로 이동
  • 반 시계 방향으로 45도 회전하는 경우 → rotationLeft() 메서드

    1. 주 대각선에 있는 모든 요소 → 중간 행으로 이동
    2. 중간 행에 있는 모든 요소 → 부 대각선으로 이동
    3. 부 대각선에 있는 모든 요소 → 중간 열로 이동
    4. 중간 열에 있는 모든 요소 → 주 대각선으로 이동
  • 이차원 배열을 총 3개 만들어야 합니다

    1. 실제 요소가 존재하는 배열 → arr
    2. 회전하기 전 배열 → map (이후에 temp 배열을 다시 저장)
    3. 회전 후 배열 → temp
  • rotationRight, rotationLeft 메서드를 진행하면서 회전을 진행하여 temp 배열을 만들고 map에 temp 배열 다시 저장 후 map 배열에서 0에 해당하는 요소는 arr 배열의 요소로 다시 채우는 과정 진행

문제 코드

import java.io.*;
import java.util.*;

import java.lang.*;

public class Main{
    static int len;
    static int[][] map;

    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        // 총 배열 개수
        int num = Integer.parseInt(br.readLine());

        // StringBuilder 선언
        StringBuilder sb = new StringBuilder();


        for (int q = 0 ; q < num ; q++){
            StringTokenizer st = new StringTokenizer(br.readLine());

            // 이차원 배열 길이
            len = Integer.parseInt(st.nextToken());

            map = new int[len][len];

            int[][] arr = new int[len][len];

            int angle = Integer.parseInt(st.nextToken());

            // angle 45로 나눈 값 = r
            int r = angle/45;

            for (int i = 0 ; i < len; i++) {
                st = new StringTokenizer(br.readLine());
                for (int j = 0; j < len; j++) {
                    int value = Integer.parseInt(st.nextToken());
                    map[i][j] = value;
                    arr[i][j] = value;
                }
            }

            // r에 해당하는 만큼 회전 반복문 진행
            for (int i = 0; i < Math.abs(r); i++){
                if (angle > 0){
                    rotateRight();
                }
                else{
                    rotateLeft();
                }
            }

            for (int i = 0; i < len; i++){
                for (int j = 0; j < len; j++){
                    // 만약 map에 요소가 0일 경우 arr에 있는 값을 그대로 가져오기
                    if (map[i][j] == 0){
                        map[i][j] = arr[i][j];
                    }
                    sb.append(map[i][j]+" ");
                }
                sb.append("\n");
            }
        }
        bw.write(sb.toString());

        bw.flush();
        br.close();
        bw.close();
    }

    // 45도 회전
    public static void rotateRight(){
        int[][] temp = new int[len][len];

        for (int i = 0; i < len; i++){
            temp[i][len/2] = map[i][i]; // 주 대각선 요소를 중간열 요소에 대입
            temp[i][i] = map[len/2][i]; // 중간 행 요소를 주 대각선 요소에 대입
            temp[len/2][i] = map[len-i-1][i]; // 중간 행 요소를 부 대각선 요소에 대입
            temp[len-i-1][i] = map[len-i-1][len/2]; // 부 대각선 요소를 중간 열 요소에 대입
        }
        //map 배열에 회전을 완료한 temp 배열을 대입
        map = temp;
    }

    public static void rotateLeft(){
        int[][] temp = new int[len][len];

        for (int i = 0; i < len; i++){
            temp[i][len/2] = map[i][len-i-1]; // 부 대각선 요소를 중간 열 요소에  대입
            temp[i][i] = map[i][len/2]; // 중간 열 요소를 주 대각선 요소에 대입
            temp[len/2][i] = map[i][i]; // 주 대각선 요소를 중간 행 요소에 대입
            temp[len-i-1][i] = map[len/2][i]; // 중간 행 요소를 부 대각선 요소에 대입
        }
        // map 배열에 회전을 완료한 temp 배열을 대입
        map = temp;
    }

}
profile
There are no two words in the English language more harmful than “good job”.

0개의 댓글