2019CCPC网络赛 杭电 6708 Windows Of CCPC(题解+代码)

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6708
题目:

Windows Of CCPC
Problem Description

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:
t题目1
And the 2-nd order CCPC window is shown in the figure:
题目2
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区,各区之间的关系与一阶相似。注意,第三区的所有符号都要与其他区的符号‘相反’。
题解图片2阶
三阶及以上阶窗口与上面推导过程相似。
题解:由于题目的数据比较小,所以我们就可以直接用数组预处理,到时候直接输出就好了。需要注意的是,我们可以发现每一阶窗口的大小是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;
}

ac图片

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值