The movement of 3--black and white pieces
First, experience
Ii. Topics and Analysis
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)
Iii. Code and results
1#include <iostream>2 using namespacestd;3 4 intN;5 intstep=0;6 Charans[101];7 intsp;8 9 voidprint () {Tencout<<"Step"<<step<<":"; One for(intI=1; i<=2*n+2; i++) cout<<Ans[i]; Acout<<Endl; -step++; - } the - voidInitintN) { - for(intI=1; i<=n;i++) ans[i]='o'; - for(inti=n+1; i<=2*n;i++) ans[i]='*'; + for(intI=2*n+1; i<=2*n+2; i++) ans[i]='-'; - print (); +sp=2*n+1; A } at - voidMoveintk) { - for(intI=0; i<=1; i++){ -ans[sp+i]=ans[k+i]; -ans[k+i]='-';// - } insp=K; -Print ();// to } + - voidmvintN) { the if(n==4){ *Move4), Move (8), Move (2), Move (7), Move (1); $ }Panax Notoginseng Else{ -Move (n), move (2*n-1), MV (n1); the } + } A the intMain () { +Cin>>N; - init (n); $ MV (n); $ return 0; -}
The movement of 3--black and white pieces