1327:黑白棋子的移动

 

【题目描述】

有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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值