뜌릅

뱀과 사다리 게임 16928번 [백준] 본문

카테고리 없음

뱀과 사다리 게임 16928번 [백준]

TwoCastle9 2022. 9. 6. 00:47
반응형

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

 

16928번: 뱀과 사다리 게임

첫째 줄에 게임판에 있는 사다리의 수 N(1 ≤ N ≤ 15)과 뱀의 수 M(1 ≤ M ≤ 15)이 주어진다. 둘째 줄부터 N개의 줄에는 사다리의 정보를 의미하는 x, y (x < y)가 주어진다. x번 칸에 도착하면, y번 칸으

www.acmicpc.net

 

bfs을 사용하여 손쉽게 풀 수 있는 문제입니다.

 

#include<bits/stdc++.h>
#include<sys/types.h>


using namespace std;
#define endl '\n'
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i, j) for(ll i=0;i<j;i++)
#define mrep(i, j, k) for(ll i = j; i<=k;i++)
#define pb push_back
#define pii pair<int,int>
#define ff first
#define ss second
typedef long long ll;

int n, m, x, y;
bool visited[101] = {false,};
vector<int> matrix[101];

void solve() {
    cin >> n >> m;
    rep(i, n) {
        cin >> x >> y;
        matrix[x].pb(y);
    }
    rep(i, m) {
        cin >> x >> y;
        matrix[x].pb(y);
    }

    queue<pii > q;
    q.push({1, 0});
    visited[1] = true;
    while (!q.empty()) {
        auto cur = q.front();
        q.pop();
        visited[cur.ff] = true;
        if (cur.ff == 100) {
            cout << cur.ss << endl;
            return;
        }
        mrep(i, 1, 6) {
            int next = cur.ff + i;
            if (next < 1 || next > 100)continue;
            if (visited[next])continue;
            if (!matrix[next].empty())q.push({matrix[next][0], cur.ss + 1});
            else
                q.push({next, cur.ss + 1});
        }
    }
    cout << "ADFS" << endl;
}

int main() {
    fast_io;
    solve();
    return 0;
}

 

반응형