๐Ÿ™‡โ€โ™€๏ธ[Gold IV] Puyo Puyo - 11559

๋ฌธ์ œ ๋งํฌ

์„ฑ๋Šฅ ์š”์•ฝ

๋ฉ”๋ชจ๋ฆฌ: 2024 KB, ์‹œ๊ฐ„: 0 ms

๋ถ„๋ฅ˜

๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰, ๊ทธ๋ž˜ํ”„ ์ด๋ก , ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰, ๊ตฌํ˜„, ์‹œ๋ฎฌ๋ ˆ์ด์…˜

์ œ์ถœ ์ผ์ž

2024๋…„ 2์›” 21์ผ 15:17:30

๋ฌธ์ œ ์„ค๋ช…

๋ฟŒ์š”๋ฟŒ์š”์˜ ๋ฃฐ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

ํ•„๋“œ์— ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์ƒ‰๊น”์˜ ๋ฟŒ์š”๋ฅผ ๋†“๋Š”๋‹ค. ๋ฟŒ์š”๋Š” ์ค‘๋ ฅ์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ ์•„๋ž˜์— ๋ฐ”๋‹ฅ์ด๋‚˜ ๋‹ค๋ฅธ ๋ฟŒ์š”๊ฐ€ ๋‚˜์˜ฌ ๋•Œ๊นŒ์ง€ ์•„๋ž˜๋กœ ๋–จ์–ด์ง„๋‹ค.

๋ฟŒ์š”๋ฅผ ๋†“๊ณ  ๋‚œ ํ›„, ๊ฐ™์€ ์ƒ‰ ๋ฟŒ์š”๊ฐ€ 4๊ฐœ ์ด์ƒ ์ƒํ•˜์ขŒ์šฐ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉด ์—ฐ๊ฒฐ๋œ ๊ฐ™์€ ์ƒ‰ ๋ฟŒ์š”๋“ค์ด ํ•œ๊บผ๋ฒˆ์— ์—†์–ด์ง„๋‹ค. ์ด๋•Œ 1์—ฐ์‡„๊ฐ€ ์‹œ์ž‘๋œ๋‹ค.

๋ฟŒ์š”๋“ค์ด ์—†์–ด์ง€๊ณ  ๋‚˜์„œ ์œ„์— ๋‹ค๋ฅธ ๋ฟŒ์š”๋“ค์ด ์žˆ๋‹ค๋ฉด, ์—ญ์‹œ ์ค‘๋ ฅ์˜ ์˜ํ–ฅ์„ ๋ฐ›์•„ ์ฐจ๋ก€๋Œ€๋กœ ์•„๋ž˜๋กœ ๋–จ์–ด์ง€๊ฒŒ ๋œ๋‹ค.

์•„๋ž˜๋กœ ๋–จ์–ด์ง€๊ณ  ๋‚˜์„œ ๋‹ค์‹œ ๊ฐ™์€ ์ƒ‰์˜ ๋ฟŒ์š”๋“ค์ด 4๊ฐœ ์ด์ƒ ๋ชจ์ด๊ฒŒ ๋˜๋ฉด ๋˜ ํ„ฐ์ง€๊ฒŒ ๋˜๋Š”๋ฐ, ํ„ฐ์ง„ ํ›„ ๋ฟŒ์š”๋“ค์ด ๋‚ด๋ ค์˜ค๊ณ  ๋‹ค์‹œ ํ„ฐ์ง์„ ๋ฐ˜๋ณตํ•  ๋•Œ๋งˆ๋‹ค 1์—ฐ์‡„์”ฉ ๋Š˜์–ด๋‚œ๋‹ค.

ํ„ฐ์งˆ ์ˆ˜ ์žˆ๋Š” ๋ฟŒ์š”๊ฐ€ ์—ฌ๋Ÿฌ ๊ทธ๋ฃน์ด ์žˆ๋‹ค๋ฉด ๋™์‹œ์— ํ„ฐ์ ธ์•ผ ํ•˜๊ณ  ์—ฌ๋Ÿฌ ๊ทธ๋ฃน์ด ํ„ฐ์ง€๋”๋ผ๋„ ํ•œ๋ฒˆ์˜ ์—ฐ์‡„๊ฐ€ ์ถ”๊ฐ€๋œ๋‹ค.

