POJ 1143 记忆化搜索+博弈论

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 }
时间: 2025-01-04 09:00:22

POJ 1143 记忆化搜索+博弈论的相关文章

poj 1088 记忆化搜索

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 90368   Accepted: 34028 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

hdu 4111 Alice and Bob 记忆化搜索 博弈论

Alice and Bob Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4111 Description Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The most amazing thing is that they

poj 1351 记忆化搜索

//好久没水题了,水一发记忆化好了= ̄ω ̄= 1 #include "iostream" 2 #include "cstdio" 3 #include "cstring" 4 #include "algorithm" 5 #include "cmath" 6 using namespace std; 7 __int64 dp[20][5][2][2]; 8 bool vis[20][5][2][2]; 9

Test for Job (poj 3249 记忆化搜索)

Language: Default Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9733   Accepted: 2245 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's

POJ 1179 记忆化搜索

Polygon Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5024   Accepted: 2108 Description Polygon is a game for one player that starts on a polygon with N vertices, like the one in Figure 1, where N=4. Each vertex is labelled with an int

CF 39E. What Has Dirichlet Got to Do with That?(记忆化搜索+博弈论)

传送门 解题思路 首先很好写出一个\(O(ab)\)的记搜,但发现这样无法处理\(a=1\)和\(b=1\)的情况,这两种情况需要特判.首先\(a=1\)的情况,就是如果当前选手让\(a+1\)必胜,那么他一定会给\(a+1\),否则会给\(b+1\),如果到\(2^b>n\)时,说明谁动\(a\)谁输,就是平局了,这样的话可以模拟这个过程不断给\(b+1\)即可.第二种情况是\(b=1\),这种情况一定没有平局,那么就和刚才一样模拟即可.最后一种情况是\(a=1\)并且\(b=1\),这样的话

POJ 1579 Function Run Fun 【记忆化搜索入门】

题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20560   Accepted: 10325 Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c): if a <=

poj1191 分治思想,记忆化搜索

http://poj.org/problem?id=1191 Description 将一个8*8的棋盘进行如下分割:将原棋盘割下一块矩形棋盘并使剩下部分也是矩形,再将剩下的部分继续如此分割,这样割了(n-1)次后,连同最后剩下的矩形棋盘共有n块矩形棋盘.(每次切割都只能沿着棋盘格子的边进行) 原棋盘上每一格有一个分值,一块矩形棋盘的总分为其所含各格分值之和.现在需要把棋盘按上述规则分割成n块矩形棋盘,并使各矩形棋盘总分的均方差最小. 均方差,其中平均值,xi为第i块矩形棋盘的总分. 请编程对给

POJ 2704 Pascal&#39;s Travels (基础记忆化搜索)

Pascal's Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5328   Accepted: 2396 Description An n x n game board is populated with integers, one nonnegative integer per square. The goal is to travel along any legitimate path from t