본문 바로가기

백준/삼성기출

마법사상어와 블리자드

728x90

문제

마법사 상어는 파이어볼토네이도, 파이어스톰, 물복사버그, 비바라기 마법을 할 수 있다. 오늘 새로 배운 마법은 블리자드이고, 크기가 N×N인 격자에서 연습하려고 한다. N은 항상 홀수이고, (r, c)는 격자의 r행 c열을 의미한다. 격자의 가장 왼쪽 윗 칸은 (1, 1)이고, 가장 오른쪽 아랫 칸은 (N, N)이며 마법사 상어는 ((N+1)/2, (N+1)/2)에 있다.

일부 칸과 칸 사이에는 벽이 세워져 있으며, 다음은 N = 3, 5, 7인 경우의 예시이다. 실선은 벽이고, 점선은 벽이 아니다. 칸에 적혀있는 수는 칸의 번호이다.

     
N = 3 N = 5 N = 7

가장 처음에 상어가 있는 칸을 제외한 나머지 칸에는 구슬이 하나 들어갈 수 있다. 구슬은 1번 구슬, 2번 구슬, 3번 구슬이 있다. 같은 번호를 가진 구슬이 번호가 연속하는 칸에 있으면, 그 구슬을 연속하는 구슬이라고 한다. 다음은 N = 7인 경우 예시이다. 

블리자드 마법을 시전하려면 방향 di와 거리 si를 정해야 한다. 총 4가지 방향 ↑, ↓, ←, →가 있고, 정수 1, 2, 3, 4로 나타낸다. 상어는 di 방향으로 거리가 si 이하인 모든 칸에 얼음 파편을 던져 그 칸에 있는 구슬을 모두 파괴한다. 구슬이 파괴되면 그 칸은 구슬이 들어있지 않은 빈 칸이 된다. 얼음 파편은 벽의 위로 떨어지기 때문에, 벽은 파괴되지 않는다.

다음 예시는 방향은 아래, 거리는 2인 경우이다.

   
빨간색으로 표시된 칸에 얼음 파편이 떨어진다. 구슬이 파괴된 후

만약 어떤 칸 A의 번호보다 번호가 하나 작은 칸이 빈 칸이면, A에 있는 구슬은 그 빈 칸으로 이동한다. 이 이동은 더 이상 구슬이 이동하지 않을 때까지 반복된다. 따라서, 구슬이 파괴된 후에는 빈 칸이 생겨 구슬이 이동하고, 구슬이 모두 이동한 결과는 다음과 같다.

이제 구슬이 폭발하는 단계이다. 폭발하는 구슬은 4개 이상 연속하는 구슬이 있을 때 발생한다. 다음은 왼쪽 그림은 위의 상태에서 폭발하는 구슬이 들어있는 칸을 파란색과 초록색으로 표시한 것이고, 오른쪽 그림은 구슬이 폭발한 후의 상태이다.

   
구슬이 폭발하기 전 구슬이 폭발한 후

구슬이 폭발해 빈 칸이 생겼으니 다시 구슬이 이동한다. 구슬이 이동한 후에는 다시 구슬이 폭발하는 단계이고, 이 과정은 더 이상 폭발하는 구슬이 없을때까지 반복된다. 구슬이 폭발한 후의 상태에서 구슬이 이동하면 다음과 같다.

위의 상태는 4개 이상 연속하는 구슬이 있기 때문에 구슬이 다시 폭발하게 된다.

   
구슬이 폭발하기 전 구슬이 폭발하고 이동한 후

이제 더 이상 폭발한 구슬이 없기 때문에, 구슬이 변화하는 단계가 된다. 연속하는 구슬은 하나의 그룹이라고 한다. 다음은 1번 구슬은 빨간색, 2번 구슬은 파란색, 3번 구슬은 보라색으로 표시한 그림이다.

하나의 그룹은 두 개의 구슬 A와 B로 변한다. 구슬 A의 번호는 그룹에 들어있는 구슬의 개수이고, B는 그룹을 이루고 있는 구슬의 번호이다. 구슬은 다시 그룹의 순서대로 1번 칸부터 차례대로 A, B의 순서로 칸에 들어간다. 다음 그림은 구슬이 변화한 후이고, 색은 구분하기 위해 위의 그림에 있는 그룹의 색을 그대로 사용했다. 만약, 구슬이 칸의 수보다 많아 칸에 들어가지 못하는 경우 그러한 구슬은 사라진다.

 

