首页 > 其他 > 详细

POJ 1143 记忆化搜索+博弈论

时间:2015-02-03 22:47:14      阅读:500      评论:0      收藏:0      [点我收藏+]
Number Game
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3181   Accepted: 1280

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
 

POJ 1143解题思路:

1.从当前的状态推断下一个状态,如果下一个状态中会出现必败态,则此状态会为必胜态,否则下一个状态中不会出现必败态,则此状态为必胜态,这是博弈论最基本的东西;

2.假设此次要移动当前还存在的某一个数字,则可以移动掉一部分数字得到下一个状态,根据下一个状态推断出此次状态;

3.对每一个状态中相应数字的出现用二进制表示,用快速幂可以很快得到;

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 const int maxn=21;
 8 bool in[maxn];
 9 int status[1<<21];
10 int ans[maxn];
11 int n;
12 int get_index(bool a[])//快速幂运算
13 {
14     int index=0;
15     for(int i=2;i<=20;i++)
16     {
17          if(a[i]) index=index|1;
18         index=index<<1;
19     }
20     return index=index>>1;
21 }
22 bool dfs(bool a[],int point)//假设该次移动的是point
23 {
24     bool aa[maxn];
25     memcpy(aa, a, 21);
26     aa[point]=false;
27     for(int i=2;i+point<=20;i++)
28     {
29         if(!aa[i]) aa[i+point]=false;//如果在当前的队列中i已经不存在,则i+point也不存在
30     }
31     int index=get_index(aa);
32     if(status[index]>0) return true;//移动point得到必胜太
33     if(status[index]<0) return false;//移动point得到必败太
34     for(int i=2;i<=20;i++)//从移动point后得到的状态中可以得到一个必败太,则移动point后得到的是必胜太
35     {
36         if(aa[i]&&!dfs(aa,i)){
37             status[index]=1;
38             return true;
39         }
40     }
41     status[index]=-1;
42     return false;
43 }
44 int main()
45 {
46     // freopen("in.txt","r",stdin);
47     int aa;int cnt=1;
48     while(~scanf("%d",&n)){
49         if(n==0) break;
50         memset(in,0,sizeof(in));
51         for(int i=0;i<n;i++)
52         {
53             scanf("%d",&aa);
54             in[aa]=1;
55         }
56         int index=get_index(in);
57         int top=0;
58         for(int i=2;i<=20;i++)//移动掉i后可以得到必败太,此状态为必胜太
59         {
60             if(in[i]&&!(dfs(in,i))){
61                 ans[top++]=i;
62             }
63         }
64         printf("Test Case #%d\n",cnt++);
65         if(top==0){
66             status[index]=-1;
67             printf("There‘s no winning move.\n");
68         }
69         else{
70               printf("The winning moves are:");
71               for(int i=0;i<top;i++)
72               {
73                   printf(" %d",ans[i]);
74               }
75               printf("\n");
76         }
77         printf("\n");
78     }
79     return 0;
80 }

 

POJ 1143 记忆化搜索+博弈论

原文:http://www.cnblogs.com/codeyuan/p/4271054.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!