16928번: 뱀과 사다리 게임

myeongrangcoding·2023년 12월 18일
0

백준

목록 보기
34/47

https://www.acmicpc.net/problem/16928

풀이

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <queue>

using namespace std;

int Board[101];
int CheckBoard[101];

int main()
{
    ios_base::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    //freopen("input.txt", "rt", stdin);

    int N{}, M{};
    cin >> N >> M;

    int x{}, y{};
    for (int i = 0; i < N; ++i)
    {
        cin >> x >> y;

        Board[x] = y;
    }
    for (int i = 0; i < M; ++i)
    {
        cin >> x >> y;

        Board[x] = y;
    }

    queue<pair<int, int>> Q;
    Q.push(make_pair(1, 0));

    CheckBoard[1] = 1;

    while (!Q.empty())
    {
        int CurrentPos = Q.front().first;
        int CurrentCost = Q.front().second;

        Q.pop();

        if (100 == CurrentPos)
        {
            cout << CurrentCost;
            return 0;
        }

        for (int i = 1; i <= 6; ++i)
        {
            int CheckPos = CurrentPos + i;

            if (100 < CheckPos || CheckBoard[CheckPos]) continue;
            if (Board[CheckPos])
            {
                CheckPos = Board[CheckPos];
            }

            Q.push(make_pair(CheckPos, CurrentCost + 1));
            CheckBoard[CheckPos] = 1;
        }
    }

    return 0;
}
profile
명랑코딩!

0개의 댓글