Algorithm/백준(BOJ)

[백준] 20061. 모노미노도미노2 (Java)

Carroti 2022. 10. 6. 12:29

 

문제

 

20061번: 모노미노도미노 2

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

www.acmicpc.net

 

알고리즘

구현, 시뮬레이션

 

풀이

크기 [4][6]의 파란색 보드 배열과 크기 [6][4]의 초록색 보드 배열을 만들고 각 보드마다 아래 작업을 수행한다.

1. 블록 놓기

2. 가득 찬 줄이 있다면 점수 증가 후 제거하고 블록 밀기

3. 연한 색 보드에 블록이 있는 줄의 수만큼 제거하고 블록 밀기

 

문제에 나와있는 대로 구현만 하면 되는 문제이다.

구현을 잘못하면 결과로 시간 초과가 나올 수 있다.

블록을 밀 때 이전 위치를 0으로 바꿔주지 않았더니 예제는 모두 맞았고 틀렸습니다가 아닌 시간 초과가 발생했다.

 

코드

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

public class Main {
	static int N, blueBoard[][], greenBoard[][], score;

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;

		N = Integer.parseInt(br.readLine());
		blueBoard = new int[4][6];
		greenBoard = new int[6][4];
		score = 0;
		for (int i = 0; i < N; i++) {
			st = new StringTokenizer(br.readLine(), " ");
			int t = Integer.parseInt(st.nextToken());
			int y = Integer.parseInt(st.nextToken());
			int x = Integer.parseInt(st.nextToken());

			pushBlue(t, y, x);
			popBlue();
			removeBlue();

			pushGreen(t, y, x);
			popGreen();
			removeGreen();
		}

		int count = 0;
		for (int i = 0; i < 4; i++) {
			for (int j = 2; j < 6; j++) {

				if (blueBoard[i][j] == 1)
					count++;

				if (greenBoard[j][i] == 1)
					count++;
			}
		}

		System.out.println(score);
		System.out.println(count);
	}

	private static void removeGreen() {
		int lightCount = getlightGreenCount();
		if (lightCount == 0)
			return;

		for (int i = 0; i < 4; i++) {
			for (int j = 5; j >= lightCount; j--) {
				greenBoard[j][i] = greenBoard[j - lightCount][i];
				greenBoard[j - lightCount][i] = 0;
			}
		}
	}

	private static int getlightGreenCount() {

		int count = 0;

		for (int i = 0; i <= 1; i++) {
			for (int j = 0; j <= 3; j++) {
				if (greenBoard[i][j] == 1) {
					count++;
					break;
				}
			}
		}

		return count;
	}

	private static void popGreen() {

		label1: for (int i = 5; i >= 2; i--) {
			for (int j = 0; j <= 3; j++) {
				if (greenBoard[i][j] == 0)
					continue label1;
			}

			score++;
			pushGreen(i);
			i++;
		}
	}

	private static void pushGreen(int y) {
		for (int i = 0; i < 4; i++) {
			for (int j = y; j >= 1; j--) {
				greenBoard[j][i] = greenBoard[j - 1][i];
				greenBoard[j - 1][i] = 0;
			}
		}
	}

	private static void pushGreen(int t, int y, int x) {

		if (t == 1) {
			int ny = 0;
			while (true) {
				if (++ny > 5 || greenBoard[ny][x] == 1)
					break;
			}
			greenBoard[ny - 1][x] = 1;
		} else if (t == 2) {
			int ny = 0;
			while (true) {
				if (++ny > 5 || greenBoard[ny][x] == 1 || greenBoard[ny][x + 1] == 1)
					break;
			}

			greenBoard[ny - 1][x] = 1;
			greenBoard[ny - 1][x + 1] = 1;
		} else if (t == 3) {
			int ny = 1;
			while (true) {
				if (++ny > 5 || greenBoard[ny][x] == 1)
					break;
			}

			greenBoard[ny - 1][x] = 1;
			greenBoard[ny - 2][x] = 1;
		}
	}

	static void removeBlue() {
		int lightCount = getlightBlueCount();
		if (lightCount == 0)
			return;

		for (int i = 0; i < 4; i++) {
			for (int j = 5; j >= lightCount; j--) {
				blueBoard[i][j] = blueBoard[i][j - lightCount];
				blueBoard[i][j - lightCount] = 0;
			}
		}
	}

	static int getlightBlueCount() {

		int count = 0;

		for (int i = 0; i <= 1; i++) {
			for (int j = 0; j <= 3; j++) {
				if (blueBoard[j][i] == 1) {
					count++;
					break;
				}
			}
		}

		return count;
	}

	private static void popBlue() {

		label1: for (int i = 5; i >= 2; i--) {
			for (int j = 0; j <= 3; j++) {
				if (blueBoard[j][i] == 0)
					continue label1;
			}

			score++;
			pushBlue(i);
			i++;
		}
	}

	static void pushBlue(int x) {
		for (int i = 0; i < 4; i++) {
			for (int j = x; j >= 1; j--) {
				blueBoard[i][j] = blueBoard[i][j - 1];
				blueBoard[i][j - 1] = 0;
			}
		}
	}

	static void pushBlue(int t, int y, int x) {

		if (t == 1) {
			int nx = 0;
			while (true) {
				if (++nx > 5 || blueBoard[y][nx] == 1)
					break;
			}

			blueBoard[y][nx - 1] = 1;
		} else if (t == 2) {
			int nx = 1;
			while (true) {
				if (++nx > 5 || blueBoard[y][nx] == 1)
					break;
			}
			blueBoard[y][nx - 1] = 1;
			blueBoard[y][nx - 2] = 1;
		} else if (t == 3) {
			int nx = 0;
			while (true) {
				if (++nx > 5 || blueBoard[y][nx] == 1 || blueBoard[y + 1][nx] == 1)
					break;
			}
			blueBoard[y][nx - 1] = 1;
			blueBoard[y + 1][nx - 1] = 1;
		}
	}
}