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
- ue5
- 문자열
- 알고리즘
- 유니온 파인드
- VR
- Team Fortress 2
- 구현
- 우선순위 큐
- 시뮬레이션
- Unreal Engine 5
- XR Interaction Toolkit
- BFS
- DFS
- 누적 합
- 백준
- 백트래킹
- 스택
- 재귀
- 그리디 알고리즘
- 자료구조
- 다이나믹 프로그래밍
- 트리
- 다익스트라
- 투 포인터
- c++
- 정렬
- 브루트포스
- 수학
- 유니티
- 그래프
Archives
- Today
- Total
1일1알
백준 15558번 점프 게임 C++ 본문
https://www.acmicpc.net/problem/15558
15558번: 점프 게임
첫째 줄에 N과 k가 주어진다. (1 ≤ N, k ≤ 100,000) 둘째 줄에는 왼쪽 줄의 정보가 주어진다. i번째 문자가 0인 경우에는 위험한 칸이고, 1인 경우에는 안전한 칸이다. 셋째 줄에는 오른쪽 줄의 정보
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;
};
vector<vector<int>> board;
vector<vector<bool>> found;
int n, k;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k;
board = vector<vector<int>>(2, vector<int>(n));
found = vector<vector<bool>>(2, vector<bool>(n, false));
for (int i = 0; i < 2; i++) {
string str;
cin >> str;
for (int j = 0; j < n; j++) {
board[i][j] = str[j] - '0';
}
}
int ans = 0;
queue<Info> q;
q.push({ 0,0,0 });
found[0][0] = true;
while (!q.empty()) {
auto curr = q.front();
q.pop();
int nextRow;
int nextCol;
int nextMoveCnt = curr.moveCnt + 1;
nextRow = curr.row;
nextCol = curr.col + 1;
if (nextCol >= n) {
ans = 1;
break;
}
if (found[nextRow][nextCol] == false) {
if (board[nextRow][nextCol] == 1 && nextCol >= nextMoveCnt) {
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol,nextMoveCnt });
}
}
nextRow = curr.row;
nextCol = curr.col - 1;
if (nextCol >= 0 && found[nextRow][nextCol] == false) {
if (board[nextRow][nextCol] == 1 && nextCol >= nextMoveCnt) {
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol,nextMoveCnt });
}
}
nextRow = (curr.row + 1) % 2;
nextCol = curr.col + k;
if (nextCol >= n) {
ans = 1;
break;
}
if (found[nextRow][nextCol] == false) {
if (board[nextRow][nextCol] == 1 && nextCol >= nextMoveCnt) {
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol,nextMoveCnt });
}
}
}
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 17265번 나의 인생에는 수학과 함께 C++ (0) | 2022.12.28 |
---|---|
백준 12845번 모두의 마블 C++ (0) | 2022.12.27 |
백준 19640번 화장실의 규칙 C++ (0) | 2022.12.24 |
백준 20920번 영단어 암기는 괴로워 C++ (0) | 2022.12.23 |
백준 16924번 십자가 찾기 C++ (0) | 2022.12.22 |