๋‚จ๊ทœ๋Š” ์ตœ๊ทผ ๋ฟŒ์š”๋ฟŒ์š” ๊ฒŒ์ž„์— ํ‘น ๋น ์กŒ๋‹ค. ์ด ๊ฒŒ์ž„์€ 1:1๋กœ ๋ถ™๋Š” ๋Œ€์ „๊ฒŒ์ž„์ด๋ผ ์ž˜ ์Œ“๋Š” ๊ฒƒ๋„ ์ค‘์š”ํ•˜์ง€๋งŒ, ์ƒ๋Œ€๋ฐฉ์ด ํ„ฐ๋œจ๋ฆฐ๋‹ค๋ฉด ์—ฐ์‡„๊ฐ€ ๋ช‡ ๋ฒˆ์ด ๋ ์ง€ ๋ฐ”๋กœ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋Š” ๋Šฅ๋ ฅ๋„ ํ•„์š”ํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์•„์ง ์‹ค๋ ฅ์ด ๋ถ€์กฑํ•˜์—ฌ ๋‚จ๊ทœ๋Š” ์ž๊ธฐ ํ•„๋“œ์—๋งŒ ์‹ ๊ฒฝ ์“ฐ๊ธฐ ๋ฐ”์˜๋‹ค. ์ƒ๋Œ€๋ฐฉ์˜ ํ•„๋“œ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์—ฐ์‡„๊ฐ€ ๋ช‡ ๋ฒˆ ์—ฐ์†์œผ๋กœ ์ผ์–ด๋‚ ์ง€ ๊ณ„์‚ฐํ•˜์—ฌ ๋‚จ๊ทœ๋ฅผ ๋„์™€์ฃผ์ž!

์ž…๋ ฅ

์ด 12๊ฐœ์˜ ์ค„์— ํ•„๋“œ์˜ ์ •๋ณด๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, ๊ฐ ์ค„์—๋Š” 6๊ฐœ์˜ ๋ฌธ์ž๊ฐ€ ์žˆ๋‹ค.

์ด๋•Œ .์€ ๋นˆ๊ณต๊ฐ„์ด๊ณ  .์ด ์•„๋‹Œ๊ฒƒ์€ ๊ฐ๊ฐ์˜ ์ƒ‰๊น”์˜ ๋ฟŒ์š”๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

R์€ ๋นจ๊ฐ•, G๋Š” ์ดˆ๋ก, B๋Š” ํŒŒ๋ž‘, P๋Š” ๋ณด๋ผ, Y๋Š” ๋…ธ๋ž‘์ด๋‹ค.

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ํ•„๋“œ๋Š” ๋ฟŒ์š”๋“ค์ด ์ „๋ถ€ ์•„๋ž˜๋กœ ๋–จ์–ด์ง„ ๋’ค์˜ ์ƒํƒœ์ด๋‹ค. ์ฆ‰, ๋ฟŒ์š” ์•„๋ž˜์— ๋นˆ ์นธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

์ถœ๋ ฅ

ํ˜„์žฌ ์ฃผ์–ด์ง„ ์ƒํ™ฉ์—์„œ ๋ช‡์—ฐ์‡„๊ฐ€ ๋˜๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค. ํ•˜๋‚˜๋„ ํ„ฐ์ง€์ง€ ์•Š๋Š”๋‹ค๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿš€ํ’€์ด

๋™์‹œ์— ๋ฟŒ์š”๊ฐ€ ํ„ฐ์งˆ ๋•Œ ์—ฐ์‡„๊ฐ€ ๊ทธ ๊ฐœ์ˆ˜๋งŒํผ ๋Š˜์–ด๋‚˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ํ•˜๋‚˜๋งŒ ๋Š˜์–ด๋‚˜์•ผํ•œ๋‹ค.

๋จผ์ € ์ž…๋ ฅ ๊ฐ’์„ ๋ฐ›๋Š”๋‹ค.

bool flag = false;
int boomCnt = 0, maxBoomCnt = 0;
char board[13][7];
int visited[13][7];
int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };
void setting()
{
	for (int i = 1; i <= 12; ++i)
	{
		for (int j = 1; j <= 6; ++j)
		{
			cin >> board[i][j];
		}
	}
}

4๊ฐœ ์ด์ƒ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ BFS๋ฅผ ๋งŒ๋“ ๋‹ค.

