๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 11559_Puyo Puyo

๋ฟŒ์•ผ._. 2023. 8. 7. 14:17

Gold IV

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/11559)

< Puyo Puyo >

 

๋ฌธ์ œ ํ’€์ด 

 

ํ•„๋“œ ๋ฐ”๋‹ฅ๋ถ€ํ„ฐ bfs ํƒ์ƒ‰์„ ํ†ตํ•ด ๊ฐ™์€ ์ƒ‰ ๋ฟŒ์š”๊ฐ€ 4๊ฐœ ์ด์ƒ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ๋ถ€๋ถ„์„ ์ฐพ๋Š”๋‹ค.

์ฐพ์€ ๋ถ€๋ถ„์„ ํ„ฐํŠธ๋ฆฐ ํ›„ ๋นˆ ๊ณต๊ฐ„์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.

์ค‘๋ ฅ์„ ํ†ตํ•ด ์•„๋ž˜๋กœ ๋–จ์–ด์ง€๋„๋ก ๋งŒ๋“œ๋Š” ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•ด ์ค€๋‹ค.

 

 

 my solution (Java)

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

public class _11559_ { // Puyo Puyo
	static int dx[] = { -1, 1, 0, 0 };
	static int dy[] = { 0, 0, -1, 1 };
	static char arr[][];
	static int cnt, result;

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		arr = new char[12][6];
		boolean[][] visited = new boolean[12][6];
		boolean[][] check = new boolean[12][6];

		for (int i = 0; i < 12; i++) {
			String str = bf.readLine();
			for (int j = 0; j < 6; j++) {
				arr[i][j] = str.charAt(j);
			}
		}

		result = 0;
		boolean flag = false;

		while (true) {
			for (int i = 11; i >= 0; i--) {
				for (int j = 0; j < 6; j++) {
					if (arr[i][j] != '.' && !visited[i][j]) {
						bfs(i, j, arr[i][j], visited, check);
						if (cnt >= 4) {
							flag = true;
							boom(check);
						}
						init(check);
					}
				}
			}
			if (!flag) {
				break;
			} else {
				result += 1;
				flag = false;
			}

			init(visited);
			gravity();
		}
		System.out.println(result);
	}

	private static void gravity() {
		for (int i = 0; i < 6; i++) {
			int idx = -1;
			for (int j = 11; j >= 0; j--) {
				if (idx == -1 & arr[j][i] == '.') {
					idx = j;
				} else if (idx != -1 && arr[j][i] != '.') {
					arr[idx][i] = arr[j][i];
					arr[j][i] = '.';
					idx -= 1;
				}
			}
		}
	}

	private static void bfs(int i, int j, char color, boolean visited[][], boolean check[][]) {
		Queue<int[]> queue = new LinkedList<>();
		queue.add(new int[] { i, j });
		visited[i][j] = true;
		check[i][j] = true;
		cnt = 1;

		while (!queue.isEmpty()) {
			int temp[] = queue.poll();
			for (int k = 0; k < 4; k++) {
				int x = temp[0] + dx[k];
				int y = temp[1] + dy[k];
				if (x >= 0 && x < 12 && y >= 0 && y < 6 && !visited[x][y] && arr[x][y] == color) {
					visited[x][y] = true;
					check[x][y] = true;
					queue.add(new int[] { x, y });
					cnt += 1;
				}
			}
		}
	}

	private static void boom(boolean[][] check) {
		for (int i = 0; i < 12; i++) {
			for (int j = 0; j < 6; j++) {
				if (check[i][j]) {
					arr[i][j] = '.';
				}
			}
		}
	}

	private static void init(boolean[][] check) {
		for (int i = 0; i < 12; i++) {
			for (int j = 0; j < 6; j++) {
				check[i][j] = false;
			}
		}
	}
}

 

Main

