[Java] 백준 / 배열 돌리기 3 / 16935번

정현명·2022년 2월 9일
0

baekjoon

목록 보기
45/180
post-thumbnail

[Java] 백준 / 배열 돌리기 3 / 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 → 7 4 6 2 3 1
7 4 6 2 3 1 → 1 8 3 4 2 9
9 2 3 6 1 5 → 7 2 6 9 8 2
4 2 9 3 1 8 → 1 6 2 9 8 4
   <배열>       <연산 결과>

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

1 6 2 9 8 4 → 4 8 9 2 6 1
7 2 6 9 8 2 → 2 8 9 6 2 7
1 8 3 4 2 9 → 9 2 4 3 8 1
7 4 6 2 3 1 → 1 3 2 6 4 7
9 2 3 6 1 5 → 5 1 6 3 2 9
4 2 9 3 1 8 → 8 1 3 9 2 4
   <배열>       <연산 결과>

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

1 6 2 9 8 4 → 4 9 7 1 7 1
7 2 6 9 8 2 → 2 2 4 8 2 6
1 8 3 4 2 9 → 9 3 6 3 6 2
7 4 6 2 3 1 → 3 6 2 4 9 9
9 2 3 6 1 5 → 1 1 3 2 8 8
4 2 9 3 1 8 → 8 5 1 9 2 4
   <배열>       <연산 결과>

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

1 6 2 9 8 4 → 4 2 9 1 5 8
7 2 6 9 8 2 → 8 8 2 3 1 1
1 8 3 4 2 9 → 9 9 4 2 6 3
7 4 6 2 3 1 → 2 6 3 6 3 9
9 2 3 6 1 5 → 6 2 8 4 2 2
4 2 9 3 1 8 → 1 7 1 7 9 4
   <배열>       <연산 결과>

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

1 1 1 1 2 2 2 2
1 1 1 1 2 2 2 2
1 1 1 1 2 2 2 2
4 4 4 4 3 3 3 3
4 4 4 4 3 3 3 3
4 4 4 4 3 3 3 3

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

3 2 6 3 1 2 9 7 → 2 1 3 8 3 2 6 3
9 7 8 2 1 4 5 3 → 1 3 2 8 9 7 8 2
5 9 2 1 9 6 1 8 → 4 5 1 9 5 9 2 1
2 1 3 8 6 3 9 2 → 6 3 9 2 1 2 9 7
1 3 2 8 7 9 2 1 → 7 9 2 1 1 4 5 3
4 5 1 9 8 2 1 3 → 8 2 1 3 9 6 1 8
     <배열>            <연산 결과>

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

3 2 6 3 1 2 9 7 → 1 2 9 7 6 3 9 2
9 7 8 2 1 4 5 3 → 1 4 5 3 7 9 2 1
5 9 2 1 9 6 1 8 → 9 6 1 8 8 2 1 3
2 1 3 8 6 3 9 2 → 3 2 6 3 2 1 3 8
1 3 2 8 7 9 2 1 → 9 7 8 2 1 3 2 8
4 5 1 9 8 2 1 3 → 5 9 2 1 4 5 1 9
     <배열>            <연산 결과>


입력

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

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

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

6 8 6
3 2 6 3 1 2 9 7
9 7 8 2 1 4 5 3
5 9 2 1 9 6 1 8
2 1 3 8 6 3 9 2
1 3 2 8 7 9 2 1
4 5 1 9 8 2 1 3
1 2 3 4 5 6


출력

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

3 1 2 8 9 1 5 4
1 2 9 7 8 2 3 1
2 9 3 6 8 3 1 2
8 1 6 9 1 2 9 5
3 5 4 1 2 8 7 9
7 9 2 1 3 6 2 3


접근 방식

각 케이스에 대해 함수를 만들고 그 안에 temp 배열을 생성해서 구현



코드

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

public class Main_S1_16935 {
	
	static int N,M;
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		N = Integer.parseInt(st.nextToken());
		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());
			}
		}
		
		// 명령 받기
		st= new StringTokenizer(br.readLine());
		
		// 명령 수행
		for(int i=0;i<R;i++) {
			int func = Integer.parseInt(st.nextToken());
			
			switch (func) {
			case 1:
				arr = func1(arr);
				break;
			
			case 2:
				arr = func2(arr);
				break;
				
			case 3:
				int temp = M;
				M = N;
				N = temp;
				arr = func3(arr);
				break;
				
			case 4:
				temp = M;
				M = N;
				N = temp;
				arr = func4(arr);
				break;
			
			
			case 5:
				arr = func5(arr);
				break;
				
			case 6:
				arr = func6(arr);
				break;
			}
		}
		
		// 배열 출력
		StringBuilder sb = new StringBuilder();
		
		for(int i=0;i<arr.length;i++) {
			for(int j=0;j<arr[0].length;j++) {
				sb.append(arr[i][j]).append(" ");
			}
			sb.setLength(sb.length()-1);
			sb.append("\n");
		}
		sb.setLength(sb.length()-1);
		System.out.print(sb);
		
			
		
	}
	
	// 상하 반전
	public static int[][] func1(int arr[][]) {
		int[][] temp = new int[N][M];
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				temp[i][j] = arr[N-1-i][j];
			}
		}
		
		return temp;
	}
	
	// 좌우 반전
	public static int[][] func2(int arr[][]) {
		int[][] temp = new int[N][M];
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				temp[i][j] = arr[i][M-j-1];
			}
		}
		
		return temp;
	}
	
	// 시계방향 회전
	public static int[][] func3(int arr[][]) {
		int[][] temp = new int[N][M];
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				temp[i][j] = arr[M-1-j][i];
			}
		}
		
		return temp;
	}
	
	// 반시계방향 회전
	public static int[][] func4(int arr[][]) {
		int[][] temp = new int[N][M];
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				temp[i][j] = arr[j][N-1-i];
			}
		}
		
		return temp;
	}
	
	// 사분면 시계방향 회전
	public static int[][] func5(int arr[][]){
		int[][] temp = new int[N][M];
		
		
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				if(i < N/2 && j < M/2) temp[i][j+M/2] = arr[i][j];
				if(i < N/2 && j >= M/2) temp[i+N/2][j] = arr[i][j];
				if(i >= N/2 && j >= M/2) temp[i][j-M/2] = arr[i][j];
				if(i >= N/2 && j < M/2) temp[i-N/2][j] = arr[i][j];
				
			}
		}
		return temp;
	}
	
	// 사분면 반시계방향 회전
	public static int[][] func6(int arr[][]){
		int[][] temp = new int[N][M];
		
		
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				if(i < N/2 && j < M/2) temp[i+N/2][j] = arr[i][j];
				if(i < N/2 && j >= M/2) temp[i][j-M/2] = arr[i][j];
				if(i >= N/2 && j >= M/2) temp[i-N/2][j] = arr[i][j];
				if(i >= N/2 && j < M/2) temp[i][j+M/2] = arr[i][j];
				
			}
		}
		return temp;
	}

}
profile
꾸준함, 책임감

0개의 댓글