728x90
https://www.acmicpc.net/problem/16935
package a0811;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Solution_BOJ배열돌리기3_서울_12반_이서영 {
static int n,m,r;
static StringTokenizer st;
static int[][]arr;
public static void swap(int x1, int y1, int x2, int y2) {
int temp = arr[x1][y1];
arr[x1][y1]=arr[x2][y2];
arr[x2][y2]=temp;
}
public static void one() { //상하반전
int k = n/2;
for(int i=0; i<k; i++) {
int[] temp = Arrays.copyOf(arr[i], m);
arr[i]= Arrays.copyOf(arr[n-1-i],m);
arr[n-1-i]= Arrays.copyOf(temp,m);
}
}
public static void two() { //좌우반전
int k = m/2;
for(int j=0; j<k; j++) {
for(int i=0; i<n; i++) {
swap(i,j,i,m-1-j);
}
}
}
public static void three() { //오른쪽 90도 회전
int[][] narr = new int[m][n];
for(int i=0; i<n; i++) {
int[] temp = Arrays.copyOf(arr[i], m);
for(int j=0; j<m; j++) {
narr[j][n-1-i]=temp[j];
}
}
arr= narr;
}
public static void four() { //왼쪽 90도 회전
int[][] narr = new int[m][n];
for(int i=0; i<n; i++) {
int[] temp = Arrays.copyOf(arr[i], m);
for(int j=0; j<m; j++) {
narr[m-1-j][i]=temp[j];
}
}
arr= narr;
}
public static void five() { // 4등분 복사
int hn = n/2;
int hm = m/2;
int [][]temp = new int[hn][hm];
for(int i=0; i<hn; i++) {
temp[i]=Arrays.copyOfRange(arr[i], hm, m);//2번복사
}
for(int i=0; i<hn; i++) {
for(int j=hm; j<m; j++) {
arr[i][j] = arr[i][j-hm]; //1번을 2번자리에
}
}
for(int i=0; i<hn; i++) { //4번을 1번자리에
for(int j=0; j<hm; j++) {
arr[i][j]=arr[i+hn][j];
}
}
for(int i=hn; i<n; i++) { //3번을 4번자리에
for(int j=0; j<hm; j++) {
arr[i][j]= arr[i][hm+j];
}
}
for(int i=hn; i<n; i++) { //복사한 2번을 3번에
for(int j=hm; j<m; j++) {
arr[i][j] = temp[i-hn][j-hm];
}
}
}
public static void six() {
int hn = n/2;
int hm = m/2;
int [][]temp = new int[hn][hm];
for(int i=0; i<hn; i++) {
temp[i]=Arrays.copyOfRange(arr[i], hm, m);//2번복사
}
for(int i=0; i<hn; i++) {
for(int j=hm; j<m; j++) {
arr[i][j] = arr[i+hn][j]; //3번을 2번자리에
}
}
for(int i=hn; i<n; i++) { //4번을 3번에
for(int j=hm; j<m; j++) {
arr[i][j] =arr[i][j-hm];
}
}
for(int i=hn; i<n; i++) { //1번을 4번자리에
for(int j=0; j<hm; j++) {
arr[i][j]= arr[i-hn][j];
}
}
for(int i=0; i<hn; i++) { //복사한 2번을 1번자리에
for(int j=0; j<hm; j++) {
arr[i][j]=temp[i][j];
}
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
st= new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
r = Integer.parseInt(st.nextToken());
int t = Math.max(n, m);
arr = new int[t][t];
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 num = Integer.parseInt(st.nextToken());
if(num==1) {
one();
}
else if(num==2) {two(); }
else if(num==3) {three(); int tmp = n; n=m; m=tmp;} // 회전하면 가로 세로길이 서로 바뀜
else if(num==4) {four();int tmp = n; n=m; m=tmp;} //회전하면 가로세로길이 서로 바뀜
else if(num==5) {five();}
else if(num==6) { six();}
}
StringBuilder sb = new StringBuilder();
for(int i=0; i<n; i++) {
//sb.append()
for(int j=0; j<m; j++) {
sb.append(arr[i][j]+" ");
}
sb.append("\n");
}
System.out.println(sb);
}
}
'백준 > 구현' 카테고리의 다른 글
배열돌리기시리즈 (0) | 2021.09.29 |
---|---|
배열돌리기1-16926번(java) (0) | 2021.08.11 |
색종이- 2563번(java) (0) | 2021.08.11 |
요세푸스 문제 - 1158번 (java) (0) | 2021.08.11 |
스위치 켜고 끄기 (0) | 2021.08.02 |