Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- DFS
- 유니온 파인드
- XR Interaction Toolkit
- Team Fortress 2
- 정렬
- 자료구조
- 누적 합
- VR
- 수학
- c++
- BFS
- 트리
- 재귀
- 투 포인터
- 스택
- 그리디 알고리즘
- 유니티
- 백준
- 백트래킹
- 구현
- 시뮬레이션
- 브루트포스
- 다익스트라
- 알고리즘
- ue5
- Unreal Engine 5
- 문자열
- 우선순위 큐
- 다이나믹 프로그래밍
- 그래프
Archives
- Today
- Total
1일1알
백준 3197번 백조의 호수 C++ 본문

매번 빙판이 녹는것과 백조가 만날 수 있는지를 bfs를 돌리면 시간초과가 난다.
그렇기 때문에 각 구역마다 번호를 지정해두고 유니온 파인드를 이용해 빙판이 녹아서 합쳐지면 두 구역을 합쳐주면서 백조가 같은 구역에 있는지를 검사하면 된다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int r, c;
int head = 0;
vector<vector<bool>> found;
vector<vector<pair<int, char>>> board;
vector<int> parent;
vector<int> height;
vector<int> targets;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
int GetParent(int n) {
if (n == parent[n])
return n;
return parent[n] = GetParent(parent[n]);
}
void Merge(int u, int v) {
u = GetParent(u);
v = GetParent(v);
if (u == v)
return;
if (height[u] > height[v])
::swap(u, v);
parent[u] = v;
if (height[u] == height[v])
height[v]++;
}
void InitBfs(int row, int col) {
queue<pair<int, int>> q;
q.push({ row,col });
found[row][col] = true;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (board[curr.first][curr.second].second == 'L')
targets.push_back(head);
board[curr.first][curr.second].first = head;
for (int i = 0; i < 4; i++) {
int nextRow = curr.first + dRow[i];
int nextCol = curr.second + dCol[i];
if (nextRow < 0 || nextRow >= r) continue;
if (nextCol < 0 || nextCol >= c) continue;
if (board[nextRow][nextCol].second == 'X') continue;
if (found[nextRow][nextCol]) continue;
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol });
}
}
head++;
}
void Bfs(int row, int col, vector<pair<int, int>> &tmpFounds) {
set<pair<int, int>> melts;
int currHead = board[row][col].first;
queue<pair<int, int>> q;
q.push({ row,col });
found[row][col] = true;
while (!q.empty()) {
auto curr = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nextRow = curr.first + dRow[i];
int nextCol = curr.second + dCol[i];
if (nextRow < 0 || nextRow >= r) continue;
if (nextCol < 0 || nextCol >= c) continue;
if (board[nextRow][nextCol].second == 'X') {
melts.insert({ nextRow,nextCol });
for (int j = 0; j < 4; j++) {
int nnextRow = nextRow + dRow[j];
int nnextCol = nextCol + dCol[j];
if (nnextRow < 0 || nnextRow >= r) continue;
if (nnextCol < 0 || nnextCol >= c) continue;
if (board[nnextRow][nnextCol].second != 'X') {
Merge(currHead, board[nnextRow][nnextCol].first);
}
}
continue;
}
if (found[nextRow][nextCol]) continue;
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol });
}
}
for (auto a : melts) {
found[a.first][a.second] = true;
tmpFounds.push_back({ a.first,a.second });
board[a.first][a.second] = { currHead,'.' };
}
}
void RefreshFound() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
found[i][j] = false;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> r >> c;
found = vector<vector<bool>>(r, vector<bool>(c, false));
board = vector<vector<pair<int, char>>>(r, vector <pair<int, char>>(c));
for (int i = 0; i < r; i++) {
string str;
cin >> str;
for (int j = 0; j < c; j++) {
board[i][j] = { -1,str[j] };
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (board[i][j].second == 'X') continue;
if (found[i][j]) continue;
InitBfs(i, j);
}
}
parent = vector<int>(head);
height = vector<int>(head, 1);
for (int i = 0; i < head; i++) {
parent[i] = i;
}
RefreshFound();
int cnt = 0;
int target1 = targets[0];
int target2 = targets[1];
vector<pair<int, int>> tmpFounds;
while (true) {
if (GetParent(target1) == GetParent(target2))
break;
if (cnt == 0) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (board[i][j].second == 'X') continue;
if (found[i][j]) continue;
Bfs(i, j, tmpFounds);
}
}
}
else {
vector<pair<int, int>> tmp;
for (auto a : tmpFounds) {
tmp.push_back({ a.first, a.second });
found[a.first][a.second] = false;
}
tmpFounds.clear();
for (auto a : tmp) {
Bfs(a.first, a.second, tmpFounds);
}
}
cnt++;
}
cout << cnt;
}
'알고리즘' 카테고리의 다른 글
백준 15685번 드래곤 커브 C++ (0) | 2022.07.31 |
---|---|
백준 4991번 로봇 청소기 C++ (0) | 2022.07.26 |
백준 2310번 어드벤처 게임 C++ (0) | 2022.07.24 |
백준 16946번 벽 부수고 이동하기 4 C++ (0) | 2022.07.23 |
백준 16724번 피리 부는 사나이 C++ (0) | 2022.07.22 |