728x90
문제에서 원하는 회전에 대한, 각각의 연산을 수행해 주면 된다.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static StringBuilder sb = new StringBuilder();
static StringTokenizer st;
static int N,M,R,c[],map[][];
private static void dataInput() throws IOException {
st= new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken()); // 세로
M = Integer.parseInt(st.nextToken()); // 가로
R = Integer.parseInt(st.nextToken()); // 명령갯수
map = new int[N][M];
for(int i=0; i<N; i++) {
st = new StringTokenizer(br.readLine());
for(int j=0; j<M; j++){
map[i][j] = Integer.parseInt(st.nextToken());
}
}
c = new int[R];
st = new StringTokenizer(br.readLine());
for(int k=0; k<R; k++) {
c[k]=Integer.parseInt(st.nextToken());
}
}
private static void calculation1() {
for(int i=0; i<N/2; i++) {
for(int j=0; j<M; j++) {
int tmp = map[i][j];
map[i][j]=map[N-1-i][j];
map[N-1-i][j] = tmp;
}
}
}
private static void calculation2() {
for(int i=0; i<N; i++) {
for(int j=0; j<M/2; j++) {
int tmp = map[i][j];
map[i][j]=map[i][M-1-j];
map[i][M-1-j] = tmp;
}
}
}
private static int[][] calculation3() {
int[][] tmp = new int[M][N];
for(int i=0; i<N; i++) {
for(int j=0; j<M; j++) {
tmp[j][N-1-i] = map[i][j];
}
}
int temp = M;
M = N;
N = temp;
return tmp;
}
private static int[][] calculation4() {
int[][] tmp = new int[M][N];
for(int i=0; i<N; i++) {
for(int j=0; j<M; j++) {
tmp[M-1-j][i] = map[i][j];
}
}
int temp = M;
M = N;
N = temp;
return tmp;
}
private static int[][] calculation5() {
int[][] tmp = new int[N][M];
int[] dy = {0 , 0 , N/2 , N/2 };
int[] dx = {0 , M/2, M/2 , 0 };
int[] my = {0 , N/2, 0 , -N/2};
int[] mx = {M/2, 0 ,-M/2 , 0 };
for(int d=0; d<4; d++) {
for(int i=dy[d]; i<N/2+dy[d]; i++) {
for(int j=dx[d]; j<M/2+dx[d]; j++) {
tmp[i+my[d]][j+mx[d]] = map[i][j];
}
}
}
return tmp;
}
private static int[][] calculation6() {
int[][] tmp = new int[N][M];
int[] dy = {0 , 0 , N/2 , N/2 };
int[] dx = {0 , M/2, M/2 , 0 };
int[] my = {N/2, 0 , -N/2, 0 };
int[] mx = { 0 , -M/2, 0 ,M/2};
for(int d=0; d<4; d++) {
for(int i=dy[d]; i<N/2+dy[d]; i++) {
for(int j=dx[d]; j<M/2+dx[d]; j++) {
tmp[i+my[d]][j+mx[d]] = map[i][j];
}
}
}
return tmp;
}
public static void main(String[] args) throws IOException {
dataInput();
for(int rotate : c) {
switch(rotate) {
case 1:
calculation1();
break;
case 2:
calculation2();
break;
case 3:
map = calculation3();
break;
case 4:
map = calculation4();
break;
case 5:
map = calculation5();
break;
case 6:
map = calculation6();
break;
default:
break;
}
}
for(int i=0; i<N; i++) {
for(int j=0; j<M; j++) {
System.out.print(map[i][j] + " ");
}
System.out.println();
}
}
}
728x90
'[Algorithms] > [Problems]' 카테고리의 다른 글
BOJ. 6593번. 상범빌딩 (0) | 2021.02.18 |
---|---|
BOJ. 17406번. 배열 돌리기 4 (0) | 2021.02.14 |
BOJ. 16927번. 배열 돌리기 2 (0) | 2021.02.14 |
BOJ. 16926번. 배열 돌리기 1 (0) | 2021.02.14 |
BOJ. 10799번. 쇠막대기 (0) | 2021.02.14 |