题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6708
题目:
In recent years, CCPC has developed rapidly and gained a large number of competitors .One contestant designed a design called CCPC Windows .The 1-st order CCPC window is shown in the figure:
And the 2-nd order CCPC window is shown in the figure:
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 input file contains T test samples.(1<=T<=10)
The first line of input file is an integer T.
Then the T lines contains a positive integers k , (1≤k≤10)
Output
For each test case,you should output the answer .
Sample Input
3
1
2
3
Sample Output
CC
PC
CCCC
PCPC
PPCC
CPPC
CCCCCCCC
PCPCPCPC
PPCCPPCC
CPPCCPPC
PPPPCCCC
CPCPPCPC
CCPPPPCC
PCCPCPPC
题意:其实题目的意思可以直接从图片里面看出来,如下图:
假设我们将第一阶窗口化为4个区,可以发现1、2、4区的符号是一样的,第三个区与其他区符号是‘相反’的(C就转为P,P就转为C)。
在第二阶及以上的情况下,我们可以知道它是以上一阶窗口为基础,将上一阶窗口认为是1区,各区之间的关系与一阶相似。注意,第三区的所有符号都要与其他区的符号‘相反’。
三阶及以上阶窗口与上面推导过程相似。
题解:由于题目的数据比较小,所以我们就可以直接用数组预处理,到时候直接输出就好了。需要注意的是,我们可以发现每一阶窗口的大小是2k(k为阶数)。所以第2区的横坐标范围是从2k-1+1 ~ 2k,纵坐标范围是从1 ~ 2k-1(博主把初始化起点定为1)。其他区与此类似。
代码与注释如下:
#include<iostream>
#include<cmath>
using namespace std;
char map[1050][1050];//最大为1024
void init() {
map[1][1] = map[1][2] = map[2][2] = 'C';
map[2][1] = 'P';
for(int i=2;i<=10;i++) {
int left=pow(2,i-1);//1区的大小
int right=pow(2,i);//窗口的总大小
for(int j=1;j<=left;j++)
for(int k=left+1;k<=right;k++)
map[j+left][k]=map[j][k]=map[j][k-left];//2、4区同时赋值
for(int j=left+1;j<=right;j++)
for(int k=1;k<=left;k++)
map[j][k]=(map[j-left][k]=='P')?'C':'P';//3区赋值
}
}
int main() {
int T;
init();
cin>>T;
while(T--) {
int k,len;
cin>>k;
len=pow(2,k);//len为k阶的大小范围
for(int i=1;i<=len;i++) {
for(int j=1;j<=len;j++)
cout<<map[i][j];
cout<<endl;
}
}
return 0;
}