- 注册时间
- 2007-12-27
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 40155
- 在线时间
- 小时
|
发表于 2017-3-5 08:40:24
|
显示全部楼层
从网页中搜索了源代码,不知道是否能够使用
- #include <stdio.h>
- #include <stdlib.h>
- #include <vector>
- #include <algorithm>
- using namespace std;
- #define MAX 1000
- int s[MAX];
- //create state array if the user could remove all chesses
- //Given a list of number n1,n2,...,nh, the user wins iff
- // s[n1]^s[n2]^...^s[nh]==0
- //if users could remove the last chess
- void init()
- {
- int st[MAX],i,j;
- s[0]=0;s[1]=1,s[2]=2;
- for(i=3;i<MAX;i++){
- for(j=0;j<MAX;j++)st[j]=0;
- st[s[i-1]]=1,st[s[i-2]]=1;
- for(j=1;j<=(i-1)/2;j++){
- int k=i-1-j;
- st[s[j]^s[k]]=1;
- k=i-2-j;
- st[s[j]^s[k]]=1;
- }
- for(j=0;j<MAX;j++)if(st[j]==0)break;
- s[i]=j;
- }
- }
- int odd(int x){return (x&1)!=0;}
- int even(int x){return (x&1)==0;}
- //Function to test whether it is a win state if the last chess should not be removed
- //return 1 for win state and 0 for lost state
- //It is almost same as the case when last chess could be removed except for
- // some special cases related to number 1,4,5,9,12,17,20,25
- int test_state(const vector<int>& a)
- {
- int s1,s4,s5,s9,s12,s17,s20,s25;
- int i,r,so=0;
- s1=s4=s5=s9=s12=s17=s20=s25=0;
- for(r=0,i=0;i<a.size();i++){
- switch(a[i]){
- case 1:s1++;break;
- case 4:s4++;break;
- case 5:s5++;break;
- case 9:s9++;break;
- case 12:s12++;break;
- case 17:s17++;break;
- case 20:s20++;break;
- case 25:s25++;break;
- default:so++;break;
- }
- r^=s[a[i]];
- }
- if(odd(s9)&&even(s4+s1)&&so+s5+s12+s17+s20+s25==0||
- s12==1&&even(s1+s4)&&so+s5+s9+s17+s20+s25==0||
- odd(s5)&&odd(s1+s4)&&so+s9+s12+s17+s20+s25==0||
- even(s5)&&so+s9+s12+s17+s20+s25==0||
- even(s9+s12+s17)&&so+s5+s25==0||
- even(s9+s12+s17)&&odd(s1+s4+s20)&&s25==1&&so+s5==0||
- odd(s1+s4)&&odd(s9)&&s25==1&&so+s5+s12+s17+s20==0){
- r=!r;
- }
- return r!=0;
- }
- //read input number lists
- //user should input
- //programname n1 n2 n2 ... nh
- void readArg(int argc, char *argv[], vector<int>& input)
- {
- int i;
- input.clear();
- for(i=1;i<argc;i++){
- int u=atoi(argv[i]);
- if(u<=0||u>=MAX){
- fprintf(stderr,"input %d not in range [1,%d]\n",u,MAX-1);
- exit(-1);
- }
- input.push_back(u);
- }
- sort(input.begin(),input.end());
- }
- //test the case that the offset'th chess of line index is removed
- // If it result in a lost state, output it
- void tryremoveone(const vector<int> input, int index, int offset)
- {
- vector<int> outv;
- int i;
- for(i=0;i<index;i++)outv.push_back(input[i]);
- if(offset==0){
- if(input[index]>1)outv.push_back(input[index]-1);
- }else{
- outv.push_back(offset);
- if(offset<input[index]-1)outv.push_back(input[index]-1-offset);
- }
- for(i=index+1;i<input.size();i++)outv.push_back(input[i]);
- if(test_state(outv)==0){
- int left1=offset,left2=input[index]-1-offset;
- if(left1==0&&left2==0)
- printf("Possible Move: Remove 1\n");
- else if(left1==0){
- printf("Possible Move: %d => %d\n",input[index],left2);
- }else if(left2==0){
- printf("Possible Move: %d => %d\n", input[index],left1);
- }else{
- printf("Possible Move: %d => %d,%d\n", input[index],left1,left2);
- }
- }
- }
- //test the case that the offset'th and (offset+1)'th chesses of line index are removed
- // If it result in a lost state, output it
- void tryremovetwo(const vector<int> input, int index, int offset)
- {
- vector<int> outv;
- int i;
- if(input[index]<2)return;
- for(i=0;i<index;i++)outv.push_back(input[i]);
- if(offset==0){
- if(input[index]>2)outv.push_back(input[index]-2);
- }else{
- outv.push_back(offset);
- if(offset<input[index]-2)outv.push_back(input[index]-2-offset);
- }
- for(i=index+1;i<input.size();i++)outv.push_back(input[i]);
- if(test_state(outv)==0){
- int left1=offset,left2=input[index]-2-offset;
- if(left1==0&&left2==0)
- printf("Possible Move: Remove 2\n");
- else if(left1==0){
- printf("Possible Move: %d => %d\n",input[index],left2);
- }else if(left2==0){
- printf("Possible Move: %d => %d\n", input[index],left1);
- }else{
- printf("Possible Move: %d => %d,%d\n", input[index],left1,left2);
- }
- }
- }
- int main(int argc, char *argv[])
- {
- vector<int> input,newi;
- init();
- readArg(argc, argv,input);
- if(test_state(input)==0){
- printf("Lost\n");
- }else{
- int i,j;
- for(i=0;i<input.size();i++){
- if(i==0||input[i]!=input[i-1]){
- for(j=0;j<=(input[i]-1)/2;j++)tryremoveone(input,i,j);
- for(j=0;j<input[i]/2;j++)tryremovetwo(input, i, j);
- }
- }
- }
- }
复制代码 |
|