void BFS(int y, int x)
{
	vector<pair<int, int>> boomList;
	queue<pair<int, int>> q;
	if (visited[y][x] == false && board[y][x] != '.')
	{
		q.push({ y, x });
		visited[y][x] = 1;
	}

	pair<int, int> here;
	while (q.empty() == false)
	{
		here = q.front();
		q.pop();
		boomList.push_back(here);

		for (int i = 0; i < 4; ++i)
		{
			int ny = here.first + dy[i];
			int nx = here.second + dx[i];

			if (ny < 1 || nx < 1 || ny > 12 || nx > 6)
				continue;
			if (visited[ny][nx] == 1)
				continue;
			if (board[ny][nx] != board[here.first][here.second])
				continue;

			q.push({ ny, nx });
			visited[ny][nx] = 1;
		}
	}

	if (boomList.size() >= 4)
	{
		// ์—ฐ์‡„ ์‹œ์ž‘
		for (int i = 0; i < boomList.size(); ++i)
		{
			board[boomList[i].first][boomList[i].second] = '.';
		}

		flag = true;
		//boomCnt++;
		//maxBoomCnt = max(maxBoomCnt, boomCnt);
	}
}

์ œ์ผ ์ฒ˜์Œ ๋งํ•œ๊ฑฐ์ฒ˜๋Ÿผ ํ„ฐํŠธ๋ฆฐ ๋ฟŒ์š” ์ˆ˜๋งŒํผ ๋Š˜์–ด๋‚˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ์„œ flag๋งŒ ๋ฐ”๊ฟ”์ค€๋‹ค.

๋ฟŒ์š”๋ฅผ ํ„ฐํŠธ๋ฆฌ๋ฉด ์ค‘๋ ฅ์˜ ์˜ํ–ฅ์œผ๋กœ ์•„๋ž˜๋กœ ๋–จ์–ด์ง€๋Š”๋ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ๊ตฌํ˜„ํ–ˆ๋‹ค.

void move(int y, int x)
{
	char ch = board[y][x];

	while (true)
	{
		int ny = y + 1;
		if (board[ny][x] != '.')
			break;

		board[y][x] = '.';
		board[ny][x] = ch;

		y = ny;
	}
}

ํ•œ ์‚ฌ์ดํด์ด ๋๋‚˜๋ฉด visited๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผํ•œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๊ฒŒ์ž„ ์ƒํ™ฉ์„ ๋ณด๊ธฐ ์œ„ํ•œ printBoardํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.

void visitedClear()
{
	for (int i = 1; i <= 12; ++i)
	{
		for (int j = 1; j <= 6; ++j)
		{
			visited[i][j] = 0;
		}
	}
}

void printBoard()
{
	for (int i = 1; i <= 12; ++i)
	{
		for (int j = 1; j <= 6; ++j)
		{
			cout << board[i][j] << " ";
		}
		
		cout << endl;
	}

	cout << endl;
}

๊ฒŒ์ž„์„ ์‹œ์ž‘ํ•˜๋Š” ํ•จ์ˆ˜๋กœ ์œ„์˜ ํ•จ์ˆ˜๋“ค์„ ์ข…ํ•ฉํ•ด์„œ ๋งŒ๋“ ๋‹ค.

void gameStart()
{
	while (true)
	{
		//printBoard();

		for (int i = 1; i <= 12; ++i)
		{
			for (int j = 1; j <= 6; ++j)
			{
				BFS(i, j);
			}
		}
		//printBoard();

        // flag๊ฐ€ ์ผœ์ง€๋ฉด ํ„ฐํŠธ๋ฆฐ ๋ฟŒ์š”๊ฐ€ ์žˆ๋‹ค๋Š” ๋œป
		if (flag == true)
			boomCnt++;

        // ๋ฐ‘์—์„œ ๋ถ€ํ„ฐ ์›€์ง์—ฌ์•ผํ•˜๋ฏ€๋กœ i๊ฐ€ 12๋ถ€ํ„ฐ ์ˆœํšŒํ•œ๋‹ค.
		for (int i = 12; i >= 1; --i)
		{
			for (int j = 1; j <= 6; ++j)
			{
				move(i, j);
			}
		}
		//printBoard();

		visitedClear();

		if (flag == false)
			break;
		flag = false;

		//cout << boomCnt << " " << maxBoomCnt << endl;
	}
	
}
void solve()
{
	setting();
	gameStart();

	cout << boomCnt;
}

๋ฌธ์ œ์˜ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋Œ๋ฆฌ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ๋œ๋‹ค.

image image image

image image image

image

