假设下一个状态有必败。那么此时状态一定是必胜,否则此时状态一定是必败

状压DP

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int dp[1<<20];
int n;
int dfs(int state)
{
int i,t;
if(state==0)//0必败
return dp[0]=0;
if(dp[state]!=-1)//此状态已知
return dp[state];
dp[state]=0;
for(i=0;i<n;i++)
{
if((state>>i)&1)//摘掉第i个花瓣
{
t=state^(1<<i);
if(dfs(t)==0)
{
dp[state]=1;//此状态必胜
break;
}
if(i<n-1&&((t>>(i+1))&1))//再摘掉第i+1个花瓣
{
t=t^(1<<(i+1));
if(dfs(t)==0)
{
dp[state]=1;//此状态必胜
break;
}
}
}
}
return dp[state];
}
int main()
{
bool vis[30];
int a[30];
int i,T,j,k,m,t,state,cnt;
memset(dp,-1,sizeof(dp));
cin>>T;
for(j=1;j<=T;j++)
{
cin>>n>>m;
memset(vis,0,sizeof(vis));
while(m--)
{
cin>>t;
vis[t]=1;
}
cnt=0;
for(i=1;i<=n;i++)
{
if(!vis[i])
a[cnt++]=1;
else
break;
}
if(i!=n)
{
for(k=n;k>=i;k--)
if(!vis[k])
a[cnt++]=1;
else
a[cnt++]=0;
}
state=0;
for(i=0;i<cnt;i++)
state+=a[i]*(1<<(cnt-i-1));//生成初始状态
n=cnt;
if(dfs(state))
printf("Case %d: yes\n",j);
else
printf("Case %d: no\n",j);
}
return 0;
}

12846 A Daisy Puzzle Game

Little Gretchen playing the Daisy game

Gretchen, a little peasant girl from the Swiss Alps, is an expert

at the Daisy game, a simple game that is very well-known

around the country. Two players pluck of the petals of a Daisy

fower, and each player is always at liberty to pluck a single

petal or any two contiguous ones, so that the game would

continue by singles or doubles until the victorious one takes

the last leaf and leaves the “stump”—called the “old maid”—

to the opponent.

The pretty mädchen has mastered the Daisy game to such

an extent that she always plays optimally. In other words, she

always plays by performing the best possible moves on each

turn, a feat which never fails to astonish tourists who dare to

challenge her to a game.

Analyzing the game, it is not very complicated to fgure out a winning strategy for the second player,

as long as the game starts with a complete fower (having all of its petals intact). However, what will

happen when Gretchen plays against an opponent that also plays optimally, and some of the fower’s

petals have been plucked of at random?

A fower is described by a number N which represents the original number of petals of the fower,

and a list of the petals that have been plucked of. All petals are numbered from 1 to N, and given the

circular nature of the fower, that means petals 1 and N are originally adjacent.

Given the description of a fower, and assuming it’s Gretchen’s turn, will she win the game? Remember

that both players always play optimally.

Input

Input starts with a positive integer T, that denotes the number of test cases.

Each test case begins with two integers in a single line, N and M, representing the number of petals

originally in the fower, and the number of petals that have been plucked of, respectively.

The next line contains M distinct integers, representing the petals that have been plucked of. These

numbers will always be in ascending order.

T<= &#20; 5000; 3<= &#20; N<= &#20; 20; 1 &#20;<= M < N

Output

For each test case, print the case number, followed by the string ‘yes’ if Gretchen wins the game, or

‘no’ otherwise.

Sample Input

2

13 1

7

5 3

1 3 4

Sample Output

Case 1: yes

Case 2: no

