Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

코드와이

[BAEKJOON] 17822. 원판 돌리기 본문

acmicpc

[BAEKJOON] 17822. 원판 돌리기

코드와이 2021. 9. 26. 00:08

 

문제링크

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

 

17822번: 원판 돌리기

반지름이 1, 2, ..., N인 원판이 크기가 작아지는 순으로 바닥에 놓여있고, 원판의 중심은 모두 같다. 원판의 반지름이 i이면, 그 원판을 i번째 원판이라고 한다. 각각의 원판에는 M개의 정수가 적혀

www.acmicpc.net

 

디큐를 사용하면 정말 쉽게 풀 수 있는 문제

 

package acmicpc.Gold4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class 원판_돌리기 {

	static int n, m, map[][];
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		n = Integer.parseInt(st.nextToken());
		m = Integer.parseInt(st.nextToken());
		int T = Integer.parseInt(st.nextToken());
		map = new int[n + 2][m + 2];
		
		
		// 원판에 수를 채운다.
		for(int i = 1 ; i <= n ; i++) {
			
			st = new StringTokenizer(br.readLine());
			for(int j = 1 ; j <= m ; j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
			}
			
		}
		
		Deque<Integer> dequeue;
		for(int t = 0 ; t < T ; t++) {
			
			st = new StringTokenizer(br.readLine());
			
			int x = Integer.parseInt(st.nextToken());
			int d = Integer.parseInt(st.nextToken());
			int k = Integer.parseInt(st.nextToken());
			dequeue = new ArrayDeque<>();
			
			if(d == 0) {
				k = m - k;
			}
			
			for(int i = 1 ; i <= n ; i++) {
				if((i % x) == 0) {
					
					for(int j = 1 ; j <= m ; j++) {
						dequeue.add(map[i][(j + k - 1) % m + 1]);
					}
					
					for(int j = 1 ; j <= m ; j++) {
						map[i][j] = dequeue.poll();
					}
					
				}
			}

			remove();
		}
		
		
		int ans = 0;
		for(int i = 0 ; i <= n + 1 ; i++) {
			for(int j = 0 ; j <= m + 1 ; j++) {
				ans += map[i][j];
			}
		}
		
		System.out.println(ans);
		
	}
	
	public static void remove() {
		
		Queue<int[]> queue = new LinkedList<>();
		double sum = 0;
		double cnt = 0;
		
		for(int i = 1 ; i <= n ; i++) {
			
			for(int j = 1 ; j <= m ; j++) {
				
				if(map[i][j] == 0) continue;
				
				sum += map[i][j];
				if(map[i][j] > 0) cnt++;
				
				if(map[i][j] == map[i][j - 1] || map[i][j] == map[i][j + 1] ||
						map[i][j] == map[i - 1][j] || map[i][j] == map[i + 1][j]) {
					queue.add(new int[]{i,j});
				}
				
				if((j == 1 || j == m) && map[i][1] == map[i][m]) queue.add(new int[]{i,j});
				
			}
			
		}
		
		if(queue.isEmpty()) {
			
			double mean = sum / cnt;
			for(int i = 1 ; i <= n ; i++) {
				for(int j = 1 ; j <= m ; j++) {
					
					if(map[i][j] == 0) continue;
					
					if(map[i][j] > mean) map[i][j]--;
					else if(map[i][j] < mean) map[i][j]++;
				}
			}
			
		} else {
		
			while(!queue.isEmpty()) {
				int[] q = queue.poll();
				
				map[q[0]][q[1]] = 0;
			}
		}
		
	}
}

'acmicpc' 카테고리의 다른 글

[BAEKJOON] 1939. 중량 제한  (0) 2021.10.09
[BAEKJOON] 17825. 주사위 윷놀이  (0) 2021.09.26
[BAEKJOON] 19238. 스타트 택시  (0) 2021.09.26
[BAEKJOON] 20057 마법사 상어와 토네이도  (0) 2021.09.26
[BAEKJOON] 2661. 좋은 수열  (0) 2021.09.11