개발일지

백준 배열돌리기 본문

알고리즘

백준 배열돌리기

devbh 2019. 10. 21. 12:06

import java.util.Scanner;

public class Main_배열돌리기 {
	static int N, M, K, ans;
	static int[][] A;
	static int[][] order;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		N = sc.nextInt();
		M = sc.nextInt();
		K = sc.nextInt();
		A = new int[N + 2][M + 2];
		ans = Integer.MAX_VALUE;
		order = new int[K][3];
		for (int i = 1; i < N + 1; i++) {
			for (int j = 1; j < M + 1; j++) {
				A[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < K; i++) {
			for (int j = 0; j < 3; j++) {
				order[i][j] = sc.nextInt();
			}
		}
		for (int i = 0; i < K; i++) {
			rotate(order[i][0], order[i][1], order[i][2]);
		}
		for (int i = 1; i < N + 1; i++) {
			int sum = 0;
			for (int j = 1; j < M + 1; j++) {
				sum += A[i][j];
			}
			ans = Math.min(sum, ans);
		}
		System.out.println(ans);
		sc.close();
	}

	static int[] dx = { 0, 1, 0, -1 };
	static int[] dy = { 1, 0, -1, 0 };

	static void rotate(int r, int c, int s) {
		for (int i = 1; i <= s; i++) {
			int x = r - i;
			int y = c - i;
			int tempPre = A[x][y];
			int d = 0;
			int cnt = 0;
			int totalCnt = i * 2;
			while (d < 4) {
				x += dx[d];
				y += dy[d];
				int tempNex = A[x][y];
				A[x][y] = tempPre;
				tempPre = tempNex;
				cnt++;
				if (totalCnt <= cnt) {
					d++;
					cnt = 0;
				}
			}
		}
	}
}

'알고리즘' 카테고리의 다른 글

게리맨더링2  (0) 2019.11.14
Heap (수정 중)  (0) 2019.11.13
다익스트라 (수정중)  (0) 2019.11.13
Shortest Path DAG  (0) 2019.11.13
1249. [S/W - 보급로]  (0) 2019.11.12
Comments