Untitled

 avatar
unknown
c_cpp
3 years ago
1.2 kB
7
Indexable
#include<stdio.h>
int n, len[20], dx[] = {0, 0, 1, 1}, dy[] = {0, 1, 0, 1};
char res[6600][6600];

int qpow(int a, int b){
    if(b == 0) return 1;
    if(b == 1) return a;
    if(b%2 == 0){
        int tmp = qpow(a, b/2);
        return tmp*tmp;
    }
    else return a*qpow(a, b-1);
}

void rec(int row, int col, int depth, int loss){
    if(depth == 1){
        res[row][col] = '*';
        return;
    }
    int w = qpow(2, depth-1);
    int gap = len[depth-1] + 2;
    for(int i=0 ; i<4 ; i++){
        if(depth < n && loss == i) continue;
        rec(row + dx[i]*gap, col + dy[i]*gap, depth-1, 3-i);
    }
    gap = len[depth]/2 - w/2;
    for(int i=0 ; i<w ; i++){
        for(int j=0 ; j<w ; j++){
            int nx = row + gap + i;
            int ny = col + gap + j;
            res[nx][ny] = '*';
        }
    }
}

int main(){
    len[1] = 1, len[2] = 4;
    for(int i=3 ; i<=12 ; i++){
        len[i] = 2*len[i-1] + 2;
    }
    scanf("%d", &n);
    rec(0, 0, n, 0);
    for(int i=0 ; i<len[n] ; i++){
        for(int j=0 ; j<len[n] ; j++){
            if(res[i][j] == '*') printf("*");
            else printf(" ");
        }
        printf("\n");
    }
    return 0;
}
Editor is loading...