마법사 상어는 블리자드를 총 M번 시전했다. 시전한 마법의 정보가 주어졌을 때, 1×(폭발한 1번 구슬의 개수) + 2×(폭발한 2번 구슬의 개수) + 3×(폭발한 3번 구슬의 개수)를 구해보자.

입력

첫째 줄에 N, M이 주어진다. 둘째 줄부터 N개의 줄에는 격자에 들어있는 구슬의 정보가 주어진다. r번째 행의 c번째 정수는 (r, c)에 들어있는 구슬의 번호를 의미한다. 어떤 칸에 구슬이 없으면 0이 주어진다. 상어가 있는 칸도 항상 0이 주어진다.

다음 M개의 줄에는 블리자드 마법의 방향 di와 거리 si가 한 줄에 하나씩 마법을 시전한 순서대로 주어진다.

출력

첫째 줄에 1×(폭발한 1번 구슬의 개수) + 2×(폭발한 2번 구슬의 개수) + 3×(폭발한 3번 구슬의 개수)를 출력한다.

package Samsung;

import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.*;

public class 마법사상어와블리자드 { //2시간 4분
	
	 
	static int n,m;
	static int[][] arr;
	static int[] dx = {0,-1,1,0,0};
	static int[] dy = {0,0,0,-1,1};
	
	static int[] lx = {0,1,0,-1}; //왼 아래 오 위
	static int[] ly = {-1,0,1,0};
 	static int sx,sy;
	static ArrayList<Integer>slist = new  ArrayList<>();
	static ArrayList<Pair>elist = new ArrayList<>();
	
	static int one =0;
	static int two =0;
	static int three =0;
	
	static class Pair {
		
		int x,y;
		Pair(int x, int y){
			this.x =x;
			this.y = y;
		}
	}
	
	public static boolean cango(int x,int y ) {
		
		return x>=0 && x<n && y>=0 && y<n;
	}
	
	public static void print() {
		
		
		for(int i=0; i<n; i++) {
			
			for(int j=0; j<n; j++) {
				System.out.print(arr[i][j]+" ");
			}
			System.out.println();
		}
		System.out.println();
		
	}
	
	
	
