【例7.6】黑白棋子的移動

來源:互聯網
上載者:User

標籤:font   任務   題目   work   void   size   space   cst   開始   

【例7.6】黑白棋子的手機連結:http://ybt.ssoier.cn:8088/problem_show.php?pid=1327
時間限制: 1000 ms         記憶體限制: 65536 KB

【題目描述】

有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<cstring>#include<cstdio>#include<iomanip>using namespace std;int n,sp,step;char a[101];void print(){    cout<<"step"<<setw(2)<<step<<":";    for(int i=1;i<=2*n+2;i++)    printf("%c",a[i]);    cout<<endl;    step++;}void inti(int n){    for(int i=1;i<=n;i++)a[i]=‘o‘;    for(int i=n+1;i<=2*n;i++)a[i]=‘*‘;    a[2*n+1]=a[2*n+2]=‘-‘;    sp=2*n+1;    print();}void mv(int k){    a[sp]=a[k];a[sp+1]=a[k+1];    a[k]=‘-‘;a[k+1]=‘-‘;    sp=k;    print();}void work(int n){    if(n==4)    {        mv(4);mv(8);mv(2);mv(7);mv(1);    }    else     {        mv(n);mv(2*n-1);work(n-1);    }}int main(){        cin>>n;    inti(n);    work(n);}

 


【例7.6】黑白棋子的移動

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.