본문 바로가기

ALGORITHM

백준 16935 배열 돌리기 3 (Java)

1. 문제 링크

 

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

 

2. 문제 및 입출력예제

 

3. 문제 풀이

문제가 요구하는 연산이 6개나 되기 때문에 각각을 메서드로 구현했다.

기존 배열의 원소를 새로운 배열의 원소로 넣고 출력했다.

 

1, 2번 연산은 반전이기 때문에 원하는 위치는 배열 길이 - 지금 원소의 인덱스 -1을 하면된다.

1번 연산은 x좌표의 값을 원하는 위치에 넣어주면 된다.

2번 연산은 y좌표의 값을 원하는 위치에 넣어주면 된다.

 

3, 4번 연산은 회전이기 때문에 x좌표와 y좌표의 위치를 바꾸면 된다.

3번 연산은 오른쪽 90도 회전이기 때문에 x좌표의 값을 1번 연산처럼 하면 된다.

4번 연산은 왼쪽 90도 회전이기 때문에 y좌표의 값을 2번 연산처럼 하면 된다.

여기서 주의할 점은 회전을 할 경우 배열의 N과 M이 바뀌기 때문에

새로운 배열의 크기부터 연산 후 N과 M을 바꾸는 과정까지 잊지 않고 해야 한다.

 

5, 6번 연산은 구역을 4개로 나눠서 진행해야 한다.

이중 포문을 이용해 나눠진 구역 각각에서 연산을 진행하면 된다.

더 효율적으로 짤 수 있을 것 같기도 하다... 

 

4. 코드

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	private static StringBuilder sb = new StringBuilder();
	private static int N, M, R;
	private static int[][] map;
	
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

		String[] split = br.readLine().split(" ");
		N = Integer.parseInt(split[0]);
		M = Integer.parseInt(split[1]);
		R = Integer.parseInt(split[2]);
		map = new int[N][M];
		for(int i=0;i<N;i++) {
			String[] split2 = br.readLine().split(" ");
			for(int j=0;j<M;j++) {
				map[i][j] = Integer.parseInt(split2[j]);
			}
		}
		
		String[] r = br.readLine().split(" ");		
		for(int i=0;i<R;i++) {
			switch(r[i]) {
			case "1":
				one();
				break;
			case "2":
				second();
				break;
			case "3":
				third();
				break;
			case "4":
				four();
				break;
			case "5":
				five();
				break;
			case "6":
				six();
				break;
			}
		}
		
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				if(map[i][j]!=0)
					sb.append(map[i][j]+" ");
			}
			sb.append("\n");
		}
		System.out.println(sb);
	}

	private static void one() {
		int[][] map2 = new int[N][M];
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				map2[N-i-1][j] = map[i][j];
			}
		}
		map = map2;
	}
	
	private static void second() {
		int[][] map2 = new int[N][M];
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				map2[i][M-j-1] = map[i][j];
			}
		}
		map = map2;	
	}
	
	private static void third() {
		int[][] map2 = new int[M][N];
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				map2[j][N-i-1] = map[i][j];
			}
		}
		int temp = N;
		N = M;
		M = temp;
		map = map2;
	}

	private static void four() {
		int[][] map2 = new int[M][N];
		for(int i=0;i<N;i++) {
			for(int j=0;j<M;j++) {
				map2[M-j-1][i] = map[i][j];
			}
		}
		int temp = N;
		N = M;
		M = temp;
		map = map2;
		map = map2;
	}
	
	private static void five() {
		int[][] map2 = new int[N][M];
		int[] dx = {0, N/2, 0, -N/2};
		int[] dy = {M/2, 0, -M/2, 0};
		for(int i=0;i<N/2;i++) {
			for(int j=0;j<M/2;j++) {
				map2[i+dx[0]][j+dy[0]] = map[i][j];
			}
		}
		for(int i=0;i<N/2;i++) {
			for(int j=M/2;j<M;j++) {
				map2[i+dx[1]][j+dy[1]] = map[i][j];
			}
		}
		for(int i=N/2;i<N;i++) {
			for(int j=M/2;j<M;j++) {
				map2[i+dx[2]][j+dy[2]] = map[i][j];
			}
		}
		for(int i=N/2;i<N;i++) {
			for(int j=0;j<M/2;j++) {
				map2[i+dx[3]][j+dy[3]] = map[i][j];
			}
		}
		map = map2;
	}
	
	private static void six() {
		int[][] map2 = new int[N][M];
		int[] dx = {N/2, 0, -N/2, 0};
		int[] dy = {0, M/2, 0, -M/2};
		for(int i=0;i<N/2;i++) {
			for(int j=0;j<M/2;j++) {
				map2[i+dx[0]][j+dy[0]] = map[i][j];
			}
		}
		for(int i=N/2;i<N;i++) {
			for(int j=0;j<M/2;j++) {
				map2[i+dx[1]][j+dy[1]] = map[i][j];
			}
		}
		for(int i=N/2;i<N;i++) {
			for(int j=M/2;j<M;j++) {
				map2[i+dx[2]][j+dy[2]] = map[i][j];
			}
		}
		for(int i=0;i<N/2;i++) {
			for(int j=M/2;j<M;j++) {
				map2[i+dx[3]][j+dy[3]] = map[i][j];
			}
		}
		map = map2;
	}
	
}

 

'ALGORITHM' 카테고리의 다른 글

백준 11286 절댓값 힙 (Java)  (0) 2023.02.25
백준 23971 ZOAC 4 (Java)  (0) 2023.02.24
백준 16926 배열 돌리기 1 (Java)  (0) 2023.02.23
백준 13458 시험 감독 (Java)  (0) 2023.02.22
백준 1991 트리 순회 (Java)  (0) 2023.02.21