Number Game

来源:互联网 发布:北京东路的日子 知乎 编辑:程序博客网 时间:2024/05/19 02:00

题目描述

Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take turns choosing integers greater than 1. First, Christine chooses a number, then Matt chooses a number, then Christine again, and so on. The following rules restrict how new numbers may be chosen by the two players: •A number which has already been selected by Christine or Matt, or a multiple of such a number,cannot be chosen. •A sum of such multiples cannot be chosen, either. If a player cannot choose any new number according to these rules, then that player loses the game. Here is an example: Christine starts by choosing 4. This prevents Matt from choosing 4, 8, 12, etc.Let‘s assume that his move is 3. Now the numbers 3, 6, 9, etc. are excluded, too; furthermore, numbers like: 7 = 3+4;10 = 2*3+4;11 = 3+2*4;13 = 3*3+4;... are also not available. So, in fact, the only numbers left are 2 and 5. Christine now selects 2. Since 5=2+3 is now forbidden, she wins because there is no number left for Matt to choose. Your task is to write a program which will help play (and win!) the Number Game. Of course, there might be an infinite number of choices for a player, so it may not be easy to find the best move among these possibilities. But after playing for some time, the number of remaining choices becomes finite, and that is the point where your program can help. Given a game position (a list of numbers which are not yet forbidden), your program should output all winning moves. A winning move is a move by which the player who is about to move can force a win, no matter what the other player will do afterwards. More formally, a winning move can be defined as follows. •A winning move is a move after which the game position is a losing position. •A winning position is a position in which a winning move exists. A losing position is a position in which no winning move exists. •In particular, the position in which all numbers are forbidden is a losing position. (This makes sense since the player who would have to move in that case loses the game.)

输入

The input consists of several test cases. Each test case is given by exactly one line describing one position. Each line will start with a number n (1 <= n <= 20), the number of integers which are still available. The remainder of this line contains the list of these numbers a1,...,an(2 <= ai <= 20). The positions described in this way will always be positions which can really occur in the actual Number Game. For example, if 3 is not in the list of allowed numbers, 6 is not in the list, either. At the end of the input, there will be a line containing only a zero (instead of n); this line should not be processed.

输出

For each test case, your program should output "Test case #m", where m is the number of the test case (starting with 1). Follow this by either "There‘s no winning move." if this is true for the position described in the input file, or "The winning moves are: w1 w2 ... wk" where the wi are all winning moves in this position, satisfying wi < wi+1 for 1 <= i < k. After this line, output a blank line.

样例输入

2 2 52 2 35 2 3 4 5 60

样例输出

Test Case #1The winning moves are: 2Test Case #2There‘s no winning move.Test Case #3The winning moves are: 4 5 6

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[(1<<20)+2];//1:胜;0:负int a[25],n;bool mark[25];int update(int state,int k){    int st=state^(1<<k),p=a[k],t;    mark[p]=false;    for(int i=0;i<n;i++)    {        if(st&(1<<i))        {            t=a[i]-p;            if(t>1&&!mark[t])            {                mark[a[i]]=false;                st^=(1<<i);            }        }    }    return st;}void resume(int state,int org){    for(int i=0;i<n;i++)    {        int t=1<<i;        if(!(state&t)&&(org&t))            mark[a[i]]=true;    }}int dfs(int state){    if(dp[state]!=-1)        return dp[state];    for(int i=0;i<n;i++)    {        if(state&(1<<i))        {            int st=update(state,i);            if(dfs(st)==0)            {                resume(st,state);                return dp[state]=1;            }            resume(st,state);        }    }    return dp[state]=0;}int main(){    int ca=0;    while(scanf("%d",&n),n)    {        int state=0;        memset(mark,false,sizeof(mark));        memset(dp,-1,sizeof(dp));        dp[0]=0;        for(int i=0;i<n;i++)            scanf("%d",&a[i]),mark[a[i]]=true,state|=(1<<i);        sort(a,a+n);        int top=0,ans[25];        for(int i=0;i<n;i++)        {            int st=update(state,i);            if(dfs(st)==0)                ans[top++]=a[i];            resume(st,state);        }        printf("Test Case #%d\n",++ca);        if(top==0)            printf("There's no winning move.\n\n");        else        {            printf("The winning moves are:");            for(int i=0;i<top;i++)                printf(" %d",ans[i]);            printf("\n\n");        }    }    return 0;}



0 0
原创粉丝点击