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
- 다이나믹 프로그래밍
- 수학
- 구현
- 그래프
- 시뮬레이션
- 백준
- VR
- 재귀
- 스택
- Unreal Engine 5
- XR Interaction Toolkit
- 문자열
- c++
- 유니온 파인드
- 우선순위 큐
- 투 포인터
- 알고리즘
- BFS
- DFS
- 그리디 알고리즘
- 자료구조
- ue5
- 유니티
- 정렬
- 누적 합
- 백트래킹
- 브루트포스
- 다익스트라
- 트리
- Team Fortress 2
Archives
- Today
- Total
1일1알
백준 19238번 스타트 택시 C++ 본문
https://www.acmicpc.net/problem/19238
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>
#include <bitset>
using namespace std;
using int64 = long long;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
int n, m, f;
struct Info {
int row;
int col;
int useF;
};
vector<vector<int>> board;
vector<vector<bool>> found;
map<pair<int, int>, pair<int, int>> mp;
void RefReshFound() {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
found[i][j] = false;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> f;
board = vector<vector<int>>(n + 1, vector<int>(n + 1));
found = vector<vector<bool>>(n + 1, vector<bool>(n + 1, false));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> board[i][j];
}
}
int startRow, startCol;
cin >> startRow >> startCol;
for (int i = 0; i < m; i++) {
int sR, sC, eR, eC;
cin >> sR >> sC >> eR >> eC;
board[sR][sC] = 2;
mp.insert({ {sR,sC},{eR,eC} });
}
while (true) {
queue<Info> q;
q.push({ startRow,startCol,0 });
found[startRow][startCol] = true;
Info foundPerson{ -1,-1,-1 };
set<pair<int, int>> persons;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (board[curr.row][curr.col] == 2) {
persons.insert({ curr.row,curr.col });
foundPerson = curr;
break;
}
for (int i = 0; i < 4; i++) {
int nextRow = curr.row + dRow[i];
int nextCol = curr.col + dCol[i];
if (nextRow<1 || nextRow>n) continue;
if (nextCol<1 || nextCol>n) continue;
if (found[nextRow][nextCol]) continue;
if (board[nextRow][nextCol] == 1) continue;
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol,curr.useF + 1 });
}
}
if (foundPerson.row == -1) {
f = -1;
break;
}
if (foundPerson.useF >= f) {
f = -1;
break;
}
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (curr.useF > foundPerson.useF) break;
if (board[curr.row][curr.col] == 2) {
persons.insert({ curr.row,curr.col });
}
}
while (!q.empty()) q.pop();
startRow = persons.begin()->first;
startCol = persons.begin()->second;
f -= foundPerson.useF;
auto it = mp.find({ startRow,startCol });
pair<int, int> target = it->second;
mp.erase(it);
RefReshFound();
board[startRow][startCol] = 0;
found[startRow][startCol] = true;
q.push({ startRow,startCol,0 });
int useF = -1;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (curr.row == target.first && curr.col == target.second) {
useF = curr.useF;
break;
}
for (int i = 0; i < 4; i++) {
int nextRow = curr.row + dRow[i];
int nextCol = curr.col + dCol[i];
if (nextRow<1 || nextRow>n) continue;
if (nextCol<1 || nextCol>n) continue;
if (found[nextRow][nextCol]) continue;
if (board[nextRow][nextCol] == 1) continue;
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol,curr.useF + 1 });
}
}
if (useF == -1) {
f = -1;
break;
}
if (useF > f) {
f = -1;
break;
}
f += useF;
startRow = target.first;
startCol = target.second;
if (mp.empty()) break;
RefReshFound();
}
cout << f;
}
'알고리즘' 카테고리의 다른 글
백준 17135번 캐슬 디펜스 C++ (0) | 2022.09.18 |
---|---|
백준 25307번 시루의 백화점 구경 C++ (0) | 2022.09.17 |
백준 3273번 두 수의 합 C++ (0) | 2022.09.15 |
백준 2437번 저울 C++ (1) | 2022.09.14 |
백준 1939번 중량제한 C++ (0) | 2022.09.13 |