(弃疗搁置T)poj 1143 记忆化搜索+状压dp+dfs

Number Game
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3078 Accepted: 1235

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

TLE代码:
不知道为什么连记忆化搜索都用上了还是T。
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 1000005
#define mod 100007

using namespace std;
typedef long long ll;
bool aa[25];
int dp[1<<20][2];

int dfs(int first,int x,int pre,int a[],int times)
{
    int cur=pre-(1<<(x-1));
    if(dp[cur][times%2]!=-1){
        return dp[cur][times%2];
    }
    vector<int> g;
    a[x]=0;
    g.push_back(x);

    int countt=0;
    /*while(tmp<=20){     //消去倍数
        if(a[tmp]==1){
            a[tmp]=0;
            g.push_back(tmp);
        }
        tmp+=x;
    }*/

    for(int i=2;i<=20;i+=1){  //消
        if(a[i]==0){
            int tmp=x+i;
            if(a[tmp]==1){
                a[tmp]=0;
                g.push_back(tmp);
            }
        }
    }

    int flag=0;
    for(int i=2;i<=20;i+=1){   //找出还未被消去的数进行递归
        if(a[i]==1){
            if(dfs(first,i,cur,a,times+1)){
                flag=1;
            }
            countt+=1;
        }
    }
    int sz=g.size();
    for(int i=0;i<sz;i+=1){
        a[g[i]]=1;
    }

    if(!countt){
        return dp[cur][times%2]=(times%2!=0?1:0);
    }
    return dp[cur][times%2]=(times%2!=0?flag:!flag);
}

int main()
{
    int n,t=0;
    memset(dp,-1,sizeof(dp));
    while(scanf("%d",&n)!=EOF){
        int cur=0;
        if(!n) break;

        t+=1;
        int flag=0;
        int a[25];
        memset(aa,false,sizeof(aa));
        memset(a,0,sizeof(a));
        for(int i=0;i<n;i+=1){
            int num;
            scanf("%d",&num);
            cur|=(1<<(num-1));
            a[num]=1;
        }
        for(int i=2;i<=20;i+=1){
            if(a[i]==1&&dfs(i,i,cur,a,1)){
                aa[i]=true;
                flag=1;
            }
        }
        printf("Test Case #%d\n",t);
        if(!flag){
            printf("There's no winning move.\n");
            continue;
        }
        printf("The winning moves are: ");
        for(int i=2;i<=20;i+=1){
            if(aa[i]==true){
                printf("%d ",i);
            }
        }
        printf("\n");
    }

    return 0;
}




别人的ac代码:
#include<stdio.h>
#include<math.h>

int dp[1048600];

int find(int a[],int n,int k)
{
	int i,b[21];
	for(i=2;i<21;i++)
		b[i]=a[i];
	b[n]=0;
	k=k-(int )pow(2,n-2);
	for(i=2;i<21-n;i++)
	{
		if(!b[i])
			if(b[i+n])
			{
				b[i+n]=0;
				k=k-(int )pow(2,i+n-2);
			}
	}
	if(dp[k]!=-1) return dp[k];
	for(i=2;i<21;i++)
		if(b[i])
			break;
	if(i<21)
	{
		for(i=2;i<21;i++)
		{
			if(b[i])
				if(find(b,i,k))
					break;
		}
		if(i<21)
			return dp[k]=0;
		else
			return dp[k]=1;
	}
	else
	{
		return dp[k]=1;
	}
}

int main()
{
	int i,n,num,count,k,a[21],b[21],Case=0;
	for(i=0;i<1048600;i++)
		dp[i]=-1;
	while(scanf("%d",&n)&&n)
	{
		Case++;
		for(i=2;i<21;i++)
			a[i]=0;
		k=0;
		for(i=0;i<n;i++)
		{
			scanf("%d",&num);
			a[num]=1;
			k=k+(int )pow(2,num-2);
		}
		count=0;
		for(i=2;i<=20;i++)
		{
			if(a[i])
			{
				if(find(a,i,k))
				{
					b[count]=i;
					count++;
				}
			}
		}
		if(count>0)
		{
			printf("Test Case #%d\nThe winning moves are:",Case);
			for(i=0;i<count;i++)
				printf(" %d",b[i]);
			putchar('\n');
		}
		else
		{
			printf("Test Case #%d\nThere's no winning move.\n",Case);
		}
		putchar('\n');
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值