1일1알

백준 15558번 점프 게임 C++ 본문

알고리즘

백준 15558번 점프 게임 C++

영춘권의달인 2022. 12. 25. 11:11

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;
}