Repository navigation
Expand file tree
/
Copy pathmidE.cpp
More file actions
72 lines (60 loc) · 1.47 KB
/
Copy pathmidE.cpp
File metadata and controls
72 lines (60 loc) · 1.47 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
long long int mod(long long int a, long long int b)
{
return (a + b) % b;
}
int bfsMinDiceRolls(long long int n, long long int x, const vector<long long int>& diceSides)
{
vector<bool> visited(n + 1, false);
queue<pair<long long int, int>> q; //(current position, number of rolls)
q.push({1, 0}); // Start from square 1 with 0 rolls
while (!q.empty())
{
long long int position = q.front().first;
int rolls = q.front().second;
q.pop();
if (position == x)
{
return rolls;
}
// Try each possible move
for (long long int move : diceSides)
{
long long int newPosition = mod(position * move, n);
if (newPosition == 0)
{
newPosition = n;
}
if (!visited[newPosition])
{
visited[newPosition] = true;
q.push({newPosition, rolls + 1});
}
}
}
return -1;
}
int main()
{
long long int n, x, k;
cin >> n >> x >> k;
vector<long long int> diceSides(k);
for (int i = 0; i < k; ++i)
{
cin >> diceSides[i];
}
int result = bfsMinDiceRolls(n, x, diceSides);
if (result == -1)
{
cout << "I LOVE TSING HUA" << endl;
}
else
{
cout << result << endl;
}
return 0;
}