您的位置:首页 > 其它

POJ 3537 Crosses and Crosses SG函数

2016-07-18 09:03 337 查看
在1*n的格子里轮流划×,先划3个连续的×获胜,当在i这个位置划×之后,分成2部分:开始到i-3和i+2到结束。
package fd;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

static int[] sg = new int[2222];
static int SG(int n) {
if(n <= 0) {
return 0;
}
if(n <= 3) {
return 1;
}
if(sg
!= -1) {
return sg
;
}
Set<Integer> s = new HashSet<Integer>();
for(int i = 1; i<= n; i++) {
s.add(SG(i-3)^SG(n-i-2));
}
int res = 0;
while(s.contains(res)) {
res++;
}
return sg
= res;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
Arrays.fill(sg, -1);
while(sc.hasNext()) {
int n = sc.nextInt();
System.out.println(SG(n) == 0 ? 2 : 1);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: