Before the cows head home for rest and recreation, Farmer John wantsthem to get some intellectual stimulation by playing a game.
The game board comprises N (1 <= N <= 15) identical holes in theground, all of which are initially empty. A cow moves by eithercovering exactly one hole with a rock, or uncovering exactly onepreviously covered hole. The game state is defined by which holesare covered with rocks and which aren't. The goal of the game isfor the cows to reach every possible game state exactly once andthen return to the state with all holes uncovered.The cows have been having a tough time winning the game. Below isan example of one of their games:
Holes
time 1 2 3
-----------------
0 O O O Initially all of the holes are empty
1 O O X The cow covers hole 3
2 X O X The cow covers hole 1
3 X O O The cow uncovers hole 3
4 X X O The cow covers hole 2
5 O X O The cow uncovers hole 1
6 O X X The cow covers hole 3
7 X X X The cow covers hole 1
Now the cows are stuck! They must uncover one hole and no matterwhich one they uncover they will reach a state they have alreadyreached. For example if they remove the rock from the second hole they will reach the state (X O X) which they already visited at
time 2.
Below is an example of a valid solution for N=3 holes:
Holes
time 1 2 3
-----------------
0 O O O Initial state: all of the holes are empty
1 O X O The cow covers hole 2
2 O X X The cow covers hole 3
3 O O X The cow uncovers hole 2
4 X O X The cow covers hole 1
5 X X X The cow covers hole 2
6 X X O The cow uncovers hole 3
7 X O O The cow uncovers hole 2
8 O O O The cow uncovers the 1st hole
which returns the game board to the start having, visited each state once.
The cows are tired of the game and want your help. Given N, create a valid sequence of states that solves the game. If there are multiple solutions return any one.

PROBLEM NAME: rocks

INPUT FORMAT:
* Line 1: A single integer: N

SAMPLE INPUT (file rocks.in):
3

OUTPUT FORMAT:
* Lines 1..2^N+1: A string of length N containing only 'O' and 'X' (where O denotes a uncovered hole and X denotes a covered hole). The jth character in this line represents whether the jth hole is covered or uncovered in this state. The first and last lines must be all uncovered (all O).

SAMPLE OUTPUT (file rocks.out):
OOO
OXO
OXX
OOX
XOX
XXX
XXO
XOO
OOO

题目大意就是写出长度为n的X和O的所有排列,其中相邻的两个排列之间只能有一个数不同。

因为数据不是很大最多有2 ^ 15种排列,所以dfs就行。

只要每一次改变其中一个数,然后判断这种排列前面是否已经存在过,不存在就输出。

但每一次判断的时候是一个字符串,无论是空间上还是时间上都不是很好。于是有一个优化:吧‘O’看成0,'X'看成1,于是就变成了一个01串,即一个数的二进制。

于是我们只要尝试修改这个数的每一位,然后判断得到的新的数是否存在过就行。

至于修改,当然是亦或号啦

 #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstring>
#include<cmath>
#include<cctype> //isdigit
using namespace std;
typedef long long ll;
#define enter printf("\n")
const int maxn = 1e6 + ;
const int INF = 0x3f3f3f3f;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch))
{
ans = ans * + ch - ''; ch = getchar();
}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) {putchar('-'); x = -x;}
if(x == ) {putchar(''); return;}
int q[], N = ;
q[] = ;
while(x) {q[++N] = x % ; x /= ;}
while(N) {putchar('' + q[N]); --N;}
} int n;
bool vis[maxn];
void print(int x) //从高位开始输出每一位
{
vis[x] = ;
for(int i = n - ; i >= ; --i)
{
if((x >> i) & ) printf("X");
else printf("O");
}
enter;
}
void dfs(int step, int x)
{
if(!vis[x]) print(x); //之所以放在这,而不是第53行之后,是为了输出最开始的OOOOOOO情况
if(step == ( << n) + ) exit();
for(int i = ; i <= n - ; ++i)
{
int now = x ^ ( << i);
if(!vis[now]) dfs(step + , now);
}
} int main()
{
n = read();
dfs(, );
for(int i = ; i <= n; ++i) printf("O"); enter;
return ;
}

