The Rock Game
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的更多相关文章
- ural 2069. Hard Rock
2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...
- POJ - 2339 Rock, Scissors, Paper
初看题目时就发了个错误,我因为没有耐心看题而不了解题目本身的意思,找不到做题的突破口,即使看了一些题解,还是没有想到方法. 后来在去问安叔,安叔一语道破天机,问我有没有搞清题目的意思,我才恍然大悟,做 ...
- ROCK 聚类算法
ROCK (RObust Clustering using linKs) 聚类算法是一种鲁棒的用于分类属性的聚类算法.该算法属于凝聚型的层次聚类算法.之所以鲁棒是因为在确认两对象(样本点/簇)之间 ...
- Rice Rock
先翻译评分要点,然后一点点翻译程序实现过程 如何产生一堆岩石? rock_group = set([])#空集合,全局变量 rock_group.add(a_rock) 要画出来draw hand ...
- HDOJ(HDU) 2164 Rock, Paper, or Scissors?
Problem Description Rock, Paper, Scissors is a two player game, where each player simultaneously cho ...
- Hard Rock
Ilya is a frontman of the most famous rock band on Earth. Band decided to make the most awesome musi ...
- 弹指之间 -- Folk Rock
CHAPTER 17 民谣摇滚 Folk Rock 以8Beat为主,120左右的速度最能表现此节奏特色. 示例曲目: 略
- 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H题 Rock Paper Scissors Lizard Spock.(FFT字符串匹配)
2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...
- HDU 2164 Rock, Paper, or Scissors?
http://acm.hdu.edu.cn/showproblem.php?pid=2164 Problem Description Rock, Paper, Scissors is a two pl ...
随机推荐
- CRC16位校验
之前有跟第三方通讯合作,应为CRC表码问题导致校验出结果不一致,纠结了很久,最后直接采用CRC计算方式校验才解决. 两种方式贴,自行对比. CRC校验计算方法 private ushort CRC_1 ...
- Maven的配置与下载
一丶下载 前提条件 :已经安装配置好了Jdk 进入maven官网选择自己看上的版本:http://maven.apache.org/ 下载后解压开始配置环境! 二丶配置环境变量 新建系统变量 其实不建 ...
- Web前端基础——JavaScript
一.脚本程序和 javascrip Javascript脚 本是嵌套在HTML网页中的程序语言,浏览器带有脚本程序的解释器(脚本引擎).脚本也可以有多种,比如还有vbscript, JScrip ...
- 【公众号转载】超详细 Nginx 极简教程,傻瓜一看也会!
什么是Nginx? Nginx (engine x) 是一款轻量级的Web 服务器 .反向代理服务器及电子邮件(IMAP/POP3)代理服务器. 什么是反向代理? 反向代理(Reverse Proxy ...
- 【Java基础】8、java中的native方法
native是与C++联合开发的时候用的!java自己开发不用的! 使用native关键字说明这个方法是原生函数,也就是这个方法是用C/C++语言实现的,并且被编译成了DLL,由java去调用.这些函 ...
- Failed to unregister the JMX name: org.apache.commons.dbcp2:name=xxx,type=BasicDataSource
把datesource的bean的class由 org.apache.commons.dbcp2.BasicDataSource 改成 org.apache.tomcat.dbcp.dbcp.Basi ...
- Java - Thread 和 Runnable实现多线程
Java多线程系列--“基础篇”02之 常用的实现多线程的两种方式 概要 本章,我们学习“常用的实现多线程的2种方式”:Thread 和 Runnable.之所以说是常用的,是因为通过还可以通过jav ...
- 设计模式之观察者模式(Observer)(4)
简介 观察者模式(Observer)完美的将观察者和被观察的对象分离开.举个例子,用户界面可以作为一个观察者,业务数据是被观察者,用户界面观察业务数据的变化,发现数据变化后,就显示在界面上.面向对象设 ...
- vue中prop传值时加不加v-bind(冒号:)
前言:有关Vue中父组件通过prop传值给子组件时,是否加v-bind的问题,没弄清楚时感觉很乱,弄清楚之后很简单. 由于结果记起来很容易,所以先给出结果: 只有传递字符串常量时,不采用v-bind形 ...
- art-template辅助函数和子模板
art-template 前端使用 用途:主要用来处理数据和优化性能,与其他的一些模块化处理数据的插件相比,art-template处理性能好 不废话,上代码 1.art-template基本语法使用 ...