UVa 10404 Bachet's Game(简单DP)
题意:
一堆石子,两个人每次可以拿X个,X有m种拿法。拿到最后石子的人获胜。
思路:
简单的状态递推dp,dp[i]=true表示有i个石子,首先选的人可以获胜。
#include <cstdio> #include <cstdlib> #include <cstring> bool dp[1000010]; int main() { int n, m, a[12]; while (scanf("%d", &n) != EOF) { scanf("%d", &m); for (int i = 0; i < m; ++i) scanf("%d", &a[i]); dp[0] = false; for (int i = 1; i <= n; ++i) { bool flag = false; for (int j = 0; j < m; ++j) if (i >= a[j] && !dp[i-a[j]]) { flag = true; break; } dp[i] = flag; } if (dp[n]) printf("Stan wins\n"); else printf("Ollie wins\n"); } return 0; }
-------------------------------------------------------
kedebug
Department of Computer Science and Engineering,
Shanghai Jiao Tong University
E-mail: kedebug0@gmail.com
GitHub: http://github.com/kedebug
-------------------------------------------------------