博弈论 HOJ 1122 Number Game

来源:互联网 发布:碧柔防晒知乎 编辑:程序博客网 时间:2024/05/17 17:14

Number Game

My Tags  (Edit)
Source : ACM ICPC Mid-Central European 2000Time limit : 3 secMemory limit : 32 M

Submitted : 77, Accepted : 41

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 file 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 file, 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 52 2 35 2 3 4 5 60
Sample Output
Test Case #1The winning moves are: 2Test Case #2There's no winning move.Test Case #3The winning moves are: 4 5 6

题意:有一个集合,其中包含2~20,然后每一次取一个数字x,那么2*x,3*x 会被去掉,k*x+y  (y是之前去掉的数字)也会被去掉。然后给一个初始的状态,问那些行动是winning move

思路:我们用二进制来表示一个局面,如果一个数还在,就用0表示,不在就用1表示,然后我们只要求出某个状态的sg值,那么答案就能的出来了,如果一个状态的sg值是0,那么这个状态先手必败,没有winning move,如果不是0,那么他的后继状态中一定有一个0的状态,然后找出哪一步能移动到0状态就行了。在状态转移的时候细心一点应该就没什么问题了。

代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define LL long long
#define mp make_pair
const int maxn = (1<<21)+10;
int sg[maxn];

int to_state(int x,int sel)
{
int ret = x;
for (int i = sel ; i <= 20 ; i += sel) if (!(x&(1<<i)))
ret += 1<<i;
for (int i = sel+2 ; i <= 20 ; ++i) if (!(ret&(1<<i)))
{
for (int j = i-sel ; j >= 2 ; j -= sel) if (ret&(1<<j))
{
ret += 1<<i;
break;
}
}
return ret;
}

void SG(int x)
{
if (sg[x]!=-1) return;
bool *vis = new bool [25];
for (int i = 0 ; i <= 20 ; ++i) vis[i] = false;
for (int i = 2 ; i <= 20 ; ++i) if (!(x&(1<<i)))
{
int to = to_state(x,i);
SG(to);
vis[sg[to]] = true;
}
for (int j = 0 ; j <= 20 ; ++j) if (!vis[j])
{
sg[x] = j;
delete [ ] vis;
return;
}
}

int n , s , Case;

void solve()
{
SG(s);
printf("Test Case #%d\n",Case);
if (sg[s]==0)
printf("There's no winning move.\n\n");
else 
{
printf("The winning moves are:");
for (int i = 2 ; i <= 20 ; ++i) if (!(s&(1<<i)))
{
int to = to_state(s,i);
if (sg[to]==0)
printf(" %d",i);
}
printf("\n\n");
}
}


int main()
{
Case = 0;
memset(sg,-1,sizeof(sg));
while (scanf("%d",&n) , n )
{
++Case;
s = ((1<<19)-1)<<2;
int a;
while (n--)
{
scanf("%d",&a);
s -= 1<<a;
}
solve();
}
}
0 0
原创粉丝点击