본문 바로가기
Algorithms/BOJ

[Java] 16935. 배열 돌리기 3

by kyungsubbb 2021. 2. 21.

www.acmicpc.net/problem/16935

 

16935번: 배열 돌리기 3

크기가 N×M인 배열이 있을 때, 배열에 연산을 R번 적용하려고 한다. 연산은 총 6가지가 있다. 1번 연산은 배열을 상하 반전시키는 연산이다. 1 6 2 9 8 4 → 4 2 9 3 1 8 7 2 6 9 8 2 → 9 2 3 6 1 5 1 8 3 4 2 9 →

www.acmicpc.net

문제

크기가 N×M인 배열이 있을 때, 배열에 연산을 R번 적용하려고 한다. 연산은 총 6가지가 있다.

1번 연산은 배열을 상하 반전시키는 연산이다.

2번 연산은 배열을 좌우 반전시키는 연산이다.

3번 연산은 오른쪽으로 90도 회전시키는 연산이다.

4번 연산은 왼쪽으로 90도 회전시키는 연산이다.

5, 6번 연산을 수행하려면 배열을 크기가 N/2×M/2인 4개의 부분 배열로 나눠야 한다. 아래 그림은 크기가 6×8인 배열을 4개의 그룹으로 나눈 것이고, 1부터 4까지의 수로 나타냈다.

5번 연산은 1번 그룹의 부분 배열을 2번 그룹 위치로, 2번을 3번으로, 3번을 4번으로, 4번을 1번으로 이동시키는 연산이다.

6번 연산은 1번 그룹의 부분 배열을 4번 그룹 위치로, 4번을 3번으로, 3번을 2번으로, 2번을 1번으로 이동시키는 연산이다.

입력

첫째 줄에 배열의 크기 N, M과 수행해야 하는 연산의 수 R이 주어진다.

둘째 줄부터 N개의 줄에 배열 A의 원소 Aij가 주어진다.

마지막 줄에는 수행해야 하는 연산이 주어진다. 연산은 공백으로 구분되어져 있고, 문제에서 설명한 연산 번호이며, 순서대로 적용시켜야 한다.

출력

입력으로 주어진 배열에 R개의 연산을 순서대로 수행한 결과를 출력한다.

제한

  • 2 ≤ N, M ≤ 100
  • 1 ≤ R ≤ 1,000
  • N, M은 짝수
  • 1 ≤ Aij ≤ 108

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
	static int N, M, R, arr[][], result[][];
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer st;
	
	static void updown(int N, int M) {								// 상하반전
		result = new int[N][M];
		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < result[i].length; j++) {
				result[i][j] = arr[result.length-1-i][j];
			}
		}
	}
	static void leftright(int N, int M) {							// 좌우반전
		result = new int[N][M];
		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < result[i].length; j++) {
				result[i][j] = arr[i][result[i].length-j-1];
			}
		}
	}
	static void right(int N, int M) {								// 오른쪽으로 90도
		result = new int[M][N];
		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < result[i].length; j++) {
				result[i][j] = arr[j][i];
			}
		}
		arr = Arrays.copyOfRange(result, 0, result.length);
		leftright(M, N);
	}
	static void left(int N, int M) {								// 왼쪽으로 90도
		result = new int[M][N];
		for (int i = 0; i < result.length; i++) {
			for (int j = 0; j < result[i].length; j++) {
				result[i][j] = arr[j][i];
			}
		}
		arr = Arrays.copyOfRange(result, 0, result.length);
		updown(M, N);
	}
	static void section1(int N, int M) {								// 1 -> 2 -> 3 -> 4 -> 1
		result = new int[N][M];
		int s1 = N/2;
		int s2 = M/2;
		for (int i = 0; i < s1; i++) {						 // section1
			for (int j = 0; j < s2; j++) {
				result[i][j] = arr[i+s1][j];
			}
		}
		for (int i = 0; i < s1; i++) {						 // section2
			for (int j = s2; j < arr[i].length; j++) {
				result[i][j] = arr[i][j-s2];
			}
		}
		for (int i = s1; i < arr.length; i++) {						 // section3
			for (int j = s2; j < arr[i].length; j++) {
				result[i][j] = arr[i-s1][j];
			}
		}
		for (int i = s1; i < arr.length; i++) {						 // section4
			for (int j = 0; j < s2; j++) {
				result[i][j] = arr[i][s2+j];
			}
		}
		arr = Arrays.copyOfRange(result, 0, result.length);
	}
	static void section2(int N, int M) {								// 4 -> 3 -> 2 -> 1 -> 4
		result = new int[N][M];
		int s1 = N/2;
		int s2 = M/2;
		for (int i = 0; i < s1; i++) {						 		// section1
			for (int j = 0; j < s2; j++) {
				result[i][j] = arr[i][j+s2];
			}
		}
		for (int i = 0; i < s1; i++) {						 		// section2
			for (int j = s2; j < arr[i].length; j++) {
				result[i][j] = arr[i+s1][j];
			}
		}
		for (int i = s1; i < arr.length; i++) {						 // section3
			for (int j = s2; j < arr[i].length; j++) {
				result[i][j] = arr[i][j-s2];
			}
		}
		for (int i = s1; i < arr.length; i++) {						 // section4
			for (int j = 0; j < s2; j++) {
				result[i][j] = arr[i-s1][j];
			}
		}
		arr = Arrays.copyOfRange(result, 0, result.length);
	}
	
	
	static void swap() {
		int tmp = N;
		N = M;
		M = tmp;
	}

	
	public static void main(String[] args) throws IOException {
		st = new StringTokenizer(in.readLine(), " ");
		N = Integer.parseInt(st.nextToken());
		M = Integer.parseInt(st.nextToken());
		R = Integer.parseInt(st.nextToken());
		
		arr = new int[N][M];
		for (int i = 0; i < arr.length; i++) {
			st = new StringTokenizer(in.readLine(), " ");
			for (int j = 0; j < arr[i].length; j++) {
				arr[i][j] = Integer.parseInt(st.nextToken());
			}
		}		
		st = new StringTokenizer(in.readLine(), " ");
		for (int i = 0; i < R; i++) {
			int tmp = Integer.parseInt(st.nextToken());
			if(tmp == 1) {					// 상하 반전
				updown(N, M);				// N, M
			}
			else if(tmp == 2) {				// 좌우반전
				leftright(N, M);			// N, M
			}
			else if(tmp == 3) {				// 오른쪽 90도
				right(N, M);				// M, N
				swap();
			}
			else if(tmp == 4) {				// 왼쪽 90도
				left(N, M);
				swap();
			}
			else if(tmp == 5) {				// 1 -> 2 -> 3 -> 4 -> 1
				section1(N, M);
			}
			else if(tmp == 6) {				// 4 -> 3 -> 2 -> 1 -> 4
				section2(N, M);
			}
			arr = Arrays.copyOfRange(result, 0, result.length);
		}
		
		
		// 배열 확인용

		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr[i].length; j++) {
				System.out.print(result[i][j]+" ");
			}
			System.out.println();
		}
	}

}

'Algorithms > BOJ' 카테고리의 다른 글

[Java] 14889. 스타트와 링크  (0) 2021.02.23
[Java] 15686. 치킨 배달  (0) 2021.02.22
[Java] 1987. 알파벳  (0) 2021.02.19
[Java] 17135. 캐슬 디펜스  (0) 2021.02.18
[Java] 11279. 최대 힙  (0) 2021.02.17