BJ16926 배열 돌리기 1

·2022년 4월 17일
0

백준 알고리즘

목록 보기
2/34

https://www.acmicpc.net/problem/16926

  1. 문제를 읽고, 배열이 돌아가는 구조를 이해한다.
  2. 각 입력을 변수와 배열에 저장한다.

다음과 같이 반시계 방향으로 각 도넛 모양의 묶음으로 배열이이 회전하게 된다.

이를 구현하기 위해서 큐에 각 도넛을 넣어준 후, 회전할 때마다 머리를 꼬리에 이어주는 방식을 사용했다.

큐를 공부한 직후라 활용했던 것으로 기억하는데,

큐를 사용하지 않고, 배열의 인덱스를 판별해 단순하게 한칸씩 당겨주는 방법이 훨씬 효율적이었다.

package day0209;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class RotateArray {
	static BufferedReader br;
	static BufferedWriter bw;
	static StringTokenizer st;
	static int[][] map;
	static int N, M, R, count = 0, full;
	static LinkedList[] LL;

	// 연결리스트를 Math.min(N,M) / 2개만큼 만들어 회전할 때, 큐를 이용함.
	public static void makeLL(int round, int x, int y, int dir) {
		if (count >= ((N - 1 - (2 * round)) + (M - 1 - (2 * round))) * 2) {
			round++;
			x = round;
			y = round;
			count = 0;
		}
		if (round == full)
			return;
		if (dir == 0) {
			if (y + 1 < M - round) {
				LL[round].add(map[x][y]);
				count++;
				makeLL(round, x, y + 1, dir);
				return;
			} else if (y + 1 >= M - round) {
				dir++;
			}
		} else if (dir == 1) {
			if (x + 1 < N - round) {
				LL[round].add(map[x][y]);
				count++;
				makeLL(round, x + 1, y, dir);
				return;
			} else if (x + 1 >= N - round) {
				dir++;
			}
		} else if (dir == 2) {
			if (y - 1 >= 0 + round) {
				LL[round].add(map[x][y]);
				count++;
				makeLL(round, x, y - 1, dir);
				return;
			} else if (y - 1 < 0 + round) {
				dir++;
			}
		} else if (dir == 3) {
			if (x - 1 >= 0 + round) {
				LL[round].add(map[x][y]);
				count++;
				makeLL(round, x - 1, y, dir);
				return;
			} else if (x - 1 < 0 + round) {
				dir -= 3;
			}
		}
		makeLL(round, x, y, dir);
	}
	static void Rotate() {
		for(int i = 0; i < full; i++) {
			for(int j = 0; j < R; j++) {
				LL[i].offer((LL[i].poll()));
			}
		}
	}
	// 회전된 큐를 다시 배열로 돌려놓는 과정.
	static void draw(int round, int x, int y, int dir) {
		if(LL[round].isEmpty()) {
			round++;
			x = round;
			y = round;
		}
		if (round == full)
			return;
		if (dir == 0) {
			if (y + 1 < M - round) {
				map[x][y] = (int) LL[round].poll();
				draw(round, x, y + 1, dir);
				return;
			} else if (y + 1 >= M - round) {
				dir++;
			}
		} else if (dir == 1) {
			if (x + 1 < N - round) {
				map[x][y] = (int) LL[round].poll();
				draw(round, x + 1, y, dir);
				return;
			} else if (x + 1 >= N - round) {
				dir++;
			}
		} else if (dir == 2) {
			if (y - 1 >= 0 + round) {
				map[x][y] = (int) LL[round].poll();
				draw(round, x, y - 1, dir);
				return;
			} else if (y - 1 < 0 + round) {
				dir++;
			}
		} else if (dir == 3) {
			if (x - 1 >= 0 + round) {
				map[x][y] = (int) LL[round].poll();
				draw(round, x - 1, y, dir);
				return;
			} else if (x - 1 < 0 + round) {
				dir -= 3;
			}
		}
		draw(round, x, y, dir);
	}
	static void print() {
		for(int i = 0; i < N; i++) {
			for(int j = 0; j < M; j++) {
				System.out.print(map[i][j] + " ");
			}
			System.out.println();
		}
		
	}
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		bw = new BufferedWriter(new OutputStreamWriter(System.out));

		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];
		if (N < M) {
			full = N / 2;
		} else {
			full = M / 2;
		}
		LL = new LinkedList[full];
		for (int i = 0; i < full; i++) {
			LL[i] = new LinkedList<Integer>();
		}
		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());
			}
		}
		makeLL(0,0,0,0);
		Rotate();
		
/*		System.out.println(LL[0]);
		System.out.println(LL[1]);*/
		
		draw(0,0,0,0);
		print();
		bw.flush();
		bw.close();
	}

}
profile
SSAFY 7기

0개의 댓글