题目链接F. Sierpinski Triangle
找左上角起点规律
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
const int maxn = 1500;
char vis[maxn][maxn];
void dfs(int num,int x,int y)
{
if(num == 1){
vis[x][y]=vis[x+1][y]=vis[x][y+1]=vis[x+1][y+1]='X';
return ;
}
int t_size = pow(2,num - 1);
dfs(num - 1,x,y);
dfs(num - 1,x + t_size,y - t_size / 2);
dfs(num - 1,x + t_size,y + t_size / 2);
}
int main()
{
int t;
cin>>t;
int cnt = 0;
while(t--){
int n;
cin>>n;
int p_size = pow(2,n);
memset(vis,32,sizeof(vis));
for(int i = 1; i <= p_size; i++){
vis[i][p_size + 1] = 0;
}
cnt++;
cout<<"Triangle #"<<cnt<<":"<<endl;
dfs(n,1,pow(2,n - 1));
for(int i = 1; i <= p_size; i++){
cout<<vis[i] + 1<<endl;
}
cout<<endl;
}
return 0;
}