-
[java] 백준 16935 : 배열 돌리기 3알고리즘 2023. 7. 30. 18:32
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.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.*; public class Main { private static int[][] arr; private static int h; private static int w; private static int r; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); StringTokenizer st = new StringTokenizer(br.readLine()); h = Integer.parseInt(st.nextToken()); w = Integer.parseInt(st.nextToken()); r = Integer.parseInt(st.nextToken()); arr = new int[h][w]; for(int i=0; i<h; i++) { st = new StringTokenizer(br.readLine()); for(int j=0; j<w; j++) { arr[i][j] = Integer.parseInt(st.nextToken()); } } String[] num = br.readLine().split(" "); for(int menu=0; menu<num.length; menu++) { switch(num[menu]) { case "1": //상하반전 upDown(); break; case "2": //좌우반전 leftRight(); break; case "3": //오른쪽 90도 arr = right90(); break; case "4": //왼쪽 90도 arr = left90(); break; case "5": //4그룹 분할 후 시계방향 한 번 회전 quadClockRotation(); break; case "6": //4그룹 분할 후 반시계방향 회전 quadAntiClockRotation(); break; } } StringBuilder sb = new StringBuilder(); for(int i=0; i<h; i++) { for(int j=0; j<w; j++) { sb.append(arr[i][j]).append(" "); } sb.append("\n"); } System.out.println(sb); } private static void upDown() { int halfh = h/2; for(int i=0; i<halfh; i++) { for(int j=0; j<w; j++) { int temp = arr[i][j]; arr[i][j] = arr[h-1-i][j]; arr[h-1-i][j]= temp; } } } private static void leftRight() { int halfw = w/2; for(int i=0; i<h; i++) { for(int j=0; j<halfw; j++) { int temp = arr[i][j]; arr[i][j] = arr[i][w-1-j]; arr[i][w-1-j] = temp; } } } private static int[][] right90() { int[][] newArr = new int[w][h]; for(int i=0; i<w; i++) { int[] add = new int[h]; for(int j=0; j<h; j++) { add[j] = arr[h-1-j][i]; } newArr[i] = add; } int temp = w; w = h; h = temp; return newArr; } private static int[][] left90() { int[][] newArr = new int[w][h]; for(int i=0; i<w; i++) { int[] add = new int[h]; for(int j=0; j<h; j++) { add[j] = arr[j][w-1-i]; } newArr[i] = add; } int temp = w; w = h; h= temp; return newArr; } private static void quadClockRotation() { int halfw = w/2; int halfh = h/2; int[][] a = new int[halfh][halfw]; int[][] b = new int[halfh][halfw]; int[][] c = new int[halfh][halfw]; int[][] d = new int[halfh][halfw]; //북서, 북동, 남서, 남동 구역 나누기 for(int i=0; i<h; i++) { for(int j=0; j<w; j++) { if(i<halfh && j<halfw) { a[i][j] = arr[i][j]; } else if(i<halfh && j>= halfw) { b[i][j-halfw] = arr[i][j]; } else if (i>=halfh && j<halfw) { c[i-halfh][j] = arr[i][j]; } else { d[i-halfh][j-halfw] = arr[i][j]; } } } //시계방향 돌리기 for(int i=0; i<h; i++) { for(int j=0; j<w; j++) { if(i<halfh && j<halfw) { arr[i][j] = c[i][j]; } else if(i<halfh && j>= halfw) { arr[i][j] = a[i][j-halfw]; } else if (i>=halfh && j<halfw) { arr[i][j] = d[i-halfh][j]; } else { arr[i][j] = b[i-halfh][j-halfw]; } } } } private static void quadAntiClockRotation() { int halfw = w/2; int halfh = h/2; int[][] a = new int[halfh][halfw]; int[][] b = new int[halfh][halfw]; int[][] c = new int[halfh][halfw]; int[][] d = new int[halfh][halfw]; //북서, 북동, 남서, 남동 구역 나누기 for(int i=0; i<h; i++) { for(int j=0; j<w; j++) { if(i<halfh && j<halfw) { a[i][j] = arr[i][j]; } else if(i<halfh && j>= halfw) { b[i][j-halfw] = arr[i][j]; } else if (i>=halfh && j<halfw) { c[i-halfh][j] = arr[i][j]; } else { d[i-halfh][j-halfw] = arr[i][j]; } } } //반시계방향 돌리기 for(int i=0; i<h; i++) { for(int j=0; j<w; j++) { if(i<halfh && j<halfw) { arr[i][j] = b[i][j]; } else if(i<halfh && j>= halfw) { arr[i][j] = d[i][j-halfw]; } else if (i>=halfh && j<halfw) { arr[i][j] = a[i-halfh][j]; } else { arr[i][j] = c[i-halfh][j-halfw]; } } } } }
'알고리즘' 카테고리의 다른 글
[java] 프로그래머스 lv2 전력망을 둘로 나누기 (0) 2023.08.06 프로그래머스 게임 맵 최단거리 (0) 2023.08.05 [java] 백준 16926 : 배열 돌리기 1 (0) 2023.07.27 [java] 프로그래머스 예상 대진표 (0) 2023.07.11 [java] 프로그래머스 키패드 누르기 (0) 2023.07.06