Count the number of steps in a board game
number-steps.cc NumberSteps.javapublic static int numberSteps(int n, int k) {
if (n <= 1) {
return 1;
}
int steps[] = new int[k + 1];
steps[0] = steps[1] = 1;
for (int i = 2; i <= n; ++i) {
steps[i % (k + 1)] = 0;
for (int j = 1; j <= k && i - j >= 0; ++j) {
steps[i % (k + 1)] += steps[(i - j) % (k + 1)];
}
}
return steps[n % (k + 1)];
}