슈콩

[BOJ] 17822 원판돌리기 본문

Algorithms/Baekjoon

[BOJ] 17822 원판돌리기

shukong 2025. 12. 12. 23:58

[문제]

 

 

[소스 코드]

import java.io.*;
import java.util.*;

public class Main {
	static int m;
	static int[][] map;
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		int t = Integer.parseInt(st.nextToken());
		map = new int[n+1][m];
		for(int i=1;i<=n;i++) {
			st = new StringTokenizer(br.readLine());
			for(int j=0;j<m;j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		for(int tc=0;tc<t;tc++) {
			st = new StringTokenizer(br.readLine());
			int x = Integer.parseInt(st.nextToken());
			int d = Integer.parseInt(st.nextToken());
			int k = Integer.parseInt(st.nextToken());
			for(int i=x;i<=n;i+=x) rotate(i,d,k);
			boolean check = false;
			double sum = 0;
			int cnt = 0;
			boolean[][] visit = new boolean[n+1][m];
			for(int i=1;i<=n;i++) {
				for(int j=0;j<m;j++) {
					if(map[i][j]==0) continue;
					int curr = map[i][j];
					if(curr==map[i][(j+1)%m]) 
						visit[i][j] = visit[i][(j+1)%m] = true;
					if(curr==map[i][(j+m-1)%m])
						visit[i][j] = visit[i][(j+m-1)%m] = true;
					if(i>1 && curr==map[i-1][j])
						visit[i][j] = visit[i-1][j] = true;
					if(i<n && curr==map[i+1][j])
						visit[i][j] = visit[i+1][j] = true;
					sum += curr;
					cnt++;
				}
			}
			for(int i=1;i<=n;i++) {
				for(int j=0;j<m;j++) {
					if(visit[i][j]) {
						map[i][j] = 0;
						check = true;
					}
				}
			}
			if(!check) {
				sum /= cnt;
				for(int i=1;i<=n;i++) {
					for(int j=0;j<m;j++) {
						if(map[i][j]==0) continue;
						if(map[i][j]>sum) map[i][j]--;
						else if(map[i][j]<sum) map[i][j]++;
					}
				}
			}
		}
		int result = 0;
		for(int i=1;i<=n;i++) {
			for(int j=0;j<m;j++) {
				result += map[i][j];
			}
		}
		System.out.println(result);
	}
	private static void rotate(int x,int d,int k) {
		int[] temp = map[x].clone();
        for (int j=0;j<m;j++) {
            if(d==0) map[x][j] = temp[(j+m-k) % m];
            else map[x][j] = temp[(j+k) % m];
        }
	}
}

 

회전 후, 주변 값 확인

'Algorithms > Baekjoon' 카테고리의 다른 글

[BOJ] 19236 청소년 상어  (0) 2025.12.17
[BOJ] 17825 주사위 윷놀이  (5) 2025.12.16
[BOJ] 17837 새로운 게임 2  (0) 2025.12.09
[BOJ] 17779 게리멘더링 2  (0) 2025.12.08
[BOJ] 17142 연구소3  (3) 2025.12.07