【题目描述】
有2n个棋子(n≥4)排成一行,开始位置为白子全部在左边,黑子全部在右边,如下图为n=5的情形:
○○○○○●●●●●
移动棋子的规则是:每次必须同时移动相邻的两个棋子,颜色不限,可以左移也可以右移到空位上去,但不能调换两个棋子的左右位置。每次移动必须跳过若干个棋子(不能平移),要求最后能移成黑白相间的一行棋子。如n=5时,成为:
○●○●○●○●○●
任务:编程打印出移动过程。
【输入】
输入n。
【输出】
移动过程。
【输入样例】
7
【输出样例】
step 0:ooooooo*******-- step 1:oooooo--******o* step 2:oooooo******--o* step 3:ooooo--*****o*o* step 4:ooooo*****--o*o* step 5:oooo--****o*o*o* step 6:oooo****--o*o*o* step 7:ooo--***o*o*o*o* step 8:ooo*o**--*o*o*o* step 9:o--*o**oo*o*o*o* step10:o*o*o*--o*o*o*o* step11:--o*o*o*o*o*o*o*
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
#define sf(a) scanf("%d\n",&a)
#define e 1e-8
#define ms(a) memset(a,0,sizeof a)
#define rep(a,b,c) for(a=b;a<=c;a++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int idata=100+5;
int n,ans;
char *ch;
int hollow;
int cnt;
void print()
{
if(cnt<9)
printf("step %d:",++cnt);
else
printf("step%d:",++cnt);
for(int i=1;i<=2*n+2;i++)
cout<<ch[i];
puts("");
}
void move(int start)
{
ch[hollow]=ch[start];
ch[hollow+1]=ch[start+1];
ch[start]=ch[start+1]='-';
hollow=start;
print();
}
void solve(int step)
{
if(step==4)
{
move(4);
move(8);
move(2);
move(7);
move(1);
}
else
{
move(step);
move(2*step-1);
solve(step-1);
}
}
int main()
{
while(cin>>n)
{
int i;
hollow=2*n+1;
ch=new char [idata];
for(i=1;i<=n;i++)
ch[i]='o';
for(i=n+1;i<=2*n;i++)
ch[i]='*';
ch[i+1]='-',ch[i]='-';
printf("step 0:");
for(i=1;i<=2*n+2;i++)
cout<<ch[i];
puts("");
solve(n);
delete [] ch;
}
}