JOJ-2165-Hilbert Curve


The pictures above are Hilbert curve.You can see a Hilbert curve with a certain recursion depth n is composed of four different Hilbert curves with a certain recursion depth n-1 and three segments.

For this problem, you are to output the Hilbert Curve up to a certain recursion depth, using just 'X'.Draw the smallest Hilbert curve (that is not divided any further) as follows:

XXX
X
XXX
To see how to draw larger Hilbert curve, take a look at the sample output.

Input

The input contains several testcases. Each is specified by an integer n (1 < n < 9).

Output

For each test case, output the Hilbert curve with the certain recursion depth n.The output must not contain any trailing blanks. Print an empty line after each test case.

Sample Input

2
3

Sample Output

XXXXX X
X   X X
XXX XXX
  X
XXX XXX
X   X X
XXXXX X

XXXXX XXXXX XXX
X   X X   X X
XXX XXX XXX XXX
  X     X     X
XXX XXX X XXX X
X   X X X X X X
XXXXX X XXX XXX
      X
XXXXX X XXX XXX
X   X X X X X X
XXX XXX X XXX X
  X     X     X
XXX XXX XXX XXX
X   X X   X X
XXXXX XXXXX XXX





#include<iostream> using namespace std; void main() { int lines[9]; lines[0]=1; for(int i=1;i<9;i++) lines[i]=lines[i-1]*2+1; char Hilbert[520][520]; Hilbert[0][0]='X'; int r=1; for(int i=1;i<9;i++) { for(int j=0;j<r;j++) for(int k=0;k<r;k++) { Hilbert[j][r+1+k]=Hilbert[k][r-1-j]; Hilbert[r+1+j][k]=Hilbert[j][k]; Hilbert[r+1+j][r+1+k]=Hilbert[r-1-k][j]; } Hilbert[0][r]='X'; Hilbert[r][r-1]='X'; Hilbert[2*r][r]='X'; r=r*2+1; } int n; while(cin>>n) { for(int i=0;i<lines[n];i++) { int len=lines[n]-1; while(Hilbert[i][len]!='X') len--; for(int j=0;j<=len;j++) if(Hilbert[i][j]=='X') cout<<Hilbert[i][j]; else cout<<' '; cout<<endl; } cout<<endl; } }
 
 
 
没有对数组进行初始化,如果全部先赋为空格反而会费时间 在打印时判断更好
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值