Number Game_状态压缩
Description
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
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
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
【题意】两个人玩游戏,给出2~20中的几个数,取出一个数后去掉该数,其倍数也去掉,已经去掉的数和当前的数相加的和如果存在数组中也去掉;
求先选哪些数会赢;
【思路】
状态压缩
要从一个状态里去掉某个位置的数 state&=~(1<<(i))
要给一个状态加入某个位置的数state|=(1<<i)
判断一个状态里是否包含某个位置的数 if(state&(1<<i)) 为1则包含
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int dp[<<];
int get_ans(int state,int x)
{
int tmp=state;
for(int i=x; i<=; i+=x)//将倍数去掉;
{
tmp&=~(<<(i-));
}
for(int i=; i<=; i++)//假设某个数在这个集合里,那么用它不断减去x, 如果得到的差值不在这个集合里,那么这个数是非法的,所以要去掉。
{
if(tmp&(<<(i-)))
for(int j=x; i-j->=; j+=x)
{
if(!(tmp&(<<(i-j-))))
{
tmp&=~(<<(i-));
break;
}
}
}
return tmp;
}
int dfs(int state)
{
if(dp[state]!=-) return dp[state];
for(int i=; i<=; i++)
{
if(state&(<<(i-)))
{
if(dfs(get_ans(state,i))==)//等于零说明没得选了,赢了
return dp[state]=;
}
}
return dp[state]=;
}
int main()
{
int cas=;
int a[],n;
while(scanf("%d",&n)!=EOF,n)
{
int state=;
memset(dp,-,sizeof(dp));
for(int i=; i<=n; i++)
{
scanf("%d",&a[i]);
state|=(<<(a[i]-));
}
printf("Test Case #%d\n",cas++);
if(!dfs(state)) printf("There's no winning move.");
else
{
printf("The winning moves are:");
for(int i=; i<=n; i++)
{
if(dfs(get_ans(state,a[i]))==)
printf(" %d",a[i]);
} }
cout<<endl<<endl;
}
return ;
}
Number Game_状态压缩的更多相关文章
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)
B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as inte ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- SRM 513 2 1000CutTheNumbers(状态压缩)
SRM 513 2 1000CutTheNumbers Problem Statement Manao has a board filled with digits represented as St ...
- ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩
HDU 5418 Victor and World Time Limit:2000MS Memory Limit:131072KB 64bit IO Format:%I64d & ...
- [ACM_动态规划] 轮廓线动态规划——铺放骨牌(状态压缩1)
Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, af ...
- codeforces 713A A. Sonya and Queries(状态压缩)
题目链接: A. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input st ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
随机推荐
- java基础学习之 消息对话款
package Dome; import java.awt.event.*; import java.awt.*; import javax.swing.*; public class WindowM ...
- SQL Server数据库(作业讲解和复习)
--第一题 查询Student表中的所有记录的Sname.Ssex和Class列.select Sname,Ssex,Class from student --第二题 查询教师所有的单位即不重复的De ...
- JavaScript prototype 属性
prototype 属性使开发人员有能力向对象添加属性和方法. 语法 object.prototype.name=value 实例 在本例中,我们将展示如何使用 prototype 属性来向对象添加属 ...
- 六种流行的语言---C、C++、python、Java、php、C#比较[转]
语言大餐 回归正题,本文是六种语言连接mysql数据库的代码展示,在LZ尝试的过程中,无论是语言环境搭建.mysql依赖库的导入还是代码的风格,各种语言都各有千秋.接下来,我们就让这些语言一一登场吧. ...
- sql2008 附加数据库出错解决方法
当遇到 无法为此请求检索数据,(Microsoft.SqlServer.SmoEnum)其他信息执行Transact-Sql语句或批处理时发生了异常, Microsoft.SqlServer.Conn ...
- java之通过反射,来获得某对象的所有方法(类方法提取器)
参考Thinging in Java 在编程时, 如果不记得一个类是否有某个方法,或者不知道一个类究竟能做些什么,而又不想通过索引或 类的层次结构去查找jdk文档,这时通过反射的小工具能节省很多时间. ...
- 西天取经第一步——制作自己的HTML5游戏
废话不说,直入主题:这是一个休闲益智类游戏,与愤怒的小鸟类似采用Box2dWeb引擎.再开发游戏之前,首先我要把Box2dWeb给总结一下方便以后调用 大家可以在http://code.google. ...
- JavaScript中字符串转Json方法小记
例如: JSON字符串:var str1 = '{ "name": "cxh", "sex": "man" }'; JS ...
- 【NOIP模拟题】【二分】【倍增】【链表】【树规】
3 计算几何3.1 题意描述花花对计算几何有着浓厚的兴趣.他经常对着平面直角坐标系发呆,思考一些有趣的问题.今天,他想到了一个十分有意思的题目:首先,花花会在x 轴正半轴和y 轴正半轴分别挑选n 个点 ...
- Linux-编译器gcc/g++编译步骤
gcc和g++现在是gnu中最主要和最流行的c&c++编译器.g++是c++的命令,以.cpp为主:对于c语言后缀名一般为.c,这时候命令换做gcc即可.编译器是根据gcc还是g++来确定是按 ...