hdu-6708(打表)

Windows Of CCPC

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

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:

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

思路:

对于行列数为2k 的矩阵,它的左上、右上、右下均为2k-1 的矩阵,左下为2k-1 的逆矩阵。那么打表时,每次都从k=2开始做起,填补每一级K的右上右下左下,注意初始化k=1的表。循环的方法可以是:

for(每一个k次的矩阵){
	int x=(1<<k);
	for(当前矩阵的每一行){
		for(当前行的前半段/后半段){
			if(处于右上块)
			if(处于左下块)
			if(处于右下块)
		}
	}

}

这样可以比较方便地处理。以下为AC代码:

import java.io.PrintWriter;
import java.util.Scanner;

public class Main{
	public static void main(String args[]) {
		Scanner sc=new Scanner(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int T,n,maxn=(1<<12);
		StringBuffer s[]=new StringBuffer[maxn];
		T=sc.nextInt();
		while(T-->0) {
			n=sc.nextInt();
			for(int i=0;i<maxn;i++) {
				s[i]=new StringBuffer();
			}
			s[0].append("CC");
			s[1].append("PC");
			
			for(int k=2;k<=n;k++) {
				int x=(1<<k);
				for(int i=0;i<x;i++) {
					for(int j=0;j<2;j++) {
						if(i<x/2&&j==1) {
							s[i].append(s[i]);
						}else if(i>=x/2&&j==0) {
							for(int l=0;l<x/2;l++) {
								char ch=s[i-x/2].charAt(l);
								if(ch=='C') {
									s[i].append("P");
								}else {
									s[i].append("C");
								}
								
							}
						}else if(i>=x/2&&j==1) {
							for(int l=0;l<x/2;l++) {
								s[i].append(s[i-x/2].charAt(l));
							}
						}
					}
				}
			}
			for(int i=0;i<(1<<n);i++) {
				for(int j=0;j<(1<<n);j++) {
					out.print(s[i].charAt(j));
				}
				out.println();
			}
			out.flush();
		}
		out.close();
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值