	public static boolean explode() {
		
		
		
		int x = sx;
		int y = sy;
		
		int c=1;
		boolean tg = true;
		boolean[][] check = new boolean[n][n];
		ArrayList<Pair>list = new ArrayList<>();
		int num =arr[x][y-1];
		while(true) {
			
			for(int i=0; i<2; i++)
			{
				int t =c;
				while(t>0) {
				int nx = x+lx[i];
				int ny = y+ly[i];
				if(!cango(nx,ny)) {tg = false; break;}
				if(arr[nx][ny]!=0&& !check[nx][ny] ) {
			
					if(arr[nx][ny]==num) {
						list.add(new Pair(nx,ny));
						
					}
					else {
						
						if(list.size()>=4) {
							elist.addAll(list);
							
						}
						list.clear();
						num = arr[nx][ny];
						list.add(new Pair(nx,ny));
					}
					
					check[nx][ny]=true;
					
				}
				t--;
				x=nx;
				y=ny;
				}
			}			
			
			if(!tg)break;
			c++;
			
			for(int i=2; i<4; i++)
			{
				int t =c;
				while(t>0) {
				
				int nx = x+lx[i];
				int ny = y+ly[i];
				if(!cango(nx,ny)) {tg = false; break;}
				if(arr[nx][ny]!=0&&!check[nx][ny]) {
					if(arr[nx][ny]==num) {
						list.add(new Pair(nx,ny));
						
					}
					else {
						
						if(list.size()>=4) {
							elist.addAll(list);
							
						}
						list.clear();
						num = arr[nx][ny];
						list.add(new Pair(nx,ny));
					}
					
					check[nx][ny]=true;
				
				}
				t--;
				x=nx;
				y=ny;
				}
			}	
			
			if(!tg)break;
			c++;
			
			
		}
		
		if(list.size()>=4) {
			elist.addAll(list); //마지막에 리스트에 남은게 4개 이상이면 이것도 explode대상에 넣어줘야한다 !! -> 이거 때문에 첨에 틀렸음 4개이상인데 4개일떄라고 함
		}
		
		
		for(Pair p :elist) {
			
			if(arr[p.x][p.y]==1)one++;
			else if(arr[p.x][p.y]==2)two++;
			else if(arr[p.x][p.y]==3)three++;
			arr[p.x][p.y]=0;
			
		}
		
		//print();
		
		
		
		putAgain();
		
		//print();
		
		if(elist.size()>0) {
			return true;
		}
		
		return false;
	}
	
	
	public static void putAgain() {
		
		int x = sx;
		int y = sy;
		
		int c=1;
		boolean tg = true;
		boolean[][] check = new boolean[n][n];
		slist.clear();
		
		while(true) {
			
			for(int i=0; i<2; i++)
			{
				int t =c;
				while(t>0) {
				int nx = x+lx[i];
				int ny = y+ly[i];
				if(!cango(nx,ny)) {tg = false; break;}
				if(arr[nx][ny]!=0&& !check[nx][ny]) {
			//		System.out.println(nx+ ","+ny);
					check[nx][ny]=true;
					slist.add(arr[nx][ny]);
				}
				t--;
				x=nx;
				y=ny;
				}
			}			
			
			if(!tg)break;
			c++;
			
			for(int i=2; i<4; i++)
			{
				int t =c;
				while(t>0) {
				
				int nx = x+lx[i];
				int ny = y+ly[i];
				if(!cango(nx,ny)) {tg = false; break;}
				if(arr[nx][ny]!=0&&!check[nx][ny]) {
			//		System.out.println(nx+ ","+ny);
					check[nx][ny]=true;
					slist.add(arr[nx][ny]);
				}
				t--;
				x=nx;
				y=ny;
				}
			}	
			
			if(!tg)break;
			c++;
			
			
		}
		
		 x = sx;
		 y = sy;
		 
		 int[][] tmp = new int[n][n];
		 
		  c=1;
			 tg = true;
			int idx =0;

			while(true) {
				
				//System.out.println(slist.size());
				
				for(int i=0; i<2; i++)
				{
					int t =c;
					while(t>0) {
					int nx = x+lx[i];
					int ny = y+ly[i];
					if(!cango(nx,ny)) {tg = false; break;}
					if(idx>=slist.size()) {tg=false; break;}
					tmp[nx][ny]=slist.get(idx);
					idx++;
					t--;
					x=nx;
					y=ny;
					}
				}			
				
				if(!tg)break;
				c++;
				
				for(int i=2; i<4; i++)
				{
					int t =c;
					while(t>0) {
					
					int nx = x+lx[i];
					int ny = y+ly[i];
					if(!cango(nx,ny)) {tg = false; break;}
					if(idx>=slist.size()) {tg=false; break;}
					tmp[nx][ny]=slist.get(idx);
					idx++;
					t--;
					x=nx;
					y=ny;
					}
				}	
				
				if(!tg)break;
				c++;
				
				
			}
			
			for(int i=0; i<n; i++) {
				for(int j=0; j<n; j++) {
					
					arr[i][j]=tmp[i][j];
				}
			}
		
		
	}
	
