poj1143Number Game
来源:互联网 发布:张忻铃 知乎 编辑:程序博客网 时间:2024/05/17 22:29
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:
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.
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.
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 52 2 35 2 3 4 5 60
Sample Output
Test Case #1The winning moves are: 2Test Case #2There's no winning move.Test Case #3The winning moves are: 4 5 6
Source
题意:在2-20内剩余了一些数,选了i,则i的倍数都不能再选了,选了i,j,则i*n+j*m(n,m为任意整数)也不能选,当一个人没法选了他就输了,你先手可以选择那些数据使得你胜利。
#include <iostream>#include <stdio.h>#include <cstring>#include <algorithm>#include <queue>#include <map>using namespace std;const int MAXN = (1<<20);int n;int dp[MAXN]; //先手赢1,不确定-1,后手赢0int gets(bool use[]){ int rt = 0; for(int i=1; i<=20; i++) { rt<<=1; if(use[i]) { rt+=1; } } return rt;}void coypandchange(bool next[],bool use[],int i){ for(int j=0; j<25; j++) next[j]=use[j]; for(int j=i;j<=20; j+=i) { next[j] = false; } for(int j=2; j+i<=20; j++) { if(!next[j]) { next[j+i] = false; } }}int dfs(bool use[]){ int s = gets(use); if(dp[s]!=-1)return dp[s]; for(int i=2; i<=20; i++) { if(!use[i])continue; bool next[25]; coypandchange(next,use,i); if(dfs(next)==0) { //cout<<s<<" "<<1<<endl; return dp[s] = 1; } } // cout<<s<<" "<<0<<endl; return dp[s] = 0;}int main(){ int a, cs = 1, i; memset(dp,-1,sizeof dp); dp[0] = 0; while(scanf("%d",&n)!=EOF) { if(n==0)break; bool use[25]; //true为可用 memset(use,0,sizeof use); while(n--) { scanf("%d",&a); use[a] = true; } // outbool(use); if(cs>1) printf("\n"); printf("Test Case #%d\n",cs++); bool shuchu = false; for(i=2; i<=20; i++) { if(!use[i])continue; //cout<<i<<endl; bool next[25]; coypandchange(next,use,i); // outbool(use); if(dfs(next)==0) { if(!shuchu) { printf("The winning moves are:"); } shuchu = true; printf(" %d",i); } } if(!shuchu) printf("There's no winning move."); printf("\n"); } return 0;}
0 0
- poj1143Number Game
- poj1143Number Game
- poj1143Number Game(记忆化搜索+二进制保存状态+DP)
- game
- game
- game
- game...
- Game
- Game
- Game
- Game
- game
- Game
- Game
- game
- game
- Game
- GAME
- 20150714
- time_t tm timeval 和 时间字符串的转换
- cug 1126 快速模幂
- 可拖拽排序的GridView
- 使用android-support-v7-appcompat包ActionBar不能显示图标,低版本手机运行显示不正常
- poj1143Number Game
- emulator: ERROR: x86 emulation currently requires hardware acceleration!Please ensure Intel HAXM is
- spring annotation注解 autowire
- IOS开发-10.KVC
- 在Linux系统下,重启Tomcat使用命令操作的!
- 关于Linux 设置ip绑定问题 以及mysql 设置固定Ip访问问题
- C++中placement new操作符(经典)
- Excel GET.CELL
- 实习总结(1)