BFS를 1~6으로 전진해가며 하면 된다고 생각했다. 그러다가 뱀 또는 사다리를 만나면 바로 이동해주면 된다고 생각했다.
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int visted[101];
int jump[101];
int main()
{
ios::sync_with_stdio(0), cin.tie(0);
int N, M;
cin >> N >> M;
queue<int> q;
for (int i = 0; i < N + M; ++i)
{
int a, b;
cin >> a >> b;
jump[a] = b;
}
q.push(1);
while (!q.empty())
{
int cur = q.front();
if (cur == 100)
break;
q.pop();
for (int i = 1; i <= 6; ++i)
{
int next = cur + i;
if (next > 100)
continue;
if (jump[next])
{
next = jump[next];
}
if (visted[next])
continue;
visted[next] = visted[cur] + 1;
q.push(next);
}
}
cout << visted[100];
return 0;
}
사다리, 뱀이 있다면 무조건 타는 것이 중요하다.
if문을 실수하여 사다리, 뱀을 탄 뒤에도 주사위를 돌릴 수 있게 작성하여서 틀렸다.