P1745 matching gift packaging

Topic background

"The story of love and sorrow third bomb · shopping" Chapter II.

Title Description

Great God of love and sorrow at this target a gift shop to buy 2x, intends to give to classmates. Wherein x has black parts gift, x white parts gift, 2x + 2 empty boxes. This is the beginning of 2x parts gifts lined up on the left 2x empty gift box, black gift on the left, white gifts on the right, the far right has two empty boxes. Now the god of love and sorrow give this gift 2x a shift into a row of black and white. He was bored with, then set up a rule: each must move two adjacent gift, not color, can also be left and right up to the empty boxes, but you can not swap the left and right positions of the two gifts. Every move must skip several boxes (not translation), and finally ended up black and white line gifts. This rule was stumped yourself, you can help the god of love and worry out of ideas yet?

Input Format

Only one line: x

Output Format

Several lines:

Behavior of step i i-2 (1 indicated by the black gifts, gift represents white, 0 for empty boxes). Note step 0 is not added to the total.

Total final behavior.

Sample input and output

Input # 1
7
Output # 1
Step 0:1111111222222200

Step 1:1111110022222212

Step 2:1111112222220012

Step 3:1111100222221212

Step 4:1111122222001212

Step 5:1111002222121212

Step 6:1111222200121212

Step 7:1110022212121212

Step 8:1112122002121212

Step 9:1002122112121212

Step 10:1212120012121212

Step 11:0012121212121212

11

Description / Tips

40% Data: x <= 10

100% Data: x <= 70

Thinking

Hands-on simulation such as when n = 4 should be how to move

Some where n = 5 and n = 4 n = 6 As Some places as n = 5 and n = 7 Some places n = 6 and the same. . . . . .

We can put the problem into the problem of n n-1,

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N=150;

char c[N];
int n,st,sp;

void print() {
	printf("Step %d:",st);
	for(int i=1; i<=2*n+2; i++)
		printf("%c",c[i]);
	printf("\n");
	st++;
}

void move(int k) {
	for(int j=0; j<=1; j++) {
		c[sp+j]=c[k+j];
		c[k+j]='0';
	}
	sp=k;
	print();
}

void mv(int n) {
	if(n==4) {
		move(4),move(8);
		move(2),move(7),move(1);
	} else {
		move(n);
		move(2*n-1),mv(n-1);
	}
}

int main () {
	scanf("%d",&n);
	st=0,sp=2*n+1;
	for(int i=1; i<=n; i++)
		c[i]='1';
	for(int i=n+1; i<=2*n; i++)
		c[i]='2';
	c[2*n+1]='0',c[2*n+2]='0';
	print();
	mv(n);
	printf("%d\n",st-1);
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/mysh/p/11801810.html