BZOJ 3895: 取石子(记忆化搜索)

版权声明:本文为博主原创文章,未经博主允许必须转载。 https://blog.csdn.net/qq_35950004/article/details/88636594

题目
这个题。
先用常识把大小大于1和等于1的区别对待,发现大于1的可以看作就是1堆。
然后记忆化搜索。。。。。
终于有一个卡数据范围的博弈了。
AC Code:

#include<bits/stdc++.h>
using namespace std;

int f[55][50005];

int dfs(int c1,int sc){
	if(f[c1][sc]!=-1) return f[c1][sc];
	if(c1==0) return sc&1;
	if(sc == 1) return dfs(c1+1,0);
	if(c1 && !dfs(c1-1,sc)) return f[c1][sc]=1;
	if(c1>=2 && !dfs(c1-2,(sc+(sc?3:2)))) return f[c1][sc]=1;
	if(c1 && sc &&!dfs(c1-1,sc+1)) return f[c1][sc]=1;
	if(sc && !dfs(c1,sc-1)) return f[c1][sc]=1;
	return f[c1][sc] = 0;
}
char ANS[2][5]={"NO","YES"};

int main(){
	int T;
		memset(f,-1,sizeof f);
	for(scanf("%d",&T);T--;){
		int n,c1=0,sc=0;scanf("%d",&n);
		for(int i=1,x;i<=n;i++){
			scanf("%d",&x);
			sc+=(x>=2) * (x + (sc!=0)),c1+=(x==1);
		}
		printf("%s\n",ANS[dfs(c1,sc)]);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_35950004/article/details/88636594