POJ 1143 -- Number Game

Number Game

 

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3825   Accepted: 1581

 

Description

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.)

 

Input

 

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.

 

Output

 

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.

 

Sample Input

2 2 5
2 2 3
5 2 3 4 5 6
0

Sample Output

Test Case #1
The winning moves are: 2

Test Case #2
There's no winning move.

Test Case #3
The winning moves are: 4 5 6

Source


几个小知识点

1.~在c++中的意思

'~'是按位取反的意思。

举例说明:

st=10

(10)2=(0)1010   PS:第一位是符号位

~st=(1)0101+1=(1)1011=-11  PS:符号位变了

2.抑或的作用

由于时间限制的较严,因此本题采用抑或的办法来储存数据。利用抑或1^0=1的性质可以在state中储存所有剩下的数。至于为什么要先-1,大概是这样储存才能得到正确的结果。另外在循环中还有一个类似的变量叫st,是会随着不同的策略而改变的,因此每一个新的循环都要重新将其赋为state。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #define MAX(a,b) (a) > (b)? (a):(b)
 6 #define MIN(a,b) (a) < (b)? (a):(b)
 7 #define mem(a) memset(a,0,sizeof(a))
 8 #define INF 1000000007
 9 #define MAXN 1<<20
10 #define MACN 20
11 //~st是按位取反 
12 #define JUDGE (((1<<(ma[j]-ma[i]-1)) & ~st) && (ma[j]-ma[i] != 1)) || (!((ma[j]+1)%(ma[i]+1)))  //??? 
13 
14 using namespace std;
15 
16 int DP[MAXN],ma[MACN],vis[MAXN];
17 int N;
18 
19 int  search(int k,int state)
20 {
21     if(vis[state])return DP[state];   //之前已访问过这种情况,无需再算一遍 
22     vis[state]=1;
23     if(state==0)return 0;  //已经没有剩下的数,显然就输了  
24     if(k==1)return DP[state]=1;//集合里面只有一个数了,一定是必胜状态
25     int key=0;
26     for(int i=0;i<N;i++)
27     {
28         int st=state;
29         if((1<<ma[i]) & state)  //遍历找到还没取过的数 
30         {
31             for(int j=i+1;j<N;j++)//首先去掉 ma[i]的倍数和(ma[j]-ma[i])已经使用了的 ma[j]
32             {
33                 if( ((1<<ma[j]) & st) && (JUDGE) )
34                 {
35                     st^=(1<<ma[j]);
36                 }
37             }
38             key=search(k-1, st^(1<<ma[i]));
39             if(!key)return DP[state]=1;//一旦下一个状态有必输的状态,那这个状态就是必胜的
40         }
41     }
42     return DP[state]=0;
43 }
44 
45 int main()
46 {
47     int cas=1;
48     while(~scanf("%d",&N) && N)  //上面这种写法,如果不能从标准输入解析时(例如输入了字母),函数返回0,就会继续要求输入。 
49     {
50         mem(vis);
51         int state=0;
52         int ans[MACN]={0},count=0;
53 
54         for(int i=0;i<N;i++)  //state是一串二进制数,其中为1的位置刚好是从右往左数输入的ma[i]所在位置 (如:输入 2 5,则state=10010) 
55         {
56             scanf("%d",&ma[i]);
57             ma[i]--;
58             state ^= (1<<ma[i]);  //将 1左移 ma[i]位 ,之后和 state进行抑或运算
59         }
60 
61         sort(ma,ma+N);
62         for(int i=0;i<N;i++)
63         {
64             int st=state;
65             for(int j=i+1;j<N;j++)
66             {
67                 if(JUDGE) st ^= (1<<ma[j]);   //在状态中去除当前位置的数 
68             }
69             if(search(N-1, (1<<ma[i])^st) == 0)  //是必胜状态 
70             {
71                 ans[count++]=ma[i]+1;
72             }
73         }
74 
75         printf("Test Case #%d
", cas++);
76         if(!count)printf("There's no winning move.

");
77         else {
78             printf("The winning moves are:");
79             for(int i=0;i<count;i++)
80                 printf(" %d", ans[i]);
81             printf("

");
82         }
83     }
84     //system("pause");
85     return 0;
86 }
POJ1143
原文地址:https://www.cnblogs.com/YXY-1211/p/7137101.html