Piece Mobile 1295

This question is more difficult

Subject description:

There are 2n pieces (n≥4) in a row, starting position on the left side all white, all black on the right. (Where the letter O represents a piece of white, black letter X represents stones) OOOOXXXX. Mobile pieces rule is: every two adjacent pieces must be moved, not color, may be left a space right up, but can not replace two pieces of right and left position, each movement must be skipped several pieces (no translation), can be moved to the last row requires pieces of black and white. For example, when n = 4, where the final arrangement of: OXOXOXOX. An output process of moving pieces.

Enter a description:

Plural sets of inputs, each a positive integer, per line.

Output Description:

Outputting moving step, each step of the operation per line

Sample input:

4

Sample output:

4,5-->9,10

8,9-->4,5

2,3-->8,9

7,8-->2,3

1,2-->7,8

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 int n,mj,mi,x,y;
 6 char a[101];
 7 void move(int i)
 8 {
 9     a[mj] = a[i];
10     a[mj + 1] = a[i + 1];
11     a[i] = '-';
12     a[i + 1] = '-';
13     mj = i;
14     for(i = 0; i < 2 * n + 2; i++)
15         //printf("%c", a[i]);
16         if(a[i]=='-'){
17             cout<<i+1<<","<<i+2<<"-->"<<x<<","<<y;
18             x=i+1;
19             y=i+2;
20             break;
21         }
22     printf("\n");
23 }
24 void fun(int n)
25 {
26     if (n == 4){        
27         move(3);move(7); 
28         move(1);move(6);
29         move(0);
30     }
31     else{
32         move(n - 1);
33         move(2 * n - 2);
34         fun(n - 1);
35     }
36 }
37 int main()
38 {
39     int i, j;
40     while(cin>>n){
41         mi = 1;
42         mj = 2* n;
43         for(i = 0; i < n; i++)
44             a[i] = 'o';
45      
46         for(i = n; i < 2 * n; i++)
47             a[i] = '*';
48         a[2*n] = '-';
49         a[2*n + 1] = '-';
50         x=2*n+1;
51         y=2*n+2;
52         fun(n);
53     }
54     return 0;
55 }
signal
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 int n, mj;
 6 int mi;
 7 char a[101];
 8 void move(int i)
 9 {
10     a[mj] = a[i];
11     a[mj + 1] = a[i + 1];
12     a[i] = '-';
13     a[i + 1] = '-';
14     mj = i;
15     printf("step%2d:", mi++);
16     for(i = 0; i < 2 * n + 2; i++)
17         printf("%c", a[i]);
18     printf("\n");
19 }
20 void fun(int n)
21 {
22     if (n == 4){        
23         move(3);move(7); 
24         move(1);move(6);
25         move(0);
26     }
27     else{
28         move(n - 1);
29         move(2 * n - 2);
30         fun(n - 1);
31     }
32 }
33 int main()
34 {
35     int i, j;
36     scanf("%d", &n);
37     mi = 1;
38     mj = 2* n;
39     for(i = 0; i < n; i++)
40         a[i] = 'o';
41  
42     for(i = n; i < 2 * n; i++)
43         a[i] = '*';
44  
45     a[2*n] = '-';
46     a[2*n + 1] = '-';
47     printf("step%2d:", 0);
48     for(i = 0; i < 2 * n + 2; i++)
49         printf("%c", a[i]);
50     puts("");
51     fun(n);
52     return 0;
53 }

 

Guess you like

Origin www.cnblogs.com/zq-dmhy/p/11074332.html