47 博弈的相互转化的状态(必须是完全等价的)

C - Crosses and Crosses

The game of Crosses and Crosses is played on the field of 1 × n cells. Two players make moves in turn. Each move the player selects any free cell on the field and puts a cross ‘×’ to it. If after the player’s move there are three crosses in a row, he wins.

You are given n. Find out who wins if both players play optimally.

Input

Input file contains one integer number n (3 ≤ n ≤ 2000).

Output

Output ‘1’ if the first player wins, or ‘2’ if the second player does;

题意是很简单就是两个人在一排椅子上x如果谁能够先先画出三个相连的X的话那么这一个人就赢了,就是谁先取完三个相连的就赢了,这道题目的我们可以找到相互转换的状态除了规模外其他的条件应该都是一样的(可以进行的操作),这里我们跟前面的一道题目选择相同的等价状态,可以进行任意操作的椅子长度换句话说对于下面的这一种情况,我们选择的状态是这样的

 

            X   。  。  .    .     .     。    。      X

这里中间还有七个可以放X的地方,我们这样的话是选择中间的三个点作为我们的任意操作状态,随便放,这里我们长度是3

对于我们如果是在第一个点的地方放X左面的可以随便操作的长度是1-3=-2所以就是没有操作长度,后继状态是sg值是0,这里我们可以发现只要可以随便操作的长度如果是小于等于0的话那么这个长度对应的输赢状态是必输;放第一个点的右面是3-1-2=0,所以这里他的右面的状态也是必输状态,当选择第一个点的后继状态我们这里就有了,对于选择第二个点或是第三个点的话sg的求算类似

2 : 左面是2-3=-1 <0    leftsg=0,   有面是 3-2-2=-1 <0 rightsg=0;

3 :左面是3-3=0  = 0    leftsg=0 ,    有面是 3-3-2=-2 <0  rightsg=0;

还有就是在求算sg的时候,如果我们用暴力的方式不能解决(有时候分析这是对的)但是交上去是wa,这个时候就要使用递归的方式求算一下,(如果是数据量小的话建议使用递归这个是最稳的方式,就是如果数据量太大的话会mle);

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int Max = 2e3+10;
int sg[Max];
int getsg(int n){
   if(n<0) return 0;
   if(sg[n]>-1) return sg[n];
   bool visited[Max];
   memset(visited,0,sizeof(visited));
   for(int i=1;i<=n;i++){
    visited[getsg(i-3)^getsg(n-i-2)]=1;
   }
   for(int i=0;;i++){
    if(!visited[i]){
        return sg[n]=i;
    }
   }
}
int main(){
   memset(sg,-1,sizeof(sg));
   int n;
   while(scanf("%d",&n)!=EOF){
    if(getsg(n)) printf("1\n");
    else printf("2\n");
   }
   return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值