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 |
Tags
- 정렬
- 누적 합
- 다이나믹 프로그래밍
- 그리디 알고리즘
- 트리
- 다익스트라
- 스택
- 백트래킹
- Team Fortress 2
- BFS
- 우선순위 큐
- 브루트포스
- 재귀
- 자료구조
- VR
- 알고리즘
- 시뮬레이션
- 백준
- 수학
- 유니티
- 유니온 파인드
- Unreal Engine 5
- DFS
- 구현
- 투 포인터
- 그래프
- ue5
- 문자열
- c++
- XR Interaction Toolkit
Archives
- Today
- Total
1일1알
백준 2146번 다리 만들기 C++ 본문
#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>
#include <bitset>
using namespace std;
using int64 = long long;
int n;
int id = 1;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
struct Info {
int row;
int col;
int id;
int moveCnt;
};
queue<Info> q;
vector<vector<pair<int,int>>> board;
vector<vector<bool>> found;
void Bfs(int row, int col) {
if (found[row][col]) return;
if (board[row][col].first == 0) return;
queue<pair<int, int>> _q;
_q.push({ row,col });
board[row][col].first = id;
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 >= n) continue;
if (nextCol < 0 || nextCol >= n) continue;
if (found[nextRow][nextCol]) continue;
if (board[nextRow][nextCol].first == 0) {
board[nextRow][nextCol] = { id,1 };
q.push({ nextRow,nextCol,id,1 });
}
else {
board[nextRow][nextCol].first = id;
_q.push({ nextRow,nextCol });
}
found[nextRow][nextCol] = true;
}
}
id++;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
board = vector<vector<pair<int,int>>>(n, vector<pair<int,int>>(n));
found = vector<vector<bool>>(n, vector<bool>(n, false));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int input;
cin >> input;
board[i][j] = { input,0 };
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
Bfs(i, j);
}
}
int ans = 10000;
while (!q.empty()) {
auto curr = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nextRow = curr.row + dRow[i];
int nextCol = curr.col + dCol[i];
if (nextRow < 0 || nextRow >= n) continue;
if (nextCol < 0 || nextCol >= n) continue;
if (board[nextRow][nextCol].first == curr.id) continue;
if (board[nextRow][nextCol].first == 0) {
board[nextRow][nextCol] = { curr.id,curr.moveCnt+1 };
q.push({ nextRow,nextCol,curr.id,curr.moveCnt + 1 });
}
else {
ans = min(ans, curr.moveCnt + board[nextRow][nextCol].second);
}
}
}
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 2607번 비슷한 단어 C++ (0) | 2022.09.07 |
---|---|
백준 20291번 파일 정리 C++ (0) | 2022.09.06 |
백준 1937번 욕심쟁이 판다 C++ (0) | 2022.09.04 |
백준 9466번 텀 프로젝트 C++ (0) | 2022.09.03 |
백준 1520번 내리막 길 C++ (0) | 2022.09.02 |