P1259 Moving Black and White Chess Pieces Solution
Last updated on January 24, 2025 am
This article is currently an experimental machine translation and may contain errors. If anything is unclear, please refer to original Chinese version. I am continuously working to improve the translation.
Problem Description
There are $2n$ chess pieces arranged in a row. Initially, all the white pieces are on the left and all the black pieces are on the right, as shown in the figure below for $n=5$:

The rules for moving pieces are: each time, you must move two adjacent pieces simultaneously, regardless of color; they can be moved either left or right into an empty space, but the left-right order of the two pieces cannot be swapped. Each move must skip over several pieces (no sliding translation allowed). The goal is to finally arrange the pieces into an alternating black-and-white row. For $n=5$, the result should be:

Task: write a program to print out the moving process.
Input Format
A single integer $n$.
Output Format
Several lines, representing the initial state and the state after each move. Use $\verb!o!$ for a white piece, $\verb!*!$ for a black piece, and $\verb!-!$ for an empty space.
Special Judge
Sample #1
Sample Input #1
7Sample Output #1
ooooooo*******--
oooooo--******o*
oooooo******--o*
ooooo--*****o*o*
ooooo*****--o*o*
oooo--****o*o*o*
oooo****--o*o*o*
ooo--***o*o*o*o*
ooo*o**--*o*o*o*
o--*o**oo*o*o*o*
o*o*o*--o*o*o*o*
--o*o*o*o*o*o*o*Hints
$ 4\leq n\leq 100$
Solution
Looks confusing at first? Let's try drawing it out on paper following the pattern shown in the sample.
0 1 2 3 4 5 6 7 8 9 10 11 12 13
ooooooo*******--
oooooo--******o*
oooooo******--o*
ooooo--*****o*o*
ooooo*****--o*o*
oooo--****o*o*o*
oooo****--o*o*o*
ooo--***o*o*o*o*
ooo*o**--*o*o*o*
o--*o**oo*o*o*o*
o*o*o*--o*o*o*o*
--o*o*o*o*o*o*o*Then let's consider a few simpler cases.
0 1 2 3 4 5 6 7 8 9
oooo****--
ooo--***o*
ooo*o**--*
o--*o**oo*
o*o*o*--o*
--o*o*o*o*0 1 2 3 4 5 6 7 8 9 10 11
ooooo*****--
oooo--****o*
oooo****--o*
ooo--***o*o*
ooo*o**--*o*
o--*o**oo*o*
o*o*o*--o*o*
--o*o*o*o*o*At this point, we can notice the following details:
-
The pattern of the last 5 lines is always the same, namely:
ooo--***o* ooo*o**--* o--*o**oo* o*o*o*--o* --o*o*o*o*followed by n-4 copies of "o*" appended at the end.
-
The moving pattern of the earlier part: consider i starting from the n-th position; swap the positions of i and i+i+1, swap the positions of i-1 and i-1+i+1, and output the resulting string; then swap the positions of i and i+i-1, swap the positions of i-1 and i-1+i-1, and output again.
(How was this pattern discovered? You can write out more cases like this:
Start (when n=7) Index of the first swap Index of the second swap i=7 15 13 i-1=6 14 12 i=6 13 11 i-1=5 12 10 i=5 11 9 i-1=4 10 8 Difference i+1 i-1 Once you find the difference, everything becomes easy.
)
This moving operation needs to be performed n-4 times in total, which means i just needs to iterate from n down to 5.
(Why n-4 times? You can also think about it from the end: we need to create n-4 copies of "o*", and each such operation adds one more "o*" structure.
Now we can implement the code.
代码
#include<bits/stdc++.h>
using namespace std;
void move(int n){
string s;
//初始化
for(int i=0;i<n;i++){
s.push_back('o');
}
for(int i=n;i<2*n;i++){
s.push_back('*');
}
s.push_back('-');
s.push_back('-');
cout<<s<<endl;
//进行改变
for(int i=n;i>=5;i--){
swap(s[i],s[2*i+1]);
swap(s[i-1],s[2*i]);
cout<<s<<endl;
swap(s[i],s[2*i-1]);
swap(s[i-1],s[2*i-2]);
cout<<s<<endl;
}
//定义尾部字符串
string s0;
for(int i=0;i<2*(n-4);i+=2){
s0.push_back('o');
s0.push_back('*');
}
cout<<"ooo--***o*"<<s0<<endl;
cout<<"ooo*o**--*"<<s0<<endl;
cout<<"o--*o**oo*"<<s0<<endl;
cout<<"o*o*o*--o*"<<s0<<endl;
cout<<"--o*o*o*o*"<<s0;
}
int main(){
int n;
cin>>n;
move(n);
}One more point to note.
Initializing the string like this:
for(int i=0;i<2*(n-4);i+=2){
s0[i]='o';
s0[i+1]='*';
}is not allowed. Because directly accessing characters of an empty string by index is undefined behavior, which may cause the program to crash or produce no output at all.
The correct approach: consistently use the push_back() function.