[문제링크]

 

21610번: 마법사 상어와 비바라기

마법사 상어는 파이어볼, 토네이도, 파이어스톰, 물복사버그 마법을 할 수 있다. 오늘 새로 배운 마법은 비바라기이다. 비바라기를 시전하면 하늘에 비구름을 만들 수 있다. 오늘은 비바라기

www.acmicpc.net

 

0. 단순 구현 문제

 

1. 구름을 이동하고 -> 대각선 물 양을 증가시키고 -> 새 구름을 생성한다 의 반복

 

2. 구름이 사라진 칸은 boolean 배열로 관리한다

 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main{
	
	static int[][] dir = new int[][]{
		{0,-1},{-1,-1},{-1,0},{-1,1},
		{0,1},{1,1},{1,0},{1,-1}
	};
	public static void main(String[] args)throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
		int n = pint(st.nextToken());
		int m = pint(st.nextToken());
		int sum=0;
		
		int[][]map = new int[n][n];
		boolean[][]chk;
		
		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(br.readLine());
			for (int j = 0; j < n; j++) {
				map[i][j]=pint(st.nextToken());
			}
		}
		
		Queue<int[]> cloud = new LinkedList<>();
		cloud.add(new int[] {n-1,0}); cloud.add(new int[] {n-1,1});
		cloud.add(new int[] {n-2,0}); cloud.add(new int[] {n-2,1});
		
		for (int i = 0; i < m; i++) {
			st = new StringTokenizer(br.readLine());
			int d = pint(st.nextToken())-1;
			int s = pint(st.nextToken());
			chk=new boolean[n][n];
			//구름 이동 페이즈
			int len = cloud.size();
			for (int j = 0; j < len; j++) {
				int[] cur = cloud.poll();
				int newX = cur[0]+dir[d][0]*s;
				int newY = cur[1]+dir[d][1]*s;
				newX = ((newX%n)+n)%n;
				newY = ((newY%n)+n)%n;
				//1증가
				map[newX][newY]++;
				cloud.add(new int[] {newX,newY});
			}
			
			//이동 후 물양 증가 페이즈
			for (int j = 0; j < len; j++) {
				int[] cur = cloud.poll();
				if(cur[0]-1>=0 && cur[1]-1>=0 && map[cur[0]-1][cur[1]-1]>0) {
					map[cur[0]][cur[1]]++;
				}if(cur[0]-1>=0 && cur[1]+1<n && map[cur[0]-1][cur[1]+1]>0) {
					map[cur[0]][cur[1]]++;
				}if(cur[0]+1<n && cur[1]-1>=0 && map[cur[0]+1][cur[1]-1]>0) {
					map[cur[0]][cur[1]]++;
				}if(cur[0]+1<n && cur[1]+1<n && map[cur[0]+1][cur[1]+1]>0) {
					map[cur[0]][cur[1]]++;
				}
				chk[cur[0]][cur[1]]=true;
			}
			
			//신규 구름 생성 페이즈
			
			for (int j = 0; j < n; j++) {
				for (int j2 = 0; j2 < n; j2++) {
					if(!chk[j][j2] && map[j][j2]>=2) {
						map[j][j2]-=2;
						cloud.add(new int[] {j,j2});
					}
				}
			}
		}
		for (int x = 0; x < n; x++) {
			for (int y = 0; y < n; y++) {
				sum+=map[x][y];
			}
		}System.out.println(sum);
		
	}
	
	static int pint(String s) {
		return Integer.parseInt(s);
	}
}

결과 화면

 

+ Recent posts