http://acm.hdu.edu.cn/showproblem.php?pid=6708
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Problem Description
We can easily find that the window of CCPC of order k is generated by taking the window of CCPC of order k−1 as C of order k,and the result of inverting C/P in the window of CCPC of order k−1 as P of order k.
And now I have an order k ,please output k-order CCPC Windows , The CCPC window of order k is a 2k∗2k matrix.
Input
The first line of input file is an integer T.
Then the T lines contains a positive integers k , (1≤k≤10)
Output
Sample Input
3 1 2 3
Sample Output
CC
PC
CCCC
PCPC
PPCC
CPPC
CCCCCCCC
PCPCPCPC
PPCCPPCC
CPPCCPPC
PPPPCCCC
CPCPPCPC
CCPPPPCC
PCCPCPPC
Source
Recursion
Starting with the four characters in the lower left corner and the remaining three are not the same,
May be used initially makes up the second, the second is divided into four parts, a lower left corner and the opposite, i.e. P becomes C, C becomes P, remaining the same.
To export a total of 2 ^ n rows, it can output line by line, suppose I want to total output behavior 8 lines, now the output line 1,
In fact, it is twice the total output behavior output line 14 line,
When the output of the lower left corner portion, which is the overall behavior of the respective row 4 opposite to the output line 1 times, 1 times the output of the same.
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <set> 10 #include <map> 11 #include <math.h> 12 const int INF=0x3f3f3f3f; 13 typedef long long LL; 14 const int= 1E9 + MOD . 7 ; 15 // const Double the PI = ACOS (-1); 16 const int MAXN 1E5 + = 10 ; . 17 the using namespace STD; 18 is // iOS :: sync_with_stdio (to false); . 19 // cin.tie (NULL ); 20 is 21 is // n represents a total of the n-layer pattern, row represents the current number of layers, f denotes the opposite of normal output or output 22 is void Solve ( int n, int Row, int F) 23 is { 24 IF (n = = 2 ) 25 { 26 is IF(f==1) 27 { 28 if(row==1) 29 printf("CC"); 30 else 31 printf("PC"); 32 } 33 else 34 { 35 if(row==1) 36 printf("PP"); 37 else 38 printf("CP"); 39 } 40 return ; 41 is } 42 is int T% = row (n-/ 2 ); // T represents the line pattern row is the last row number order 43 is IF (T == 0 ) 44 is T = n-/ 2 ; 45 IF (F == . 1 ) 46 is { 47 IF (Row> * n- 1.0 / 2 ) 48 Solve (n-/ 2 , T, 0 ); 49 the else 50 Solve (n-/ 2 , T, . 1); 51 solve(n/2,t,1); 52 } 53 else if(f==0) 54 { 55 if(row>n*1.0/2) 56 solve(n/2,t,1); 57 else 58 solve(n/2,t,0); 59 solve(n/2,t,0); 60 } 61 } 62 63 int main() 64 { 65 int n,T; 66 scanf("%d",&T); 67 while(T--) 68 { 69 scanf("%d",&n); 70 n=1<<n; 71 for(int i=1;i<=n;i++) 72 { 73 solve(n,i,1); 74 printf("\n"); 75 } 76 } 77 Return 0 ; 78 }
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <set> 10 #include <map> 11 #include <math.h> 12 const int INF=0x3f3f3f3f; 13 typedef long long LL; 14 const int mod=1e9+7; 15 //const double PI=acos(-1); 16 const int maxn=1e5+10; 17 using namespace std; 18 //ios::sync_with_stdio(false); 19 // cin.tie(NULL); 20 21 vector<string> vt[15]; 22 23 int main() 24 { 25 ios::sync_with_stdio(false); 26 cin.tie(NULL); 27 28 vt[1].push_back("CC"); 29 vt[1].push_back("PC"); 30 for (int i = 2; i <= 10; i++) 31 { 32 for (vector<string>::iterator it1 = vt[i - 1].begin(); it1 != vt[i - 1].end(); it1++) 33 { 34 vt[i].push_back(*it1 + *it1); 35 } 36 for (vector<string>::iterator it1 = vt[i - 1].begin(); it1 != vt[i - 1].end(); it1++) 37 { 38 string s1 = *it1; 39 string s2 = ""; 40 for (string::iterator it2 = s1.begin(); it2 != s1.end(); it2++) { 41 if (*it2 == 'C') 42 s2 += 'P'; 43 else 44 s2 += 'C'; 45 46 } 47 vt[i].push_back(s2 + *it1); 48 } 49 } 50 int T; 51 cin >> T; 52 while (T--) 53 { 54 int i; 55 cin >> i; 56 for (vector<string>::iterator it1 = vt[i].begin(); it1 != vt[i].end(); it1++) 57 { 58 cout << *it1 << endl; 59 } 60 } 61 return 0; 62 }