Taene's

[백준] 골드4-치즈 C++ 2636번 본문

백준/골드

[백준] 골드4-치즈 C++ 2636번

taene_ 2025. 2. 24. 14:36

https://www.acmicpc.net/problem/2636

 

// 자기자신이 1이라면 (adj[y][x]==1) 방문한것도 알수있고 vector에 넣고 return할 수 있다.

#include <iostream>
#include <vector>
using namespace std;

int N, M;
int adj[100][100];
bool visited[100][100];
vector<pair<int, int>> eraser;
int ret, hour;
int dy[] = { -1,1,0,0 };
int dx[] = { 0,0,-1,1 };

void OneHour(int y, int x)
{
	visited[y][x] = 1;
	if (adj[y][x] == 1)
	{
		eraser.push_back({ y,x });
		return;
	}

	for (int i = 0; i < 4; i++)
	{
		int ny = y + dy[i];
		int nx = x + dx[i];

		if (ny < 0 || nx < 0 || ny >= N || nx >= M) continue;
		if (visited[ny][nx]) continue;

		OneHour(ny, nx);
	}

	return;
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	cin >> N >> M;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < M; j++)
		{
			cin >> adj[i][j];
		}
	}

	while (true)
	{
		fill(&visited[0][0], &visited[0][0] + 100 * 100, 0);
		eraser.clear();

		OneHour(0, 0);

		int cnt = eraser.size();

		if (cnt)
			ret = cnt;
		else
			break;

		for (auto it : eraser)
			adj[it.first][it.second] = 0;

		hour++;
	}
	cout << hour << '\n' << ret << '\n';

	return 0;
}

'백준 > 골드' 카테고리의 다른 글

[백준] 골드5-트리 C++ 1068번  (0) 2025.02.24
[백준] 골드4-연구소 C++ 14502번  (0) 2025.02.24