The movement of black and white pieces (divide and conquer)

Source: Internet
Author: User

the movement of black and white pieces ( Chessman ) "Problem description"There are 2n pieces (n≥4) lined up, starting position for the white child all on the left, sunspots all on the right, as in the case of n=5:00000 move the rule is: each time you must move the adjacent two pieces, color unlimited, you can move to the left or right to the vacancy up, But you cannot change the left and right positions of two pieces. Each move must skip several pieces (cannot be translated), requiring the final move to a black-and-white line of pieces. As n=5, becomes: 0 0 0 0 0 Task: Program print out the move process. "Input Sample"Chessman.in 7 "output Example"Chessman.outstep 0:ooooooo*******--step 1:oooooo--******o*step 2:oooooo******--o*step 3:ooooo--*****o*o*step 4:o Oooo*****--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* "Algorithmic Analysis"Let's start with n=4, initial: 0000 1th step: 000--0 {-empty} 2nd step: 0-3rd Step: 0--0 4th step: 0 0 0--0 5th step:--0 0 0 0 What if n=5? We continue to try, and hope to see some rules, initial: 00000 1th step: 0000--0 2nd step: 0000--0 So, n=5 the problem is broken down into n=4 situation, as long as you do n=4 5 steps on the line 。 Similarly, the situation of n=6 can be decomposed into n=5 situation, ..., so, for a problem of size n, we easily divide him into the same type of sub-problem of scale as n-1. At the beginning of a point of view did not feel that the problem is particularly complicated, in fact, according to the loss I do the problem seems to be, the process is a strong description of the topic, there are certain rules, can be recursive recursive to do. Such a problem must have certain rules, such as when n=5, then a little change on the recovery of n=4 when the situation, which is the law can be followed, the problem becomes simple, again like the front Hanoi of the topic, the topic will be carefully explained how to move, that there are rules can follow, not much explanation, and the problem, like the situation, will return to the state of n-1; (Just praise me!) QWQ) Initialize--output--Move n pieces (function)--How to move (function)--post-move output (output function) "Code"
1#include <iostream>2#include <cstdio>3#include <cstdlib>4 using namespacestd;5 intN,nowp,num;6 Charc[ -];7 voidInitint);//Initialize8 voidMoveint);//Moving9 voidmovint);//Mobile ScenariosTen intPrint ();//Output One intMain () A { -scanf"%d",&n); -Init (n);//Initialize theMOV (n);//How to move -     return 0; - } - intprint () + { -cout<<"Step"<<num<<":"; +      for(intI=1; i<=2*n+2; i++)//Output Acout<<C[i]; atcout<<Endl; -num++;  - } - voidInitintN) - { -nowp=n*2+1;//have '-' the first position, that is, you can move the pieces to this position in      for(intI=1; i<=n;i++) c[i]='o';//Initialize -      for(inti=n+1; i<=2*n;i++) c[i]='*'; toc[n*2+1]='-'; c[n*2+2]='-'; + print (); - } the voidmovintN//Mobile Scenarios * { $     if(n==4)//if it's 4*2 a pawn,Panax Notoginseng     { -Move4); Move (8); Move (2); Move (7); Move (1);//move fourth position and fifth position first, then move 8,9 ... the     } +     Else A     { theMove (n); Move (2*n-1); MOV (n1);//when the first two statements are complete, the remaining pieces are arranged in the same scheme as moving n-1 pieces. +     } - } $ voidMoveintN) $ { -      for(intI=0; i<=1; i++) -     { theC[nowp+i]=c[n+i];//now the position of the space is moved to replace the pawn . -c[n+i]='-';//the position after the move becomes '-';Wuyi     } theNowp=n;//now the position to change * * * * - print (); Wu}

The movement of black and white pieces (divide and conquer)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.