알고리즘

백준 16927번 배열 돌리기2 (JAVA)

박카스마시며코딩 2022. 1. 27. 14:21

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

 

 

저는 해당 문제를 Queue를 이용하여 문제를 해결하였습니다.

가장 밖에 있는 라인부터 안쪽으로 Queue를 이용해 같이 돌아야하는 요소들을 모두 집어넣습니다. 이후 돌아야 되는 만큼 Queue 앞에서 빼서 뒤에 넣습니다. 이때 k를 Queue 사이즈로 모듈러 연산을 합니다. k가 Queue사이즈만큼 돌면 제자리로 돌아오기 때문입니다.

 

package BOJ.ETC;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.function.Function;

public class BOJ_16927 {
    static private final int[] dy = {1,0,-1,0};
    static private final int[] dx = {0,-1,0,1};
    static private void rotate(int[][] arr, int k, int n, int m){
        int[][] temp = new int[n][m];
        for(int depth = 0 ; depth < Math.min(n,m)/2 ; depth++) {
            Queue<Integer> q = new LinkedList<>();
            int ny = depth;
            int nx = m - 1 - depth;
            for(int i = 0 ; i < 4 ; i++){
                ny += dy[i];
                nx += dx[i];
                while(ny >= depth && ny < n - depth && nx >= depth && nx < m - depth){
                    q.offer(arr[ny][nx]);
                    ny += dy[i];
                    nx += dx[i];
                }
                ny -= dy[i];
                nx -= dx[i];
            }
            int cnt = k % q.size();
            for(int i = 0 ; i < cnt ; i++){
                int num = q.poll();
                q.offer(num);
            }
            ny = depth;
            nx = m - 1- depth;
            for(int i = 0 ; i < 4 ; i++){
                ny += dy[i];
                nx += dx[i];
                while(ny >= depth && ny < n - depth && nx >= depth && nx < m - depth){
                    arr[ny][nx] = q.poll();
                    ny += dy[i];
                    nx += dx[i];
                }
                ny -= dy[i];
                nx -= dx[i];
            }
//            print(arr,n,m);
//            System.out.println("-----------------------------");
        }
    }
    static private void print(int[][] arr, int n , int m){
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < m ; j++){
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        Function<String,Integer> stoi = Integer::parseInt;
        int n = stoi.apply(st.nextToken());
        int m = stoi.apply(st.nextToken());
        int k = stoi.apply(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] = stoi.apply(st.nextToken());
            }
        }
        rotate(arr,k,n,m);
        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < m ; j++){
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
    }
}

'알고리즘' 카테고리의 다른 글

백준 12919번 A와B 2(JAVA)  (0) 2022.01.28
백준 11729번 하노이 탑 이동 순서(JAVA)  (0) 2022.01.27
백준 1043번 거짓말 (JAVA)  (0) 2022.01.26
백준 2467번 용액 (JAVA)  (0) 2022.01.25
백준 1253번 좋다 (JAVA)  (0) 2022.01.24