ACM: 博弈题 poj 1143 状态压缩

Number Game

 

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

 

题意: 2个人玩游戏, 给出你n个数在1~20范围内, 轮流划去一个数, 要求选择的数不是原来已经划去

      的和没有给出的数的线性组合. 如果最后一个人发现没有数字可以划去, 他就输了游戏. 输出

      选择一定可以胜利的那些数字.

 

解题思路:

     1. 看似很复杂的博弈题, 读了几遍题目, 才搞清题意. 简单的说, 1~20这20个数字分成2组, 一组

        是给出可以划去, 另外一组是没得选择, 每次从可以选择的组里面划去一个数之后, 划去的该

        数, 进入另外一组, 但是另外一组的数进行线性组合, 组合的数如果出现在可以选择划去的组,

        就要将它继续放入另一组中.

     2. 出现了状态, 每个组有一个状态, 设dp[i]: 表示在i状态下, 是否可以继续选择划去的数.状态

        依然采用二进制进行状态压缩. 进行枚举模拟, 一人选一个可以划去的数在状态i下. 一个人没得

        选, 另外一个人表示可以继续选. 一个可以选择无法确定另外一个人是否可以选择.

     3. 采用记忆化搜索, 二进制删除和插入元素是很简单.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 21

int n;
int a[MAX];
int dp[1<<MAX], result[MAX];
int State;

int DP(int state, int del) //state当前状态还剩下的数, del已经划去的数
{
 if(state == 0) return 0;
 if( dp[state] != -1 ) return dp[state];

 for(int i = 0; i < n; ++i)
 {
  int tempState = state, temp = del;
  if( (1<<i) & tempState )
  {
   for(int j = 0; j+a[i] < MAX; ++j)
   {
    if( (1<<j) & temp )
     temp |= (1<<(j+a[i]));
   }

   for(int k = 0; k < n; ++k)
   {
    if( ( (1<<k)&tempState ) && ( (1<<a[k])&temp ))
     tempState ^= (1<<k);
   }
   if( !DP(tempState, temp) )
    return dp[state] = 1;
  }
 }
 return dp[state] = 0;
}

int judge()
{
 int num = 0;
 for(int i = 0; i < n; ++i)
 {
  int tempState = (1<<n)-1;
  int temp = State;
  for(int j = 0; j+a[i] < MAX; ++j)
  {
   if( (1<<j) & temp ) //如果j不在可选序列中
    temp |= ( 1 << (j+a[i]) ); //j+a[i]也不在可选序列中
  }

  for(int k = 0; k < n; ++k)
  {
   if( (1<<a[k]) & temp )
    tempState ^= (1<<k);
  }
  if( !DP(tempState, temp) )
   result[num++] = a[i];
 }
 return num;
}

int main()
{
 int i;
// freopen("input.txt", "r", stdin);
 int caseNum = 1;
 while(scanf("%d", &n) != EOF)
 {
  if(n == 0) break;

  State = (1<<MAX)-3; //全部数字设置为没出现, 1除外
  for(i = 0; i < n; ++i)
  {
   scanf("%d", &a[i]);
   State ^= (1<<a[i]); //出现的数字值为0, 表示出现
  }

  sort(a, a+n);
  memset(dp, -1, sizeof(dp));
  int num = judge();
  printf("Test Case #%d\n", caseNum++);
  if(num == 0)
   printf("There's no winning move.\n");
  else
  {
   printf("The winning moves are:");
   for(i = 0; i < num; ++i)
    printf(" %d", result[i]);
   printf("\n");
  }

  printf("\n");
 }

 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值