记录编号 83183 评测结果 AAAAAAAAAA
题目名称 [USACO Mar]石子游戏 最终得分 100
用户昵称 Gravatarcstdio 是否通过 通过
代码语言 C++ 运行时间 0.013 s
提交时间 2013-11-30 20:26:30 内存使用 0.34 MiB
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<deque>
using namespace std;
bool flag[65536]={0};
int pow2[20]={1};
int n;
int endstep;
vector<int> ans;
int getdigit(int x,int k){//x的左起第k位(从1标号)
	return (x>>(n-k))&1;
}
int changedigit(int x,int k,int t){//x的左起第k位改成t
	//2^(n-k)的位
	return x^(pow2[n-k]);
}
void printdig(int x){
	int i,temp;
	for(i=1;i<=n;i++){
		temp=getdigit(x,i);
		if(temp==0) printf("O");
		else printf("X");
	}
	printf("\n");
}
void answer(void){
	int i;
	for(i=0;i<ans.size();i++) printdig(ans[i]);
}
bool DFS(int x,int step){
	if(step==endstep){
		answer();
		return true;
	}
	int i,temp;
	for(i=1;i<=n;i++){
		if(getdigit(x,i)==1) temp=changedigit(x,i,0);
		else temp=changedigit(x,i,1);
		if(temp&&flag[temp]) continue;
		if(!temp&&step<endstep-1&&flag[temp]) continue;
		flag[temp]=true;
		ans.push_back(temp);
		if(DFS(temp,step+1)) return true;
		ans.pop_back();
		flag[temp]=false;
	}
	return false;
}
void init(void){
	scanf("%d",&n);
	int i;
	for(i=1;i<20;i++) pow2[i]=pow2[i-1]*2;
	endstep=pow2[n];
}
int main(){
	freopen("rocksa.in","r",stdin);
	freopen("rocksa.out","w",stdout);
	init();
	ans.push_back(0);
	flag[0]=true;
	DFS(0,0);
	return 0;
}