๋ณ€์ˆ˜)
arr : ํ•„๋“œ ์ •๋ณด
visited : ์ „์ฒด ํ•„๋“œ ํƒ์ƒ‰ ํ›„ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€
check : ๊ฐ™์€ ์ƒ‰ ๋ฟŒ์š” ํƒ์ƒ‰ ์—ฌ๋ถ€
result : ์—ฐ์‡„ ํšŸ์ˆ˜
flag : ์—ฐ์‡„ ์ž‘์šฉ ์—ฌ๋ถ€
cnt : ์ƒํ•˜์ขŒ์šฐ ๊ฐ™์€ ์ƒ‰ ๋ฟŒ์š” ๊ฐœ์ˆ˜

 

- ํ•„๋“œ ์ •๋ณด ์ž…๋ ฅ ํ›„ ์ €์žฅ(arr)

- ์—ฐ์‡„ ์ž‘์šฉ์ด ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต

: ๋ฐ”๋‹ฅ๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•ด์„œ  ๋นˆ ๊ณต๊ฐ„์ด ์•„๋‹ˆ๊ณ  ์•„์ง ๋ฐฉ๋ฌธํ•˜์ง€ ์•Š์•˜๋‹ค๋ฉด bfsํ•จ์ˆ˜ ํ˜ธ์ถœ

: bfs ํƒ์ƒ‰ ํ›„ ๊ฐ™์€ ์ƒ‰ ๋ฟŒ์š”๊ฐ€ 4๊ฐœ ์ด์ƒ์ด๋ผ๋ฉด boom ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜์—ฌ ํ„ฐํŠธ๋ฆฌ๊ธฐ

: ๊ฐ™์€ ์ƒ‰ ๋ฟŒ์š” ํƒ์ƒ‰ ์—ฌ๋ถ€๋ฅผ ํŒ๋‹จํ•˜๋Š” chek ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”

: ์—ฐ์‡„์ž‘์šฉ์ด ์—†์—ˆ๋‹ค๋ฉด ์ข…๋ฃŒ

: ์—ฐ์‡„์ž‘์šฉ์ด ์žˆ์—ˆ๋‹ค๋ฉด ์—ฐ์‡„ ํšŸ์ˆ˜(result) +1 ํ›„ ์ „์ฒด ๋ฐฉ๋ฌธ ์—ฌ๋ถ€(visited) ์ดˆ๊ธฐํ™” ๋ฐ ์ค‘๋ ฅ ์ž‘์šฉ์„ ์œ„ํ•ด gravity ํ•จ์ˆ˜ ํ˜ธ์ถœ

-  ์ •๋‹ต(result) ์ถœ๋ ฅ

 

gravity

- ๋ฐ”๋‹ฅ๋ถ€ํ„ฐ ์—ด๋ผ๋ฆฌ ํƒ์ƒ‰ํ•˜์—ฌ ๋นˆ ๊ณต๊ฐ„์ด ์žˆ๋‹ค๋ฉด ๋ฟŒ์š”๋ฅผ ๋‚ด๋ ค์คŒ

 

bfs

- ์ƒํ•˜์ขŒ์šฐ ์—ฐ๊ฒฐ๋œ ๊ณณ์ด ๊ฐ™์€ ๋ฟŒ์š” ์ƒ‰์ด๋ผ๋ฉด ๋ฐฉ๋ฌธ ํ‘œ์‹œ ๋ฐ queue์— ์ถ”๊ฐ€, ์ƒํ•˜์ขŒ์šฐ ๊ฐ™์€ ์ƒ‰ ๋ฟŒ์š” ๊ฐœ์ˆ˜(cnt) +1

 

boom

- ๋ฟŒ์š”๋ฅผ ํ„ฐํŠธ๋ ค ๋นˆ ๊ณต๊ฐ„์œผ๋กœ ๋งŒ๋“ฆ

 

init

- ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ์ดˆ๊ธฐํ™”



'๐ŸŒžAlgorithm > ๐Ÿ”ฅBaekjoon' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 1726_๋กœ๋ด‡  (0) 2023.08.08
[Baekjoon] 5464_์ฃผ์ฐจ์žฅ  (0) 2023.08.08
[Baekjoon] 15828_Router  (0) 2023.08.07
[Baekjoon] 2161_์นด๋“œ1  (0) 2023.08.07
[Baekjoon] 2146_๋‹ค๋ฆฌ ๋งŒ๋“ค๊ธฐ  (0) 2023.08.03