Ultraviolet A 1559-nim (game DP)

Source: Internet
Author: User
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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.