알고리즘
백준 17391번 무한부스터 C++
영춘권의달인
2023. 1. 9. 13:43
https://www.acmicpc.net/problem/17391
17391번: 무한부스터
카트라이더를 처음 시작하는 카린이 정범이는 어려운 조작법에 실망감이 커져가고 있다. 드리프트, 순간 부스터, 커팅, 톡톡이 등등 어려운 테크닉에 질린 정범이는 그나마 쉬운 ‘숭고한 무한
www.acmicpc.net
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;
struct Info {
int row;
int col;
int boost;
int moveCnt;
};
int dRow[2] = { 0,1 };
int dCol[2] = { 1,0 };
int n, m;
vector<vector<int>> board;
vector<vector<bool>> found;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
board = vector<vector<int>>(n, vector<int>(m));
found = vector<vector<bool>>(n, vector<bool>(m, false));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> board[i][j];
}
}
queue<Info> q;
q.push({ 0,0,board[0][0],0 });
found[0][0] = true;
int ans = -1;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (curr.row == n - 1 && curr.col == m - 1) {
ans = curr.moveCnt;
break;
}
for (int i = 0; i < 2; i++) {
for (int j = 1; j <= curr.boost; j++) {
int nextRow = curr.row + dRow[i] * j;
int nextCol = curr.col + dCol[i] * j;
if (nextRow >= n || nextCol >= m) continue;
if (found[nextRow][nextCol]) continue;
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol,board[nextRow][nextCol],curr.moveCnt + 1 });
}
}
}
cout << ans;
}