BOJ 2636. ์น์ฆ
๐โโ๏ธ[Gold IV] ์น์ฆ - 2636
์ฑ๋ฅ ์์ฝ
๋ฉ๋ชจ๋ฆฌ: 2160 KB, ์๊ฐ: 68 ms
๋ถ๋ฅ
๋๋น ์ฐ์ ํ์, ๊ทธ๋ํ ์ด๋ก , ๊ทธ๋ํ ํ์, ๊ตฌํ, ์๋ฎฌ๋ ์ด์
์ ์ถ ์ผ์
2024๋ 2์ 13์ผ 14:40:27
๋ฌธ์ ์ค๋ช
์๋ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ์ด ์ ์ฌ๊ฐํ ์นธ๋ค๋ก ์ด๋ฃจ์ด์ง ์ฌ๊ฐํ ๋ชจ์์ ํ์ด ์๊ณ , ๊ทธ ์์ ์์ ์น์ฆ(ํ์์ผ๋ก ํ์๋ ๋ถ๋ถ)๊ฐ ๋์ฌ ์๋ค. ํ์ ๊ฐ์ฅ์๋ฆฌ(<๊ทธ๋ฆผ 1>์์ ๋ค๋ชจ ์นธ์ X์น ๋ถ๋ถ)์๋ ์น์ฆ๊ฐ ๋์ฌ ์์ง ์์ผ๋ฉฐ ์น์ฆ์๋ ํ๋ ์ด์์ ๊ตฌ๋ฉ์ด ์์ ์ ์๋ค.
์ด ์น์ฆ๋ฅผ ๊ณต๊ธฐ ์ค์ ๋์ผ๋ฉด ๋ น๊ฒ ๋๋๋ฐ ๊ณต๊ธฐ์ ์ ์ด๋ ์นธ์ ํ ์๊ฐ์ด ์ง๋๋ฉด ๋ น์ ์์ด์ง๋ค. ์น์ฆ์ ๊ตฌ๋ฉ ์์๋ ๊ณต๊ธฐ๊ฐ ์์ง๋ง ๊ตฌ๋ฉ์ ๋๋ฌ์ผ ์น์ฆ๊ฐ ๋ น์์ ๊ตฌ๋ฉ์ด ์ด๋ฆฌ๋ฉด ๊ตฌ๋ฉ ์์ผ๋ก ๊ณต๊ธฐ๊ฐ ๋ค์ด๊ฐ๊ฒ ๋๋ค. <๊ทธ๋ฆผ 1>์ ๊ฒฝ์ฐ, ์น์ฆ์ ๊ตฌ๋ฉ์ ๋๋ฌ์ผ ์น์ฆ๋ ๋ น์ง ์๊ณ โcโ๋ก ํ์๋ ๋ถ๋ถ๋ง ํ ์๊ฐ ํ์ ๋ น์ ์์ด์ ธ์ <๊ทธ๋ฆผ 2>์ ๊ฐ์ด ๋๋ค.
<๊ทธ๋ฆผ 1> ์๋ ์น์ฆ ๋ชจ์
๋ค์ ํ ์๊ฐ ํ์๋ <๊ทธ๋ฆผ 2>์์ โcโ๋ก ํ์๋ ๋ถ๋ถ์ด ๋ น์ ์์ด์ ธ์ <๊ทธ๋ฆผ 3>๊ณผ ๊ฐ์ด ๋๋ค.
<๊ทธ๋ฆผ 2> ํ ์๊ฐ ํ์ ์น์ฆ ๋ชจ์
<๊ทธ๋ฆผ 3> ๋ ์๊ฐ ํ์ ์น์ฆ ๋ชจ์
<๊ทธ๋ฆผ 3>์ ์๋ ์น์ฆ์ ๋ ์๊ฐ ํ ๋ชจ์์ ๋ํ๋ด๊ณ ์์ผ๋ฉฐ, ๋จ์ ์กฐ๊ฐ๋ค์ ํ ์๊ฐ์ด ๋ ์ง๋๋ฉด ๋ชจ๋ ๋ น์ ์์ด์ง๋ค. ๊ทธ๋ฌ๋ฏ๋ก ์ฒ์ ์น์ฆ๊ฐ ๋ชจ๋ ๋ น์ ์์ด์ง๋ ๋ฐ๋ ์ธ ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค. <๊ทธ๋ฆผ 3>๊ณผ ๊ฐ์ด ์น์ฆ๊ฐ ๋ น๋ ๊ณผ์ ์์ ์ฌ๋ฌ ์กฐ๊ฐ์ผ๋ก ๋๋์ด ์ง ์๋ ์๋ค.
์ ๋ ฅ์ผ๋ก ์ฌ๊ฐํ ๋ชจ์์ ํ์ ํฌ๊ธฐ์ ํ ์กฐ๊ฐ์ ์น์ฆ๊ฐ ํ ์์ ์ฃผ์ด์ก์ ๋, ๊ณต๊ธฐ ์ค์์ ์น์ฆ๊ฐ ๋ชจ๋ ๋ น์ ์์ด์ง๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ๊ณผ ๋ชจ๋ ๋ น๊ธฐ ํ ์๊ฐ ์ ์ ๋จ์์๋ ์น์ฆ์กฐ๊ฐ์ด ๋์ฌ ์๋ ์นธ์ ๊ฐ์๋ฅผ ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์๋ ์ฌ๊ฐํ ๋ชจ์ ํ์ ์ธ๋ก์ ๊ฐ๋ก์ ๊ธธ์ด๊ฐ ์์ ์ ์๋ก ์ฃผ์ด์ง๋ค. ์ธ๋ก์ ๊ฐ๋ก์ ๊ธธ์ด๋ ์ต๋ 100์ด๋ค. ํ์ ๊ฐ ๊ฐ๋ก์ค์ ๋ชจ์์ด ์ ์ค๋ถํฐ ์ฐจ๋ก๋ก ๋์งธ ์ค๋ถํฐ ๋ง์ง๋ง ์ค๊น์ง ์ฃผ์ด์ง๋ค. ์น์ฆ๊ฐ ์๋ ์นธ์ 0, ์น์ฆ๊ฐ ์๋ ์นธ์ 1๋ก ์ฃผ์ด์ง๋ฉฐ ๊ฐ ์ซ์ ์ฌ์ด์๋ ๋น์นธ์ด ํ๋์ฉ ์๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์๋ ์น์ฆ๊ฐ ๋ชจ๋ ๋ น์์ ์์ด์ง๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ ์ถ๋ ฅํ๊ณ , ๋์งธ ์ค์๋ ๋ชจ๋ ๋ น๊ธฐ ํ ์๊ฐ ์ ์ ๋จ์์๋ ์น์ฆ์กฐ๊ฐ์ด ๋์ฌ ์๋ ์นธ์ ๊ฐ์๋ฅผ ์ถ๋ ฅํ๋ค.
๐ํ์ด
์ ์ด๊ฑฐ ํธ๋๋ฐ 2์ผ ๊ฑธ๋ฆผ
์น์ฆ์ ๊ฐ์ฅ์๋ฆฌ๋ฅผ ํ๋ณํ๋๊ฒ ์๋ ์ฌ๋๋ค.
BFS๋ฅผ ๋ ๊ฐ ๋ง๋ค์ด์ ํ์๋ค.
์ ์ญ๋ณ์์ ํจ์๋คโฆ
void solve();
void BFS(int y, int x);
void removeCheese(vector<pair<int, int>> cheese);
void printBoard();
void visitedClear();
void visited2Clear();
int n, m;
int dy[] = { -1, 0, 1, 0, -1, 1, 1, -1 };
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
vector<vector<int>> board;
vector<vector<int>> visited;
vector<vector<int>> visited2;
vector<pair<int, int>> cheese;
์ค์ํ ์ ์ ์น์ฆ ๊ทธ๋ฃน์ด ์ฌ๋ฌ๊ฐ๊ฐ ์๊ธธ์์๊ณ ๊ฐ๊ฐ ํ ์๊ฐ๋ง๋ค ๊ฒ์ ์ง์์ผํ๋ค.
solveํจ์์์ ๊ทธ ์กฐ๊ฑด์ ์๊ฐํด์คฌ๋ค.
void solve()
{
// ์
๋ ฅ๊ฐ ๋ฐ๊ธฐ.
cin >> n >> m;
board = vector<vector<int>>(n + 1, vector<int>(m + 1));
visited = vector<vector<int>>(n + 1, vector<int>(m + 1));
visited2 = vector<vector<int>>(n + 1, vector<int>(m + 1));
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cin >> board[i][j];
}
}
// ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ ๋ณ์๋ค
int totalTime = 0;
int lastCheese = 0;
// ํ์๊ฐ๋ง์ ์น์ฆ๊ฐ ์์ด์ง์ ์์ด์ ์์ธ ์ฒ๋ฆฌ
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (board[i][j] == 1)
{
lastCheese++;
}
}
}
// ๋จ์ ์น์ฆ๊ฐ ์์๋๊น์ง ๋ฐ๋ณต
bool flag = true;
while (flag == true)
{
flag = false;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
// ์น์ฆ๊ทธ๋ฃน ๋ง๋ค๊ธฐ
BFS(i, j);
// ์น์ฆ๊ฐ ์๋ค๋ฉด ๊ฒ๋ฉด ์ง์์ฃผ๊ธฐ
if (cheese.size() != 0)
{
flag = true;
// ์น์ฆ ์ง์์ฃผ๋ ํจ์
removeCheese(cheese);
cheese.clear();
}
}
}
// ๋จ์ ์น์ฆ ๊ฐ์ ๊ตฌํ๊ธฐ
int cheeseCnt = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (board[i][j] == 1)
{
cheeseCnt++;
}
}
}
// ์๊ฐ ์ฆ๊ฐ
totalTime++;
if (cheeseCnt != 0)
lastCheese = cheeseCnt;
else if (cheeseCnt == 0)
flag = false;
// ๋ฐฉ๋ฌธ ์ด๊ธฐ
visitedClear();
}
cout << totalTime << '\n';
cout << lastCheese;
}
์น์ฆ ๊ทธ๋ฃน์ ๋ง๋๋ BFS
void BFS(int y, int x)
{
queue<pair<int, int>> q;
if (board[y][x] == 1 && visited[y][x] == 0)
q.push({ y, x });
pair<int, int> here;
while (q.empty() == false)
{
here = q.front();
cheese.push_back(here);
q.pop();
for (int i = 0; i < 8; ++i)
{
int ny = here.first + dy[i];
int nx = here.second + dx[i];
if (ny < 1 || nx < 1 || ny > n || nx > m)
continue;
if (board[ny][nx] == 0)
continue;
if (visited[ny][nx] == 1)
continue;
q.push({ ny, nx });
visited[ny][nx] = 1;
}
}
}
๊ฐ์ฅ์๋ฆฌ์ธ์ง ํ์ธํ๋ BFS
bool BFS2(int y, int x)
{
queue<pair<int, int>> q;
q.push({ y, x });
pair<int, int> here;
bool flag = false;
while (q.empty() == false)
{
here = q.front();
q.pop();
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 > n || nx > m)
{
flag = true;
return flag;
}
if (board[ny][nx] == 1)
continue;
if (visited2[ny][nx] == 1)
continue;
q.push({ ny, nx });
visited2[ny][nx] = 1;
}
}
return flag;
}
์ง์ง ์น์ฆ ๊ฒ๋ฉด์ ์ง์์ฃผ๋ ํจ์
void removeCheese(vector<pair<int, int>> cheese)
{
vector<pair<int, int>> clearList;
int up = 100, down = 0, left = 100, right = 0;
for (int i = 0; i < cheese.size(); ++i)
{
up = min(up, cheese[i].first);
down = max(down, cheese[i].first);
left = min(left, cheese[i].second);
right = max(right, cheese[i].second);
}
for (int i = 0; i < cheese.size(); ++i)
{
visited2Clear();
// ์์ชฝ ๊ฐ์ฅ์๋ฆฌ์ธ ๊ฒฝ์ฐ
if (board[cheese[i].first - 1][cheese[i].second] == 0)
{
// bfs2 ๋๋ฆฌ๊ธฐ
bool flag = BFS2(cheese[i].first - 1, cheese[i].second);
if (flag == true) // ๊ฐ์ฅ์๋ฆฌ๋ค.
{
clearList.push_back({ cheese[i].first, cheese[i].second });
continue;
}
visited2Clear();
}
// ์ผ์ชฝ ๊ฐ์ฅ์๋ฆฌ์ธ ๊ฒฝ์ฐ
if (board[cheese[i].first][cheese[i].second - 1] == 0)
{
// bfs2 ๋๋ฆฌ๊ธฐ
bool flag = BFS2(cheese[i].first, cheese[i].second - 1);
if (flag == true) // ๊ฐ์ฅ์๋ฆฌ๋ค.
{
clearList.push_back({ cheese[i].first, cheese[i].second });
continue;
}
visited2Clear();
}
// ์ค๋ฅธ์ชฝ ๊ฐ์ฅ์๋ฆฌ์ธ ๊ฒฝ์ฐ
if (board[cheese[i].first][cheese[i].second + 1] == 0)
{
// bfs2 ๋๋ฆฌ๊ธฐ
bool flag = BFS2(cheese[i].first, cheese[i].second + 1);
if (flag == true) // ๊ฐ์ฅ์๋ฆฌ๋ค.
{
clearList.push_back({ cheese[i].first, cheese[i].second });
continue;
}
visited2Clear();
}
// ์๋์ชฝ ๊ฐ์ฅ์๋ฆฌ์ธ ๊ฒฝ์ฐ
if (board[cheese[i].first + 1][cheese[i].second] == 0)
{
// bfs2 ๋๋ฆฌ๊ธฐ
bool flag = BFS2(cheese[i].first + 1, cheese[i].second);
if (flag == true) // ๊ฐ์ฅ์๋ฆฌ๋ค.
{
clearList.push_back({ cheese[i].first, cheese[i].second });
continue;
}
visited2Clear();
}
}
for (int i = 0; i < clearList.size(); ++i)
{
board[clearList[i].first][clearList[i].second] = 0;
}
}
๐์ ์ฒด ์ฝ๋
#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>
using namespace std;
void solve();
void BFS(int y, int x);
void removeCheese(vector<pair<int, int>> cheese);
void printBoard();
void visitedClear();
void visited2Clear();
int n, m;
int dy[] = { -1, 0, 1, 0, -1, 1, 1, -1 };
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
vector<vector<int>> board;
vector<vector<int>> visited;
vector<vector<int>> visited2;
vector<pair<int, int>> cheese;
void solve()
{
cin >> n >> m;
board = vector<vector<int>>(n + 1, vector<int>(m + 1));
visited = vector<vector<int>>(n + 1, vector<int>(m + 1));
visited2 = vector<vector<int>>(n + 1, vector<int>(m + 1));
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cin >> board[i][j];
}
}
// ๊ทธ๋ฃน์ ์ง๋๋ค.
// ๊ทธ๋ฃน์ ๊ฐ์ฅ์๋ฆฌ๋ฅผ ๊ตฌํ๋ค.
// ๊ทธ๋ฃน์ ๊ฐ์ฅ์๋ฆฌ๋ฅผ ์์ค๋ค.
// ๋ค์ ๊ทธ๋ฃน์ ์ง๋๋ค.
// ...
// ๊ฐ์ฅ์๋ฆฌ ๊ฐ๋
์ ์ด๋ป๊ฒ ์์ง
// ์๊ฐ 0์ด๋ฉด์ ๊ทธ๋ฃน ๋ด์ ๋งจ์ y๊ฐ์ ์์ผํ๋ค.
// ๊ทธ๋ฃน์ ๊ฐ๋
์ bfs๋ก ๋ง๋ ๋ค?
//
//
//printBoard();
//cout << '\n';
int totalTime = 0;
int lastCheese = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (board[i][j] == 1)
{
lastCheese++;
}
}
}
bool flag = true;
while (flag == true)
{
flag = false;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
BFS(i, j);
if (cheese.size() != 0)
{
flag = true;
removeCheese(cheese);
cheese.clear();
}
}
}
//printBoard();
//cout << endl;
int cheeseCnt = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (board[i][j] == 1)
{
cheeseCnt++;
}
}
}
totalTime++;
if (cheeseCnt != 0)
lastCheese = cheeseCnt;
else if (cheeseCnt == 0)
flag = false;
visitedClear();
}
cout << totalTime << '\n';
cout << lastCheese;
}
void BFS(int y, int x)
{
queue<pair<int, int>> q;
if (board[y][x] == 1 && visited[y][x] == 0)
q.push({ y, x });
pair<int, int> here;
while (q.empty() == false)
{
here = q.front();
cheese.push_back(here);
q.pop();
for (int i = 0; i < 8; ++i)
{
int ny = here.first + dy[i];
int nx = here.second + dx[i];
if (ny < 1 || nx < 1 || ny > n || nx > m)
continue;
if (board[ny][nx] == 0)
continue;
if (visited[ny][nx] == 1)
continue;
q.push({ ny, nx });
visited[ny][nx] = 1;
}
}
}
bool BFS2(int y, int x)
{
queue<pair<int, int>> q;
q.push({ y, x });
pair<int, int> here;
bool flag = false;
while (q.empty() == false)
{
here = q.front();
q.pop();
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 > n || nx > m)
{
flag = true;
return flag;
}
if (board[ny][nx] == 1)
continue;
if (visited2[ny][nx] == 1)
continue;
q.push({ ny, nx });
visited2[ny][nx] = 1;
}
}
return flag;
}
// ์ด ํจ์๊ฐ ์๋ชป๋จ
// ์น์ฆ ๊ฒ๋ง ์ง์์ผํ๋๋ฐ ๋ก์ง์ด ์๋ชป๋๋ค
// ์น์ฆ ๊ฒ์ ์์์๋ ๋ฐฉ๋ฒ์?
void removeCheese(vector<pair<int, int>> cheese)
{
vector<pair<int, int>> clearList;
int up = 100, down = 0, left = 100, right = 0;
for (int i = 0; i < cheese.size(); ++i)
{
up = min(up, cheese[i].first);
down = max(down, cheese[i].first);
left = min(left, cheese[i].second);
right = max(right, cheese[i].second);
}
for (int i = 0; i < cheese.size(); ++i)
{
visited2Clear();
// ์์ชฝ ๊ฐ์ฅ์๋ฆฌ์ธ ๊ฒฝ์ฐ
if (board[cheese[i].first - 1][cheese[i].second] == 0)
{
// bfs2 ๋๋ฆฌ๊ธฐ
bool flag = BFS2(cheese[i].first - 1, cheese[i].second);
if (flag == true) // ๊ฐ์ฅ์๋ฆฌ๋ค.
{
clearList.push_back({ cheese[i].first, cheese[i].second });
continue;
}
visited2Clear();
}
// ์ผ์ชฝ ๊ฐ์ฅ์๋ฆฌ์ธ ๊ฒฝ์ฐ
if (board[cheese[i].first][cheese[i].second - 1] == 0)
{
// bfs2 ๋๋ฆฌ๊ธฐ
bool flag = BFS2(cheese[i].first, cheese[i].second - 1);
if (flag == true) // ๊ฐ์ฅ์๋ฆฌ๋ค.
{
clearList.push_back({ cheese[i].first, cheese[i].second });
continue;
}
visited2Clear();
}
// ์ค๋ฅธ์ชฝ ๊ฐ์ฅ์๋ฆฌ์ธ ๊ฒฝ์ฐ
if (board[cheese[i].first][cheese[i].second + 1] == 0)
{
// bfs2 ๋๋ฆฌ๊ธฐ
bool flag = BFS2(cheese[i].first, cheese[i].second + 1);
if (flag == true) // ๊ฐ์ฅ์๋ฆฌ๋ค.
{
clearList.push_back({ cheese[i].first, cheese[i].second });
continue;
}
visited2Clear();
}
// ์๋์ชฝ ๊ฐ์ฅ์๋ฆฌ์ธ ๊ฒฝ์ฐ
if (board[cheese[i].first + 1][cheese[i].second] == 0)
{
// bfs2 ๋๋ฆฌ๊ธฐ
bool flag = BFS2(cheese[i].first + 1, cheese[i].second);
if (flag == true) // ๊ฐ์ฅ์๋ฆฌ๋ค.
{
clearList.push_back({ cheese[i].first, cheese[i].second });
continue;
}
visited2Clear();
}
}
for (int i = 0; i < clearList.size(); ++i)
{
board[clearList[i].first][clearList[i].second] = 0;
}
}
void printBoard()
{
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
cout << board[i][j] << " ";
}
cout << '\n';
}
}
void visitedClear()
{
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
visited[i][j] = 0;
}
}
}
void visited2Clear()
{
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
visited2[i][j] = 0;
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
freopen("input.txt", "rt", stdin);
solve();
return 0;
}