추리 소설 “초콜릿 괴도 레나”를 감명 깊게 읽은 코코는 이 소설의 명장면을 따라 해 보기로 했다. 구체적인 방법은 다음과 같다.
코코는 1단계의 조건을 충족하는 초콜릿을 준비해 놓았지만, 2단계의 조건을 충족하려면 어느 지점에 있는 단위 초콜릿을 떼어내야 할지 고민에 빠졌다. 코코를 도와주자.
https://www.acmicpc.net/problem/31464
#include <iostream>
#include <vector>
#include <queue>
#include <utility> // pair
using namespace std;
// 결과를 담을 구조체
struct ChocolateInfo
{
int componentCount; // 연결된 조각 개수
int nodeCount; // 초콜릿 조각 수
int edgeCount; // 연결선(간선) 수
};
int visitFlag = 0; // 현재 방문 체크용 타임스탬프
vector<vector<int>> visited;
vector<vector<int>> direction =
{
// 방향 벡터: 우, 하, 좌, 상
{0, 1}, {1, 0}, {0, -1}, {-1, 0}
};
vector<vector<char>> chocolateMap; // 입력받은 초콜릿 맵
// 맵 범위 안에 있는 좌표인지 확인하는 함수
bool isInBounds(int n, int x, int y)
{
return x >= 0 && x < n && y >= 0 && y < n;
}
// BFS: 현재 맵 상태에서 조각 개수, 초콜릿 수, 간선 수 계산
ChocolateInfo bfs(int n)
{
ChocolateInfo result = { 0, 0, 0 };// 초기화
queue<pair<int, int>> q;
visitFlag++; // 방문 타임스탬프 갱신
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (chocolateMap[i][j] == '.' || visited[i][j] == visitFlag)
continue; // 빈 칸이거나 이미 방문했으면 패스
result.componentCount++; // 새 조각 발견
result.nodeCount++; // 초콜릿 1개 포함
visited[i][j] = visitFlag;
q.push(make_pair(i, j)); // 시작점 큐에 추가
while (!q.empty())
{
pair<int, int> current = q.front();
q.pop();
for (int d = 0; d < 4; ++d) // 4방향 탐색
{
int nx = current.first + direction[d][0];
int ny = current.second + direction[d][1];
// 인접한 칸이 범위 내이고 초콜릿이면
if (isInBounds(n, nx, ny) && chocolateMap[nx][ny] == '#')
{
result.edgeCount++; // 연결선 증가
if (visited[nx][ny] < visitFlag)
{
visited[nx][ny] = visitFlag; // 방문 표시
result.nodeCount++; // 노드 수 증가
q.push(make_pair(nx, ny)); // 큐에 추가
}
}
}
}
}
}
result.edgeCount /= 2; // 간선 수는 양방향이므로 2로 나눔
return result;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
// 맵과 방문 배열 초기화
chocolateMap = vector<vector<char>>(n, vector<char>(n));
visited = vector<vector<int>>(n, vector<int>(n, 0));
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
cin >> chocolateMap[i][j];
vector<pair<int, int>> result;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
if (chocolateMap[i][j] == '.')
continue; // 초콜릿이 없는 곳은 건너뜀
chocolateMap[i][j] = '.'; // 초콜릿 하나 제거
ChocolateInfo info = bfs(n); // 나머지 연결 상태 확인
// 하나의 조각이고, 트리 구조라면 (간선 = 노드 - 1)
if (info.componentCount == 1 && info.nodeCount - 1 == info.edgeCount)
result.push_back(make_pair(i + 1, j + 1)); // 1-based 좌표 저장
chocolateMap[i][j] = '#';
}
}
cout << result.size() << '\n';
for (pair<int, int> pos : result)
cout << pos.first << ' ' << pos.second << '\n';
return 0;
}
Programmers 64064 불량 사용자 (0) | 2025.02.11 |
---|---|
Programmers 양과 늑대 (0) | 2025.01.22 |
백준 17825 주사위 윷놀이 (0) | 2025.01.21 |
백준 17270// 연예인은 힘들어//다익스트라 알고리즘 (Dijkstra's Algorithm)// (0) | 2025.01.20 |
항해//프로그래머스 60060// 가사 검색/c++// (3) | 2025.01.15 |