https://www.acmicpc.net/problem/11559
๋๋์ ์ง์ง ์ด๋ ค์ ๋๊ฑฐ ๊ฐ๋ค ์ ๋ฒ ๋ฒฝ ๋ถ์๊ธฐ2๋ถํฐ ๋๋๊ฑด๋ฐ ๋ก์ง์ ์งค ์ค ์์ง๋ง ์ค์๋ฅผ ์์ฒญ ๋ง์ด ํ๋ ๊ฒ ๊ฐ๋ค ์ ์ด์ ๋์ด๋๋ ์์ง ์๋ค๋ณด๋ ์ค์ ํ ๋ฒํ๋ฉด ์๋ฌ์ก๊ธฐ๊ฐ ํฌ์ด๋ค ์ด๋ฒ์ ์ค์ํ ๋ถ๋ถ๋ค์
์ค์๋ฅผ 7๊ฐ๋ ์ฐพ์์ผ ํ์ผ๋ ๋ฌธ์ ๋ค ํธ๋๋ฐ ํ๋ฃจ ๊ฑธ๋ฆฐ๊ฑฐ ๊ฐ๋ค. ์ทจup gae๋งํda....
๊ฐ๋จํ๊ฒ ํ์ด ๊ฐ๋ฅํ๋ฉด ํํ๊ณ drag downํ๋ค. ๊ทผ๋ฐ ์ด ๊ณผ์ ์์ bfs๋ฅผ 2๋ฒ ์ฌ์ฉํ๊ณ drag downํ๋ ํจ์์์๋ ๋ฒ์์ ์ ๊ฒฝ์ธ ๋ถ๋ถ๋ค์ด ๋๋ฌด ๋ง๋ค. ๋ฐํน๋ ๋์ ๋ฐฉ๋ฒ์ ์ฐธ์กฐํ๋ ๊ฒ ํจ์ฌ ๋์ ๋ฏํ๋ค.
ํ์ด ๊ฐ๋ฅํ ๋์ do๋ฌธ์ ๋๋ค. ์ฐ์ ์ ์ผ๋ก drag downํ๋ค. ์ด drag downํ๋ ๋ถ๋ถ์ด ์ ์ ํ๋ค. ๋ชจ๋ ํ์ ๋๋๋ฐ ํ๋ง๋ค .์ผ๋์ ๊ณ์ swap์ ํ๋ค. ์ด๋ ๊ฒ ํ๋ฉด ์์ ๋ ์๋ ๋ฌธ์์ด์ด ์๋์ผ๋ก ์๋๋ก ๋ด๋ ค๊ฐ๊ณ ํ์ํ ๋ถ๋ถ์ .์ด ์๊ธธ ์ ์๋ค. ์ด ๋ถ๋ถ์ 2048 easy ๋ฒ์ ์์ ํ ๊ฒ์ฒ๋ผ ๊ทธ๋ฅ ์ ๋ฐฐ์ด์ ๋ง๋ค๊ณ ์ง์ด๋ฃ์ ์๋ ์๋ค(์ด๊ฒ ํ ์ฌ ์ฝ๋ค) ๊ทธ ๋ค์ 2์ฐจ์ ๋ฐฐ์ด์ ๋๋ฉด์ ๋ฌธ์์ด์ด ์๋ ๋ถ๋ถ์ ๋ํด์๋ bfs๋ฅผ ๋์ ํํ๋ ๋ถ๋ถ์ ์ค์ ํ๊ณ ๊ทธ๋ฅ 2์ฐจ์ ๋ฐฐ์ด์ ๋๋ฉด์ ๋ฃ์ด์ฃผ๊ณ ๋๋ธ๋ค(๊ตณ์ด ๋ค ๋ค์ bfs๋ฅผ ๋๋ฆฐ ๋๋๊ธฐ..) ํ ๋ถ๋ถ์ ๋ค ๋๊ณ ๋๋ฉด visํจ์๋ฅผ resetํด์ฃผ๊ณ ๋ค์ ํ์ด ๊ฐ๋ฅํ ๋์ ๋๋ค.
#include <bits/stdc++.h>
using namespace std;
bool isPuyo;
bool vis[12][6];
string board[12];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int ans;
void reset()
{
for(int i = 0; i < 12; ++i)
for(int j = 0; j < 6; ++j)
vis[i][j] = false;
}
void puyo(int x, int y)
{
vis[x][y] = true;
char color = board[x][y];
queue<pair<int, int> > q;
vector<pair<int, int> > tmp;
q.push({x, y});
tmp.push_back({x, y});
while (!q.empty())
{
pair<int, int> cur = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if (nx < 0 || ny < 0 || nx >= 12 || ny >= 6 || vis[nx][ny] || board[nx][ny] != color)
continue;
vis[nx][ny] = true;
q.push({nx, ny});
tmp.push_back({nx, ny});
}
}
if (tmp.size() >= 4)
{
isPuyo = true;
for (auto cur : tmp)
board[cur.first][cur.second] = '.';
}
}
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
for (int i = 0; i < 12; i++)
cin >> board[i];
do{
isPuyo = false;
//drag downํ๋ ๋ถ๋ถ
for (int j = 0; j < 6; j++)
{
for (int i = 10; i >= 0; i--)
{
int tmp = i;
while (tmp < 11 && board[tmp + 1][j] == '.')
{
swap(board[tmp][j], board[tmp + 1][j]);
tmp++;
}
}
}
for (int i = 0; i < 12; i++)
for (int j = 0; j < 6; j++)
if (!vis[i][j] && board[i][j] != '.')
puyo(i, j);
if (isPuyo)
ans++;
reset();
}while (isPuyo);
cout << ans;
}