티스토리 뷰

728x90

문제 링크

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>
using namespace std;

int n, m;
int up[111], down[111];
bool chk[111];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        up[x] = y;
    }
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        down[x] = y;
    }
    chk[1] = true;
    queue<int> q;
    q.push(1);
    int ans = -1;
    while (!q.empty()) {
        int qsze = q.size();
        ans++;
        while (qsze--) {
            int now = q.front();
            q.pop();
            if (now == 100) {
                cout << ans;
                return 0;
            }
            for (int k = 1; k <= 6; k++) {
                int next = now + k;
                if (next > 100) continue;
                if (up[next] || down[next])
                    next = up[next] + down[next];

                if (chk[next]) continue;
                chk[next] = true;
                q.push(next);
            }
        }
    }
}
728x90
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
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 29 30 31
글 보관함