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
- 브루트포스
- BFS
- 그래프
- 우선순위 큐
- 구현
- 재귀
- 알고리즘
- ue5
- 그리디 알고리즘
- 정렬
- c++
- 다익스트라
- 유니티
- 트리
- 시뮬레이션
- Team Fortress 2
- DFS
- VR
- 백트래킹
- 수학
- XR Interaction Toolkit
- 자료구조
- 스택
- 유니온 파인드
- 백준
- 투 포인터
- Unreal Engine 5
- 문자열
- 다이나믹 프로그래밍
- 누적 합
Archives
- Today
- Total
1일1알
백준 4485번 녹색 옷 입은 애가 젤다지? 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 n;
vector<vector<int>> board;
vector<vector<bool>> found;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
struct Info {
pair<int, int> pos;
int penalty;
bool operator<(const Info& other) const {
return penalty < other.penalty;
}
bool operator>(const Info& other) const {
return penalty > other.penalty;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int cnt = 1;
while (true) {
cin >> n;
if (n == 0) break;
board = vector<vector<int>>(n, vector<int>(n));
found = vector<vector<bool>>(n, vector<bool>(n, false));
pair<int, int> targetPos = { n - 1,n - 1 };
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> board[i][j];
}
}
priority_queue<Info, vector<Info>, greater<Info>> pq;
pq.push({ {0,0},board[0][0] });
found[0][0] = true;
int ans = 0;
while (!pq.empty()) {
auto curr = pq.top();
pq.pop();
if (curr.pos == targetPos) {
ans = curr.penalty;
break;
}
for (int i = 0; i < 4; i++) {
int nextRow = curr.pos.first + dRow[i];
int nextCol = curr.pos.second + dCol[i];
if (nextRow < 0 || nextRow >= n) continue;
if (nextCol < 0 || nextCol >= n) continue;
if (found[nextRow][nextCol]) continue;
pq.push({ {nextRow,nextCol},curr.penalty + board[nextRow][nextCol] });
found[nextRow][nextCol] = true;
}
}
cout << "Problem " << cnt << ": " << ans << "\n";
cnt++;
}
}
'알고리즘' 카테고리의 다른 글
백준 5427번 불 C++ (0) | 2022.08.18 |
---|---|
백준 1062번 가르침 C++ (0) | 2022.08.17 |
백준 13913번 숨바꼭질 4 C++ (0) | 2022.08.15 |
백준 5052번 전화번호 목록 C++ (0) | 2022.08.14 |
백준 14402번 가장 긴 증가하는 부분 수열 4 C++ (0) | 2022.08.13 |