uva live 12846 A Daisy Puzzle Game的更多相关文章

  1. UVA 12849 Mother’s Jam Puzzle( 高斯消元 )

    题目: http://uva.onlinejudge.org/external/128/12849.pdf #include <bits/stdc++.h> using namespace ...

  2. uva 227 Puzzle

     Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...

  3. UVA 227 Puzzle - 输入输出

    题目: acm.hust.edu.cn/vjudge/roblem/viewProblem.action?id=19191 这道题本身难度不大,但输入输出时需要特别小心,一不留神就会出问题. 对于输入 ...

  4. UVA_Digit Puzzle UVA 12107

    If you hide some digits in an integer equation, you create a digit puzzle. The figure below shows tw ...

  5. UVA 277 Puzzle

    题意:输入5x5的字符串,输入操作,要求输出完成操作后的字符串. 注意:①输入的操作执行可能会越界,如果越界则按题目要求输出不能完成的语句. ②除了最后一次的输出外,其他输出均要在后面空一行. ③操作 ...

  6. UVA 227 Puzzle(基础字符串处理)

    题目链接: https://cn.vjudge.net/problem/UVA-227 /* 问题 输入一个5*5的方格,其中有一些字母填充,还有一个空白位置,输入一连串 的指令,如果指令合法,能够得 ...

  7. uva 227 Puzzle (UVA - 227)

    感慨 这个题实在是一个大水题(虽然说是世界决赛真题),但是它给出的输入输出数据,标示着老子世界决赛真题虽然题目很水但是数据就能卡死你...一直pe pe直到今天上午AC...无比感慨...就是因为最后 ...

  8. Puzzle UVA - 227 PE代码求大佬指点

    ​ A children's puzzle that was popular 30 years ago consisted of a 5×5 frame which contained 24 smal ...

  9. UVA - 12107 Digit Puzzle(数字谜)(IDA*)

    题意:给出一个数字谜,要求修改尽量少的数,使修改后的数字谜只有唯一解.空格和数字可以随意替换,但不能增删,数字谜中所有涉及的数必须是没有前导零的正数.输入数字谜一定形如a*b=c,其中a.b.c分别最 ...

随机推荐

  1. centos下安装nodejs的三种种方式

    方法一:源码包安装 官网下载 centos下载最新版10.9 https://nodejs.org/dist/v10.9.0/node-v10.9.0-linux-x64.tar.xz mkdir / ...

  2. java登录拦截Filter

    此例子为一个简单的登录拦截. 首先在web.xml中配置拦截类. <filter-mapping> <filter-name>SessionFilter</filter- ...

  3. Xcode导入第三方库图文

    Three20这个与facebook亲戚的开源库是蜚声iPhone开发界,很多App都有它的影子,主要是其真得是功能强大.那么如何将Three20库添加到自己的项目中应用呢?一种是Python命令方式 ...

  4. POJ-3190-分配畜栏

    这个题首先,我们需要注意的是它的时间是一秒,其中还包括了你读入数据的时间,因为cin我写的时候没有解除绑定,所以直接超时,我们直接用scanf函数读入50000组数据好了. 然后就是poj交的时候,如 ...

  5. 树莓派 Centos7 安装EPEL 7

    cat > /etc/yum.repos.d/epel.repo << EOF [epel] name=Epel rebuild for armhfp baseurl=https:/ ...

  6. SQL 一次插入多条记录

    本文介绍如何快速插入多条数据到数据表中,以满足sql语句学习或项目测试的需要. 本文非原创,是对移步原文的重新整理. 如有以下表格,如图: 1,原始添加记录的方式,sql语句如下: insert in ...

  7. Android开发——使用ADB Shell命令实现模拟点击(支付宝自动转账实现)

    首先声明,本人反对一切利用技术的违法行为 本文的实现代码已经销毁,本文以介绍流程为主 1.这里所说的模拟点击不是在自己的APP里点击,点自己APP上的控件没什么好说的 不仅是支付宝转账,其他的获取别人 ...

  8. iframe in ipad safari

    http://developer.apple.com/library/safari/#documentation/appleapplications/reference/safariwebconten ...

  9. 局域网虚拟机端口映射访问apache

    如果我们在虚拟机内搭建好服务器后,希望可以在局域网内的设备上都能访问到这个虚拟服务器,就可以参照以下步骤来操作.其中包括了很多遇到的坑.先说说我的环境是 宿主机:windows 8.1 虚拟机:vmw ...

  10. python025 Python3 正则表达式

    Python3 正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. ...