2480: 【普及/提高-】【P1259】黑白棋子的移动
题目描述
有 个棋子排成一行,开始为位置白子全部在左边,黑子全部在右边,如下图为 的情况:
○○○○○●●●●●
移动棋子的规则是:每次必须同时移动相邻的两个棋子,颜色不限,可以左移也可以右移到空位上去,但不能调换两个棋子的左右位置。每次移动必须跳过若干个棋子(不能平移),要求最后能移成黑白相间的一行棋子。如 时,成为:
○●○●○●○●○●
任务:编程打印出移动过程。
输入
输出
样例输入 复制
7
样例输出 复制
ooooooo*******--
oooooo--******o*
oooooo******--o*
ooooo--*****o*o*
ooooo*****--o*o*
oooo--****o*o*o*
oooo****--o*o*o*
ooo--***o*o*o*o*
ooo*o**--*o*o*o*
o--*o**oo*o*o*o*
o*o*o*--o*o*o*o*
--o*o*o*o*o*o*o*
提示
4≤n≤100
#include<bits/stdc++.h>
using namespace std;
int n;
char a[205];
string b[4] = {"ooo*o**--","o--*o**oo","o*o*o*--o","--o*o*o*o"};
void shuchu(int l,int r) {
for (int i = l; i <=r ; i++) cout << a[i];
cout << endl;
}
void f(int step) {
if (step == 3) {
for(int i = 0; i< 4 ; i++) {
cout << b[i];
shuchu(10,2*n+2);
}
return;
}
shuchu(1,2*n+2);
swap(a[step],a[step*2+1]);
swap(a[step+1],a[step*2+2]);
shuchu(1,2*n+2);
swap(a[step],a[step*2-1]);
swap(a[step+1],a[step*2]);
f(step-1);
}
int main(){
cin >> n;
for (int i = 1; i <= n; i++) {
a[i] = 'o';
a[i+n] = '*';
}
a[2*n+1] = '-';
a[2*n+2] ='-';
f(n);
return 0;
}