알고리즘/알고리즘문풀

BOJ - 모노미노도미노2 20061번 (JAVA)

developer-ellen 2022. 4. 30. 00:58

❓ 문제 - 백준 모노미노도미노 20061번 - JAVA 풀이법

 

출처 

(https://www.acmicpc.net/problem/20061)

 

20061번: 모노미노도미노 2

모노미노도미노는 아래와 같이 생긴 보드에서 진행되는 게임이다. 보드는 빨간색 보드, 파란색 보드, 초록색 보드가 그림과 같이 붙어있는 형태이다. 게임에서 사용하는 좌표 (x, y)에서 x는 행,

www.acmicpc.net

 

 

📝 문제해결법

1. 이 문제는 구현로 풀었다.

  • 그린 보드와 파란보드를 각각 green, blue라는 2차원 배열로 관리하였다.
  • 만약 블록이 하나 떨어지면 moving_green(), moving_blue()라는 메소드를 통해 각 보드에 블록 떨어짐 처리를 한다.
  • 블록을 놓은 후 bomb()라는 메소드를 통해 각 보드에 맞춰 터지는 게 발생하는지, 그리고 발생한다면 보드를 끌어 당기는 작업을 수행한다.
  • check()를 통해 연한색으로 표시된 특별칸에 블록이 놓여있는지 체크해서 만약 블록이 존재한다면 밀어버리는 처리를 한다.

2. moving_green(), moving_blue()

 

  • 떨어지는 블록에 맞춰 떨어짐 처리를 한다.
  • t=1의 블록은 밑으로 끌어 당기거나 오른쪽으로 끌어당길 때는 상관이 없다.
  • t=2일 때 오른쪽으로 이동시킬 때는 (x, y+1)을 기준점으로 오른쪽이 빈공간일지 체크하면서 구현하고, 아래로 이동시킬 때는 (x,y), (x,y+1)이 둘 다 빈공간으로 이동가능한지 체크해야한다.
  • t=3일 때는 오른쪽으로 이동할 때 (x,y) (x+1, y)가 둘다 오른쪽으로 이동가능(빈공간)인지 체크해야하고, 아래로 이동할 때는 (x+1, y)만이 밑으로 이동가능한지(빈공간)만 체크하면 된다.

 

2. bomb()

  • bomb() 를 처리할 때 초록 보드는 맨 아래 부터 위쪽으로 이동하면서 해당 모든 열이 블록이 채워져 있는지 체크하고 만약 채워져 있다면 터짐 처리할 때 해당 행에서 위쪽으로 이동하면서 밑으로 한칸씩 당김 처리한다.
  • bomb() 를 처리할 때 파란 보드는 맨 오른쪽 부터 왼쪽쪽으로 이동하면서 해당 모든 행이 블록이 채워져 있는지 체크하고 만약 채워져 있다면 터짐 처리할 때 해당 열에서 왼쪽으로 이동하면서 오른쪽으로 당김 처리한다.

3. check()

  • 연한 칸에 채워져 있는지 확인하기 위해서 초록 보드는 블록이 존재하는 특별 지역의 갯수를 체크 해서 해당 갯수만큼 아래쪽으로 당김처리한다.
  • 연한 칸에 채워져 있는지 확인하기 위해서 파란 보드는 블록이 존재하는 특별 지역의 갯수를 체크 해서 해당 갯수만큼 오른쪽으로 당김처리한다.

 

 

💻 소스코드

// BOJ - 모노미노도미노2 (20061번)
// 구현

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main_20061 {

	public static int[][] blue;
	public static int[][] green;
	public static int score;
	// 북, 남, 서, 동
	public static int[] dx = {-1, 1, 0, 0};
	public static int[] dy = {0, 0, -1, 1};

	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = null;
		int n = Integer.parseInt(br.readLine());
		blue = new int[4][10];
		green = new int[10][4];
		score = 0;
		while(n-- > 0) {
			st = new StringTokenizer(br.readLine(), " ");
			int t = Integer.parseInt(st.nextToken());
			int x = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());

			// 움직임
			moving_green(t, x, y);
			moving_blue(t, x, y);
			//print();
			// 터짐 확인 및 터지기
			bomb();
			//print();

			// 연한 칸 확인
			check();
			//print();
		}

		int cnt = 0;
		for(int i=0;i<10;i++) {
			for(int j=0;j<4;j++) {
				cnt += green[i][j];
			}
		}


		for(int i=0;i<4;i++) {
			for(int j=0;j<10;j++) {
				cnt += blue[i][j];
			}
		}


		System.out.println(score);
		System.out.println(cnt);

	}

	public static void print() {
		for(int i=0;i<10;i++) {
			for(int j=0;j<4;j++) {
				System.out.print(green[i][j] +" ");
			}
			System.out.println();
		}
		System.out.println();

		for(int i=0;i<4;i++) {
			for(int j=0;j<10;j++) {
				System.out.print(blue[i][j] +" ");
			}
			System.out.println();
		}
		System.out.println();
	}

	public static void bomb() {
		// 초록 보드
		for(int i=9;i>=0;i--) {
			int cnt = 0;
			for(int j=0;j<4;j++) {
				cnt += green[i][j];
			}


			if(cnt == 4) {
				score++;
				for(int j=0;j<4;j++) {
					int nx = i;
					while(true) {
						nx += dx[0];
						if(nx < 0) break;
						green[nx+1][j] = green[nx][j]; 
					}

				}
				i++;
			}
		}
			// 파란 보드
				for(int j=9;j>=0;j--) {
					int cnt = 0;
					for(int i=0;i<4;i++) {
						cnt += blue[i][j];
					}

					if(cnt == 4) {
						score++;
						for(int i=0;i<4;i++) {
							int ny = j;
							while(true) {
								ny += dy[2];
								if(ny < 0) break;
								blue[i][ny+1] = blue[i][ny]; 
							}

						}
						j++; 
					}
				}
	}

	public static void check() {
		// 초록 보드
		int g_cnt =0;

		for(int i=4;i<6;i++) {
			for(int j=0;j<4;j++) {
				if(green[i][j] != 0) {
					g_cnt++;
					break;
				}
			}
		}

		if(g_cnt > 0) {
			for(int i=9;i>=4;i--) {
				for(int j=0;j<4;j++) {
					green[i][j] = green[i-g_cnt][j];
				}
			}

			// 특별 경계 지우기
			for(int i=4;i<6;i++) {
					for(int j=0;j<4;j++) {
						green[i][j] = 0;
					}
				}
		}


		// 파란 보드
		int b_cnt =0;
		for(int j=4;j<6;j++) {
			for(int i=0;i<4;i++) {
				if(blue[i][j] != 0) {
					b_cnt++;
					break;
				}
			}
		}

		if(b_cnt > 0) {
			for(int j=9;j>=4;j--) {
				for(int i=0;i<4;i++) {
					blue[i][j] = blue[i][j-b_cnt];
				}
			}

			// 특별 경계 지우기
				for(int j=4;j<6;j++) {
						for(int i=0;i<4;i++) {
							blue[i][j] = 0;
						}
					}

		}



	}


	public static void moving_blue(int t, int x, int y) {
		// 파란 보드
				if(t==1) {
					int nx = x;
					int ny = y;
					blue[x][y] = 0;
					while(true) {
						nx += dx[3];
						ny += dy[3];
						if(ny < 0 || ny >= 10 || blue[nx][ny] != 0) {
							nx -= dx[3];
							ny -= dy[3];
							blue[nx][ny] = 1;
							break;
						}
					}
				} else if(t==2) {

					int nx = x;
					int ny = y+1;
					blue[x][y] = 0;
					blue[x][y+1] = 0;
					while(true) {
						nx += dx[3];
						ny += dy[3];
						if(ny < 0 || ny >= 10 || blue[nx][ny] != 0) {
							nx -= dx[3];
							ny -= dy[3];
							blue[nx][ny-1] = 1;
							blue[nx][ny] = 1;
							break;
						}
					}
				} else {
					int nx = x;
					int ny = y;
					blue[x][y] = 0;
					blue[x+1][y] = 0;
					while(true) {
						nx += dx[3];
						ny += dy[3];
						if(ny < 0 || ny >= 10 || blue[nx][ny] != 0 || blue[nx+1][ny] != 0) {
							nx -= dx[3];
							ny -= dy[3];
							blue[nx][ny] = 1;
							blue[nx+1][ny] = 1;
							break;
						}
					}
				}
	}

	public static void moving_green(int t, int x, int y) {
		// 초록 보드
		if(t==1) {
			int nx = x;
			int ny = y;
			green[x][y] = 0;
			while(true) {
				nx += dx[1];
				ny += dy[1];
				if(nx < 0 || nx >= 10 || green[nx][ny] != 0) {
					nx -= dx[1];
					ny -= dy[1];
					green[nx][ny] = 1;
					break;
				}
			}
		} else if(t==2) {
			int nx = x;
			int ny = y;
			green[x][y] = 0;
			green[x][y+1] = 0;
			while(true) {
				nx += dx[1];
				ny += dy[1];
				if(nx < 0 || nx >= 10 || green[nx][ny] != 0 || green[nx][ny+1] != 0) {
					nx -= dx[1];
					ny -= dy[1];
					green[nx][ny] = 1;
					green[nx][ny+1] = 1;
					break;
				}
			}
		} else {
			int nx = x+1;
			int ny = y;
			green[x][y] = 0;
			green[x+1][y] = 0;
			while(true) {
				nx += dx[1];
				ny += dy[1];
				if(nx < 0 || nx >= 10 || green[nx][ny] != 0) {
					nx -= dx[1];
					ny -= dy[1];
					green[nx][ny] = 1;
					green[nx-1][ny] = 1;
					break;
				}
			}
		}


	}



}