	public static void divide() { //구슬 A,B 로나누어서 다시 배치하기 
		
		
		int x = sx;
		int y = sy;
		
		int c=1;
		boolean tg = true;
		boolean[][] check = new boolean[n][n];
		ArrayList<Pair>list = new ArrayList<>();
		ArrayList<Integer>totalnums = new ArrayList<>();
		
		int num =arr[x][y-1];
		while(true) {
			
			for(int i=0; i<2; i++)
			{
				int t =c;
				while(t>0) {
				int nx = x+lx[i];
				int ny = y+ly[i];
				if(!cango(nx,ny)) {tg = false; break;}
				if(arr[nx][ny]!=0&& !check[nx][ny] ) {
			
					if(arr[nx][ny]==num) {
						list.add(new Pair(nx,ny));
						
					}
					else {
						
						if(list.size()>0) {
						
							totalnums.add(list.size());
							totalnums.add(num);
						}
						list.clear();
						num = arr[nx][ny];
						list.add(new Pair(nx,ny));
					}
					
					check[nx][ny]=true;
					
				}
				t--;
				x=nx;
				y=ny;
				}
			}			
			
			if(!tg)break;
			c++;
			
			for(int i=2; i<4; i++)
			{
				int t =c;
				while(t>0) {
				
				int nx = x+lx[i];
				int ny = y+ly[i];
				if(!cango(nx,ny)) {tg = false; break;}
				if(arr[nx][ny]!=0&&!check[nx][ny]) {
					if(arr[nx][ny]==num) {
						list.add(new Pair(nx,ny));
						
					}
					else {
						
						if(list.size()>0) {
							totalnums.add(list.size());
							totalnums.add(num);
							
						}
						list.clear();
						num = arr[nx][ny];
						list.add(new Pair(nx,ny));
					}
					
					check[nx][ny]=true;
				
				}
				t--;
				x=nx;
				y=ny;
				}
			}	
			
			if(!tg)break;
			c++;
			
			
		}
		
		if(list.size()>0) { //마지막에 리스트에 남은 것도 마저넣어주어야한다.
			totalnums.add(list.size()); 
			totalnums.add(num);
		}
		


		 x = sx;
		 y = sy;
		 
		 int[][] tmp = new int[n][n];
		 check = new boolean[n][n];
		 
		  c=1;
			 tg = true;
			int idx =0;

			while(true) {
				
				//System.out.println(slist.size());
				
				for(int i=0; i<2; i++)
				{
					int t =c;
					while(t>0) {
					int nx = x+lx[i];
					int ny = y+ly[i];
					if(!cango(nx,ny)||(check[nx][ny]==true)) {tg = false; break;}
					if(idx>=totalnums.size()) {tg=false; break;}
					tmp[nx][ny]=totalnums.get(idx);
					check[nx][ny]=true;
					idx++;
					t--;
					x=nx;
					y=ny;
					}
				}			
				
				if(!tg)break;
				c++;
				
				for(int i=2; i<4; i++)
				{
					int t =c;
					while(t>0) {
					
					int nx = x+lx[i];
					int ny = y+ly[i];
					if(!cango(nx,ny)||(check[nx][ny]==true)) {tg = false; break;}
					if(idx>=totalnums.size()) {tg=false; break;}
					tmp[nx][ny]=totalnums.get(idx);
					check[nx][ny]=true;
					idx++;
					t--;
					x=nx;
					y=ny;
					}
				}	
				
				if(!tg)break;
				c++;
				
				
			}
			
			for(int i=0; i<n; i++) {
				for(int j=0; j<n; j++) {
					
					arr[i][j]=tmp[i][j];
				}
			}
		
		
		
		
	}
	
	
	public static void simulate(int d, int s) { 
		
		int nx = sx+dx[d];
		int ny = sy+dy[d];
		
		
		while(cango(nx,ny) && s>0) {
			
			arr[nx][ny]=0;
			nx+=dx[d];
			ny+=dy[d];
			s--;
		}
		

		putAgain();
		
		
	}
	

	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());
		
		sx = n/2;
		sy =n/2;
		arr = new int[n][n];
		
		for(int i=0; i<n; i++) {
			st = new StringTokenizer(br.readLine());
			for(int j=0; j<n; j++) {
				
				arr[i][j]= Integer.parseInt(st.nextToken());
				
			}
			
		}
		
		for(int i=0; i<m; i++) {
		st = new StringTokenizer(br.readLine());
		int d = Integer.parseInt(st.nextToken());
		int s = Integer.parseInt(st.nextToken());
		simulate(d,s);
		while(explode()) {
			
			elist.clear();
		}

		divide();

		}
		
		System.out.println(one+2*two+3*three);
		
	}
	
	
}

- 달팽이 모양으로 탐색하는 것이 인상적이었던 문제이다. 

'백준 > 삼성기출' 카테고리의 다른 글

연구소3 (Java)  (0) 2021.10.02
인구이동(JAVA)  (0) 2021.10.02
로봇청소기  (0) 2021.09.29
이차원배열과연산 - 17140번(JAVA)  (0) 2021.09.29
낚시왕 -17143번(JAVA)  (0) 2021.09.29