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
- 유니온 파인드
- 누적 합
- 문자열
- 구현
- 정렬
- 스택
- 자료구조
- Unreal Engine 5
- 알고리즘
- VR
- XR Interaction Toolkit
- 백준
- 브루트포스
- 다이나믹 프로그래밍
- 재귀
- 트리
- 시뮬레이션
- 우선순위 큐
- c++
- Team Fortress 2
- 그리디 알고리즘
- 투 포인터
- 유니티
- 수학
- 그래프
- ue5
- DFS
- 다익스트라
Archives
- Today
- Total
1일1알
백준 13903번 출근 C++ 본문
https://www.acmicpc.net/problem/13903
13903번: 출근
첫 번째 줄에는 보도블록의 세로, 가로 R, C(1 ≤ R, C ≤ 1,000)크기가 주어진다. 다음 R개의 줄에는 C개의 문자로 이루어진 보도블록의 초기 상태가 주어진다. (가로 블록은 0로 표시되고, 세로 블록
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 moveCnt;
};
int r, c, n;
vector<int> dRow;
vector<int> dCol;
vector<vector<int>> board;
vector<vector<bool>> found;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> r >> c;
board = vector<vector<int>>(r, vector<int>(c));
found = vector<vector<bool>>(r, vector<bool>(c, false));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> board[i][j];
}
}
cin >> n;
dRow = vector<int>(n);
dCol = vector<int>(n);
for (int i = 0; i < n; i++) {
cin >> dRow[i] >> dCol[i];
}
queue<Info> q;
for (int i = 0; i < c; i++) {
if (board[0][i] == 1) {
q.push({ 0,i,0 });
found[0][i] = true;
}
}
int ans = -1;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (curr.row == r - 1) {
ans = curr.moveCnt;
break;
}
for (int i = 0; i < n; i++) {
int nextRow = curr.row + dRow[i];
int nextCol = curr.col + dCol[i];
if (nextRow < 0 || nextRow >= r) continue;
if (nextCol < 0 || nextCol >= c) continue;
if (found[nextRow][nextCol]) continue;
if (board[nextRow][nextCol] == 0) continue;
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol,curr.moveCnt + 1 });
}
}
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 16437번 양 구출 작전 C++ (0) | 2023.01.06 |
---|---|
백준 14923번 미로 탈출 C++ (0) | 2023.01.05 |
백준 3980번 선발 명단 C++ (0) | 2023.01.03 |
백준 5567번 결혼식 C++ (0) | 2022.12.30 |
백준 16957번 체스판 위의 공 C++ (0) | 2022.12.29 |