记录编号 |
311876 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[USACO Mar]石子游戏 |
最终得分 |
100 |
用户昵称 |
404 |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.031 s |
提交时间 |
2016-09-25 14:45:50 |
内存使用 |
1.31 MiB |
显示代码纯文本
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- using namespace std;
- int ans[2<<16];
- int n;
- int vis[2<<16],sum=1;
- bool flag;
- int calc(int x)
- {
- int cnt=0;
- while(x)
- {
- if(x&1)
- cnt++;
- x=x>>1;
- }
- return cnt;
- }
- void dfs(int now,int step)
- {
- if(now==0&&step>1&&step<sum+1)
- return;
- if(now==0&&step==sum+1){
- flag=1;
- for(int i=1;i<=step;i++){
- for(int j=1;j<=n;j++)
- if(ans[i]&(1<<(j-1)))printf("X");
- else printf("O");
- printf("\n");
- }
- return;
- }
- if(step+calc(now)>sum+1)
- return;
- for(int i=1;i<=n;i++)
- {
- int nx=now^(1<<(i-1));
- if(nx!=0&&vis[nx])continue;
- if(nx==0&&vis[nx]==1){
- vis[nx]=2,ans[step+1]=nx;
- dfs(nx,step+1);
- ans[step+1]=0,vis[nx]=1;
- if(flag)return;
- }
- else if(nx!=0&&vis[nx]==0){
- vis[nx]=1,ans[step+1]=nx;
- dfs(nx,step+1);
- ans[step+1]=0,vis[nx]=0;
- if(flag)return;
- }
- else
- continue;
- }
- }
- int main()
- {
- freopen("rocksa.in","r",stdin);
- freopen("rocksa.out","w",stdout);
- scanf("%d",&n);
- for(int i=1;i<=n;i++)sum=sum*2;
- ans[1]=0,vis[0]=1;
- dfs(0,1);
- return 0;
- }