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

http://poj.org/problem?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 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 状态压缩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;
}

时间: 2025-02-01 12:11:54

hdu1143 状态压缩dp 记忆化搜索写法的相关文章

vijos - P1456最小总代价 (状态压缩DP + 记忆化搜索)

P1456最小总代价 Accepted 标签:[显示标签] 描述 n个人在做传递物品的游戏,编号为1-n. 游戏规则是这样的:开始时物品可以在任意一人手上,他可把物品传递给其他人中的任意一位:下一个人可以传递给未接过物品的任意一人. 即物品只能经过同一个人一次,而且每次传递过程都有一个代价:不同的人传给不同的人的代价值之间没有联系: 求当物品经过所有n个人后,整个过程的总代价是多少. 格式 输入格式 第一行为n,表示共有n个人(16>=n>=2): 以下为n*n的矩阵,第i+1行.第j列表示物

hdu 4628 Pieces(状态压缩+记忆化搜索)

Pieces Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1811    Accepted Submission(s): 932 Problem Description You heart broke into pieces.My string broke into pieces.But you will recover one

poj 1191 棋盘切割 (压缩dp+记忆化搜索)

一,题意: 中文题 二.分析: 主要利用压缩dp与记忆化搜索思想 三,代码: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> using namespace std; const int Big=20000000; int Mat[10][10]; int N; int sum[10][10]; int

poj 1191 棋盘分割 (压缩dp+记忆化搜索)

一,题意: 中文题 二,分析: 主要利用压缩dp与记忆化搜索思想 三,代码: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> using namespace std; const int Big=20000000; int Mat[10][10]; int N; int sum[10][10]; int

poj1664 dp记忆化搜索

http://poj.org/problem?id=1664 Description 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. Input 第一行是测试数据的数目t(0 <= t <= 20).以下每行均包含二个整数M和N,以空格分开.1<=M,N<=10. Output 对输入的每组数据M和N,用一行输出相应的K. Sample Input 1 7 3 Sample Output 8 /

hdu4753 状态压缩dp博弈(记忆化搜索写法)

http://acm.hdu.edu.cn/showproblem.php?pid=4753 Problem Description There is a 3 by 3 grid and each vertex is assigned a number. It looks like JiuGongGe, but they are different, for we are not going to fill the cell but the edge. For instance, adding

poj1179 区间dp(记忆化搜索写法)有巨坑!

http://poj.org/problem?id=1179 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 integer and each edge is labelled with either the symbol + (add

UVa 10817 (状压DP + 记忆化搜索) Headmaster&#39;s Headache

题意: 一共有s(s ≤ 8)门课程,有m个在职教师,n个求职教师. 每个教师有各自的工资要求,还有他能教授的课程,可以是一门或者多门. 要求在职教师不能辞退,问如何录用应聘者,才能使得每门课只少有两个老师教而且使得总工资最少. 分析: 因为s很小,所以可以用状态压缩. dp(i, s1, s2)表示考虑了前i个人,有一个人教的课程的集合为s1,至少有两个人教的集合为s2. 在递归的过程中,还有个参数s0,表示还没有人教的科目的集合. 其中m0, m1, s0, s1, s2的计算用到位运算,还

[hihocoder 1033]交错和 数位dp/记忆化搜索

#1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0,?a1,?...,?an?-?1,定义交错和函数: f(x)?=?a0?-?a1?+?a2?-?...?+?(?-?1)n?-?1an?-?1 例如: f(3214567)?=?3?-?2?+?1?-?4?+?5?-?6?+?7?=?4 给定 输入 输入数据仅一行包含三个整数,l,?r,?k(0?≤?l?≤?r?≤?1018,?|k|