๐Ÿš€์ „์ฒด ์ฝ”๋“œ

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include<iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>

// ๋ฟŒ์š”๋ฟŒ์š”
// ๊ฐ™์€ ์ƒ‰ ๋ฟŒ์š”๊ฐ€ 4๊ฐœ ์ด์ƒ ์ƒํ•˜์ขŒ์šฐ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์œผ๋ฉด ํ•œ๊บผ๋ฒˆ์— ์—†์–ด์ง„๋‹ค. -> BFS
// ์ด ๋•Œ 1์—ฐ์‡„๊ฐ€ ์‹œ์ž‘
// ์ค‘๋ ฅ์€ ํ•ญ์ƒ ์ ์šฉ
// ์ƒ‰์€ R, G, B, P, Y
// ๋นˆ๊ณต๊ฐ„์€ .
// ๋ช‡ ์—ฐ์‡„๊ฐ€ ์ผ์–ด๋‚˜๋Š”์ง€ ์•Œ์•„๋‚ด๋Š”๊ฒƒ์ด ๋ชฉํ‘œ

using namespace std;

void setting();
void BFS(int y, int x);
void move(int y, int x);
void visitedClear();
void printBoard();
void gameStart();
void solve();

bool flag = false;
int boomCnt = 0, maxBoomCnt = 0;
char board[13][7];
int visited[13][7];
int dy[] = { -1, 0, 1, 0 };
int dx[] = { 0, 1, 0, -1 };
vector<vector<char>> puyoes;
void setting()
{
	for (int i = 1; i <= 12; ++i)
	{
		for (int j = 1; j <= 6; ++j)
		{
			cin >> board[i][j];
			if (board[i][j] != '.')
			{
				puyoes.push_back({ board[i][j] });
			}
		}
	}
}

void BFS(int y, int x)
{
	vector<pair<int, int>> boomList;
	queue<pair<int, int>> q;
	if (visited[y][x] == false && board[y][x] != '.')
	{
		q.push({ y, x });
		visited[y][x] = 1;
	}

	pair<int, int> here;
	while (q.empty() == false)
	{
		here = q.front();
		q.pop();
		boomList.push_back(here);

		for (int i = 0; i < 4; ++i)
		{
			int ny = here.first + dy[i];
			int nx = here.second + dx[i];

			if (ny < 1 || nx < 1 || ny > 12 || nx > 6)
				continue;
			if (visited[ny][nx] == 1)
				continue;
			if (board[ny][nx] != board[here.first][here.second])
				continue;

			q.push({ ny, nx });
			visited[ny][nx] = 1;
		}
	}

	if (boomList.size() >= 4)
	{
		// ์—ฐ์‡„ ์‹œ์ž‘
		for (int i = 0; i < boomList.size(); ++i)
		{
			board[boomList[i].first][boomList[i].second] = '.';
		}

		flag = true;
		//boomCnt++;
		//maxBoomCnt = max(maxBoomCnt, boomCnt);
	}
}

void move(int y, int x)
{
	char ch = board[y][x];

	while (true)
	{
		int ny = y + 1;
		if (board[ny][x] != '.')
			break;

		board[y][x] = '.';
		board[ny][x] = ch;

		y = ny;
	}
}

void visitedClear()
{
	for (int i = 1; i <= 12; ++i)
	{
		for (int j = 1; j <= 6; ++j)
		{
			visited[i][j] = 0;
		}
	}
}

void printBoard()
{
	for (int i = 1; i <= 12; ++i)
	{
		for (int j = 1; j <= 6; ++j)
		{
			cout << board[i][j] << " ";
		}
		
		cout << endl;
	}

	cout << endl;
}

void gameStart()
{
	while (true)
	{
		//printBoard();

		for (int i = 1; i <= 12; ++i)
		{
			for (int j = 1; j <= 6; ++j)
			{
				BFS(i, j);
			}
		}
		//printBoard();

		if (flag == true)
			boomCnt++;

		for (int i = 12; i >= 1; --i)
		{
			for (int j = 1; j <= 6; ++j)
			{
				move(i, j);
			}
		}
		//printBoard();

		visitedClear();

		if (flag == false)
			break;
		flag = false;

		//cout << boomCnt << " " << maxBoomCnt << endl;
	}
	
}

void solve()
{
	setting();
	gameStart();

	cout << boomCnt;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	freopen("input.txt", "rt", stdin);

	solve();

	return 0;
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: