hdu1143 状态压缩dp 记忆化搜索写法

?id=1143

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 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/**poj 1143 状态压缩dp题目大意:给定一组数据a[],2~20之内的数在数组里出现过的是可以选的,一个数一旦被选,那么它的倍数以及与其倍数(整数倍)与所有已选数的倍数(整数倍)的和相等的数不能再选,两人轮流选择,问是否有一个数据第一个人选了之后无论第二个人怎么选第一个人都会胜的数。(二者都会采取最优选法)解题思路:题目只有20个数,因此我们把所有的状态压缩到一个二进制数里,然后枚举选择哪个数,,如果选择该数后,另一个人没有可以胜的选法,那么该数字就是一个可行点。*/#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;int a[25],ans[25],dp[(1<<20)+3];int n;bool vis[(1<<20)+3];bool judge(int i,int j,int st){return ((1<<(a[j]-a[i]-1))&(~st))&&(a[j]-a[i]!=1)||((a[j]+1)%(a[i]+1)==0);}bool solve(int num,int state)///um:还剩多少数字可选,state:当前选择情况,二进制压缩的数{if(vis[state])return dp[state];///记忆化,如果已经判断过直接返回vis[state]=true;if(state==0)return 0;///没数可选,必输if(num==1) return dp[state]=1;///只剩一个数,必胜int key=0;for(int i=0;i<n;i++){int st=state;if((1<<a[i])&state)///枚举当前去掉的点{for(int j=i+1;j<n;j++){if(((1<<a[j])&st)&&judge(i,j,st))///去除选择i点后i的倍数点和其与已选数倍数和的组成点{st^=(1<<a[j]);}}key=solve(num-1,st^(1<<a[i]));if(!key)return dp[state]=1;///若后者必输,则i点可行}}return dp[state]=0;}int main(){int tt=0;while(~scanf("%d",&n)){if(n==0)break;memset(vis,0,sizeof(vis));memset(a,0,sizeof(a));int state=0,count=0;for(int i=0;i<n;i++){scanf("%d",&a[i]);a[i]–;state^=(1<<a[i]);}sort(a,a+n);for(int i=0;i<n;i++){int st=state;for(int j=i+1;j<n;j++){if(judge(i,j,st)&&(st&(1<<a[j])))st^=(1<<a[j]);}if(solve(n-1,(1<<a[i])^st)==0){ans[count++]=a[i]+1;}}printf("Test Case #%d\n",++tt);if(!count)printf("There's no winning move.\n\n");else{printf("The winning moves are:");for(int i=0;i<count;i++){printf(" %d",ans[i]);}printf("\n\n");}}return 0;}

如果心在远方,只需勇敢前行,梦想自会引路,

hdu1143 状态压缩dp 记忆化搜索写法

相关文章:

你感兴趣的文章:

标签云: