알고리즘/Java
[백준/JAVA] 16935번 : 배열돌리기3
_SIHA_
2022. 8. 14. 03:27
📖 문제 링크
https://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
✅ 최종 코드
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main_BJ_16935_배열돌리기3 {
static int N, M;
static int R;
static int[][] board;
static int[] temp;
static int[][] temp2;
static int k;
static int r;
static int c;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
StringBuilder sb = new StringBuilder();
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
R = Integer.parseInt(st.nextToken());
board = new int[N][M];
for(int i=0;i<N;i++) {
st = new StringTokenizer(br.readLine());
for(int j=0;j<M;j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(br.readLine());
for(int tc=0;tc<R;tc++) {
switch(st.nextToken()) {
case "1" :
temp = new int[M];
for(int i=0,j=N-1;i<N/2;i++,j--) {
temp = board[i];
board[i] = board[j];
board[j] = temp;
}
break;
case "2" :
int tmp;
for(int i=0;i<N;i++) {
for(int j=0;j<M/2;j++) {
tmp = board[i][j];
board[i][j] = board[i][M - j -1];
board[i][M - j -1] = tmp;
}
}
break;
case "3" :
//복사할배열
temp2 = new int[board[0].length][board.length];
int k = N-1;
for(int i=0;i<board.length;i++) {
for(int j=0;j<board[0].length;j++) {
temp2[j][k] = board[i][j];
}
k--;
}
tmp = N; //배열 크기가 달라지므로, 이후 연산들을 위해서는 꼭 변경해줘야함
N = M;
M = tmp;
board = temp2;
break;
case "4" :
//복사할배열
temp2 = new int[board[0].length][board.length];
for(int i=0;i<N;i++) {
for(int j=0;j<M;j++) {
temp2[M-j-1][i] = board[i][j];
}
}
tmp = N; //배열 크기가 달라지므로, 이후 연산들을 위해서는 꼭 변경해줘야함
N = M;
M = tmp;
board = temp2;
break;
case "5" :
r = board.length;
c = board[0].length;
temp2 = new int[r/2][c/2];
//첫번째 저장
for(int i=0;i<r/2;i++) {
for(int j=0;j<c/2;j++) {
temp2[i][j] = board[i][j];
}
}
//4 -> 1
for(int i=0,l=r/2;i<r/2;i++,l++) {
for(int j=0;j<c/2;j++) {
board[i][j] = board[l][j];
}
}
//3 -> 4
for(int i=r/2;i<r;i++) {
for(int j=0, l=c/2;j<c/2;j++, l++) {
board[i][j] = board[i][l];
}
}
//2 -> 3
for(int i=r/2, l=0;i<r;i++,l++) {
for(int j=c/2;j<c;j++) {
board[i][j] = board[l][j];
}
}
//temp -> 2
for(int i=0;i<r/2 ;i++) {
for(int j=c/2, l=0;j<c;j++, l++) {
board[i][j] = temp2[i][l];
}
}
break;
case "6" :
r = board.length;
c = board[0].length;
temp2 = new int[r/2][c/2];
//1번영역 임시저장
for(int i=0;i<r/2;i++) {
for(int j=0;j<c/2;j++) {
temp2[i][j] = board[i][j];
}
}
//2->1
for(int i=0;i<r/2;i++) {
for(int j=c/2;j<c;j++) {
board[i][j-c/2] = board[i][j];
}
}
//3->2
for(int i=r/2;i<r;i++) {
for(int j=c/2;j<c;j++) {
board[i-r/2][j] = board[i][j];
}
}
//4->3
for(int i=r/2;i<r;i++) {
for(int j=0;j<c/2;j++) {
board[i][j+c/2] = board[i][j];
}
}
//1->4
for(int i=0;i<r/2;i++) {
for(int j=0;j<c/2;j++) {
board[i+r/2][j] = temp2[i][j];
}
}
break;
}
}
for(int i=0;i<board.length;i++) {
for(int j=0;j<board[i].length;j++) {
sb.append(board[i][j] + " ");
}
sb.append("\n");
}
System.out.println(sb);
}//main
}//end class