문제
https://www.acmicpc.net/problem/16935
문제 풀이
배열 돌리기 2의 상위 문제다.
총 6가지의 연산이 있는데,
1번과 2번은 각각 상하반전과 좌우 반전이다.
1번과 2번은 각각 가운데 행을 기준으로 끝과 끝을 바꿔주면 된다.
3번과 4번은 반시계와 시계방향으로 90도 회전하는 것이다.
여기서 중요한 점은 시계방향으로 한번 돌릴 때마다 N과 M이 바뀐다는 점이다.
따라서 해당 연산을 한 후 N과 M을 바꿔줘야 다음 연산에서 index 오류가 발생하지 않는다.
오른쪽 90도 회전인 경우
- 첫 번째 행을 첫번째 열로, 두 번째 행을 두번째 열로 ,,, n번째 행을 n번쨰 열로 옮겨주면 된다.
왼쪽 90도 회전인 경우
- 마지막 행을 첫번째 열로, 마지막 -1 번째 행을 두번째 열로 ,,, 0번째 행을 n번째 열로 옮겨주면 된다.
5번과 6번의 경우 배열을 4개로 분할해서 분할한 배열을 옮겨준다.
그림과 같이 배열을 통째로 구역에 옮겨주면 된다.
처음에 문제를 잘못 읽어서 R을 각 연산의 횟수로 알아들어서 굉장히 복잡하게 풀어서
코드가 너무 난잡한 것 같다..ㅠ
전체 코드
import java.io.*;
import java.util.*;
public class Main {
static BufferedReader br;
static StringTokenizer st;
static int n;
static int m;
static int r;
static int opt;
static int[][] graph;
static int[][] after;
static int[][] graph1;
static int[][] graph2;
static int[][] graph3;
static int[][] graph4;
static int maxNum;
static int[] operand;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
input();
int temp;
for (int i = 0; i < r; i++) {
opt = operand[i];
if (opt <= 2) {
rotateReverse();
} else if (opt <= 4) {
rotate90();
temp = n;
n = m;
m = temp;
} else if (opt <= 6) {
divideGraph();
rotateSpecial();
}
}
printGraph();
}
public static void input() throws IOException {
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
r = Integer.parseInt(st.nextToken());
maxNum = Math.max(n, m);
graph = new int[maxNum][maxNum];
after = new int[maxNum][maxNum];
operand = new int[r];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine(), " ");
for (int j = 0; j < m; j++) {
graph[i][j] = Integer.parseInt(st.nextToken());
}
}
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < r; i++) {
operand[i] = Integer.parseInt(st.nextToken());
}
}
public static void rotateReverse() { //상하 , 좌우
int a;
if (opt == 1) {
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < m; j++) {
a = graph[i][j];
graph[i][j] = graph[n - 1 - i][j]; // 거꾸로 넣기
graph[n - 1 - i][j] = a;
}
}
} else if (opt == 2) {
for (int i = 0; i < m / 2; i++) {
for (int j = 0; j < n; j++) {
a = graph[j][i];
graph[j][i] = graph[j][m - 1 - i];
graph[j][m - 1 - i] = a;
}
}
}
}
public static void rotate90() {
int a;
after = new int[m][n];
if (opt == 3) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
after[j][n - 1 - i] = graph[i][j];
}
}
}
if (opt == 4) { //왼쪽으로 90도
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
after[m - 1 - j][i] = graph[i][j];
}
}
}
graph = after;
}
public static void divideGraph() {
//4분면 나누기
graph1 = new int[maxNum / 2][maxNum / 2];
graph2 = new int[maxNum / 2][maxNum / 2];
graph3 = new int[maxNum / 2][maxNum / 2];
graph4 = new int[maxNum / 2][maxNum / 2];
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < m / 2; j++) {
graph1[i][j] = graph[i][j];
}
}
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < m / 2; j++) {
graph2[i][j] = graph[i][m / 2 + j];
}
}
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < m / 2; j++) {
graph3[i][j] = graph[n / 2 + i][j];
}
}
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < m / 2; j++) {
graph4[i][j] = graph[n / 2 + i][m / 2 + j];
}
}
}
public static void assembleGraph(int[][] one, int[][] two, int[][] three, int[][] four) {
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < m / 2; j++) {
graph[i][j] = one[i][j];
}
}
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < m / 2; j++) {
graph[i][m / 2 + j] = two[i][j];
}
}
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < m / 2; j++) {
graph[n / 2 + i][j] = three[i][j];
}
}
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < m / 2; j++) {
graph[n / 2 + i][m / 2 + j] = four[i][j];
}
}
}
public static void rotateSpecial() {
if (opt == 5) {
assembleGraph(graph3, graph1, graph4, graph2); //1->2 2->3 3->4 4->1
} else {
assembleGraph(graph2, graph4, graph1, graph3); //1->4 2->1 3->2 4->3
}
}
public static void printGraph() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print(graph[i][j] + " ");
}
System.out.println();
}
}
}
'CodingTest > Baekjoon' 카테고리의 다른 글
[백준 17070] - 파이프 옮기기1(JAVA)[골드5] (0) | 2024.01.19 |
---|---|
[백준 17406] - 배열돌리기4(JAVA)[골드4] (0) | 2024.01.19 |
[백준 16927] - 배열 돌리기2(JAVA)골드5] (1) | 2024.01.15 |
[백준 17471] - 게리멘더링(JAVA)[골드4] (1) | 2024.01.15 |
[백준 2887] - 행성터널(Java)[플레5] (0) | 2024.01.13 |