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串,即一个数的二进制。

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

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

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<iostream> 
 5 #include<cstring>
 6 #include<cmath>
 7 #include<cctype>    //isdigit 
 8 using namespace std;
 9 typedef long long ll;
10 #define enter printf("
")
11 const int maxn = 1e6 + 5;
12 const int INF = 0x3f3f3f3f;
13 inline ll read()
14 {
15     ll ans = 0;
16     char ch = getchar(), last = ' ';
17     while(!isdigit(ch)) {last = ch; ch = getchar();}
18     while(isdigit(ch))
19     {
20         ans = ans * 10 + ch - '0'; ch = getchar();    
21     }
22     if(last == '-') ans = -ans;
23     return ans;
24 }
25 inline void write(ll x)
26 {
27     if(x < 0) {putchar('-'); x = -x;}
28     if(x == 0) {putchar('0'); return;}
29     int q[100], N = 0;
30     q[1] = 0;
31     while(x) {q[++N] = x % 10; x /= 10;}
32     while(N) {putchar('0' + q[N]); --N;}
33 }
34 
35 int n;
36 bool vis[maxn]; 
37 void print(int x)        //从高位开始输出每一位 
38 {
39     vis[x] = 1;
40     for(int i = n - 1; i >= 0; --i)
41     {
42         if((x >> i) & 1) printf("X");
43         else printf("O");
44     }
45     enter;
46 }
47 void dfs(int step, int x)
48 {
49     if(!vis[x]) print(x);        //之所以放在这,而不是第53行之后,是为了输出最开始的OOOOOOO情况 
50     if(step == (1 << n) + 1) exit(0);
51     for(int i = 0; i <= n - 1; ++i)
52     {
53         int now = x ^ (1 << i);
54         if(!vis[now]) dfs(step + 1, now);
55     }
56 }
57 
58 int main()
59 {
60     n = read();
61     dfs(1, 0); 
62     for(int i = 1; i <= n; ++i) printf("O"); enter;
63     return 0;
64 }
原文地址:https://www.cnblogs.com/mrclr/p/9057164.html