Ultraviolet A 1559-nim
Question Link
In the beginning, there were s stones. 2n people took turns to take stones. Each person had a maximum number. 2n people had an odd number and an even number. They lost in the last stone, ask who wins
Train of Thought: memory-based search. when each person obtains the corresponding successor State, if there is a mandatory defeat state, the state is mandatory. If all are mandatory, the state is mandatory.
Code:
#include <stdio.h>#include <string.h>int n, s, m[25], dp[25][10005];int dfs(int now, int state) {if (dp[now][state] != -1) return dp[now][state];if (state == 0) return dp[now][state] = 1;dp[now][state] = 0;for (int i = 1; i <= m[now]; i++) {if (state < i) break;if (!dfs((now + 1) % (2 * n), state - i))dp[now][state] = 1; } return dp[now][state];}int main() {while (~scanf("%d", &n) && n) {memset(dp, -1, sizeof(dp));scanf("%d", &s);for (int i = 0; i < 2 * n; i++)scanf("%d", &m[i]);printf("%d\n", dfs(0, s) ? 1 : 0); }return 0;}