-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModulo Solitaire.c
38 lines (31 loc) · 915 Bytes
/
Modulo Solitaire.c
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
#include <stdio.h>
int modulo_solitaire(int m, int n, int k, int pairs[][2]) {
// Create an array of length m+1 and initialize all elements to 0
int dp[m+1];
for (int i = 0; i <= m; i++) {
dp[i] = 0;
}
// Iterate over the pairs and update the dp array
for (int i = 0; i < n; i++) {
int a = pairs[i][0];
int b = pairs[i][1];
for (int j = 0; j <= m; j++) {
if (dp[j] == i) {
dp[(j + a) % m] = i + 1;
dp[(j + b) % m] = i + 1;
}
}
}
// Return the minimum number of moves required to win the game
return dp[k];
}
int main() {
int m, n, k;
scanf("%d %d %d", &m, &n, &k);
int pairs[n][2];
for (int i = 0; i < n; i++) {
scanf("%d %d", &pairs[i][0], &pairs[i][1]);
}
printf("%d\n", modulo_solitaire(m, n, k, pairs));
return 0;
}