The Rock Game的更多相关文章

  1. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  2. POJ - 2339 Rock, Scissors, Paper

    初看题目时就发了个错误,我因为没有耐心看题而不了解题目本身的意思,找不到做题的突破口,即使看了一些题解,还是没有想到方法. 后来在去问安叔,安叔一语道破天机,问我有没有搞清题目的意思,我才恍然大悟,做 ...

  3. ROCK 聚类算法‏

    ROCK (RObust Clustering using linKs)  聚类算法‏是一种鲁棒的用于分类属性的聚类算法.该算法属于凝聚型的层次聚类算法.之所以鲁棒是因为在确认两对象(样本点/簇)之间 ...

  4. Rice Rock

    先翻译评分要点,然后一点点翻译程序实现过程 如何产生一堆岩石? rock_group = set([])#空集合,全局变量   rock_group.add(a_rock) 要画出来draw hand ...

  5. HDOJ(HDU) 2164 Rock, Paper, or Scissors?

    Problem Description Rock, Paper, Scissors is a two player game, where each player simultaneously cho ...

  6. Hard Rock

    Ilya is a frontman of the most famous rock band on Earth. Band decided to make the most awesome musi ...

  7. 弹指之间 -- Folk Rock

    CHAPTER 17 民谣摇滚 Folk Rock 以8Beat为主,120左右的速度最能表现此节奏特色. 示例曲目: 略

  8. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H题 Rock Paper Scissors Lizard Spock.(FFT字符串匹配)

    2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...

  9. HDU 2164 Rock, Paper, or Scissors?

    http://acm.hdu.edu.cn/showproblem.php?pid=2164 Problem Description Rock, Paper, Scissors is a two pl ...

随机推荐

  1. T-SQL建索引

    USE database GO   ------------开始----------- ALTER TABLE [name] DROP CONSTRAINT 主键约束    ----删除主键约束 IF ...

  2. JS DOM操作(四) Window.docunment对象——操作内容

    操作内容:即对标签所夹内容的操作 一 非表单元素内容操作 定位 var a = document.ElementById( "id" ) 1.获取内容 var s = a.inne ...

  3. 盒子模型的margin负数用法

    盒子的margin用法大家都应该很清楚,在实际中一般使用margin来水平居中或者让自己移动相应的位置,但是margin给负数在实际中也是有用的. 如图为两个浮动的盒子. 给左边的盒子margin-l ...

  4. Android-Handler使用姿势

    http://www.jianshu.com/p/8e9a54f1826e 好文章先马,慢慢看

  5. 【14】代理模式(Proxy Pattern)

    一.引言 在软件开发过程中,有些对象有时候会由于网络或其他的障碍,以至于不能够或者不能直接访问到这些对象,如果直接访问对象给系统带来不必要的复杂性.这时候可以在客户端和目标对象之间增加一层中间层,让代 ...

  6. centos7下快速安装Nginx服务器

    1.添加源 默认情况Centos7中无Nginx的源,最近发现Nginx官网提供了Centos的源地址.因此可以如下执行命令添加源: sudo rpm -Uvh http://nginx.org/pa ...

  7. python实现贪婪算法解决01背包问题

    一.背包问题 01背包是在M件物品取出若干件放在空间为W的背包里,每件物品的体积为W1,W2至Wn,与之相对应的价值为P1,P2至Pn.01背包是背包问题中最简单的问题.01背包的约束条件是给定几种物 ...

  8. 一张图看懂Mysql的join连接

    INNER JOIN:当两个表中都匹配时返回行. LEFT JOIN:返回左表中的所有行,即使右表中没有匹配项也是如此. RIGHT JOIN:返回右表中的所有行,即使左表中没有匹配项也是如此. FU ...

  9. NUnit单元测试示例

    单元测试的用法 1.下载NUnit软件 安装后打开界面如图: 2.新建测试项目 添加类库项目并在NuGet管理包中添加NUnit 这里添加NuGet的NUnit包要注意保持版本和之前下载的NUnit软 ...

  10. 使用ArcGIS Pro编辑在线三维服务图层

    ArcGIS Pro 从2.2版本起,提供了编辑在线三维服务图层的功能.通过该功能,我们可以直接在Pro中编辑发布的三维服务图层Web Scene Layer. 我们知道三维场景服务支持包含多种类型的 ...