uva live 12846 A Daisy Puzzle Game
2014-11-14 21:47
357 查看
如果下一个状态有必败,那么此时状态一定是必胜,否则此时状态一定是必败
状压DP
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<= 5000; 3<= N<= 20; 1 <= 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
状压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<= 5000; 3<= N<= 20; 1 <= 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
- UVALive 4260 Fortune Card Game (Regionals 2008 Asia Taipei +DP)
- UVALive 6322 The Swapping Game
- UVALive 5695 -The Last Puzzle -区间dp
- UVALive 7278 Game of Cards (sg函数)
- UVALive 7278 Game of Cards
- UVALive - 7715 The 2016 ACM-ICPC Asia Jakarta Regional Contest E - Guessing Game 繁琐的等差数列求和
- Regionals 2009 >> Asia - Hsinchu UVALIVE, 4529 A Constrained Queen Game - 搜索剪枝
- UVALive 6919 A game for kids(树形dp)
- The Same Game UVA 758/Uvalive 5423 大模拟 求最大联通块并清除
- UVALive - 4979 Game 暴力|模拟|签到
- UVALive - 6322 The Swapping Game
- UVALive-7278 - Game of Cards
- UVALive - 4260 Fortune Card Game (DP&状态转移)好题
- UVAlive 5027 card game KM
- UVALive 3668 A Funny Stone Game
- [nim博弈扩展 sg函数] UVALive 3668 A Funny Stone Game
- City Game UVALive - 3029(扫描法)
- UVALive-7278 - Game of Cards【博弈】【sg定理】
- UVALIVE 6301 The Shortcut Eight-Puzzle