预处理出来所有要求的图形堆(主要是因为题目的数据量不大),通过一个二维矩阵去存储,最后根据对应的阶段输出对应的图形堆即可

import java.util.*;
import java.io.*;

public class Main {
    
    static int N = (1 << 9) + 10;
    static int[][] f = new int[N][N];
    
    public static void init() {
        f[1][1] = 1;
        for (int row = 2; row <= 10; row++) {
            int curWidth = (int) Math.pow(2, row - 1);
            int lastWidth = curWidth / 2;
            int lastSum = (int) Math.pow(lastWidth, 2);
            // 右上角
            for (int i = 1; i <= lastWidth; i++) {
                for (int j = 1; j <= lastWidth; j++) {
                    f[i][j + lastWidth] = f[i][j];
                }
            }
            // 右下角
            for (int i = 1; i <= lastWidth; i++) {
                for (int j = 1; j <= lastWidth; j++) {
                    f[i + lastWidth][j + lastWidth] = f[i][j];
                }
            }
            // 左下角
            for (int i = 1; i <= lastWidth; i++) {
                for (int j = 1; j <= lastWidth; j++) {
                    // 取反
                    f[i + lastWidth][j] = f[i][j] == 1 ? 0 : 1;
                }
            }
        }
    }
    
    public static void main(String[] args) throws Exception {
        // 预处理
        // 先将所有阶段的图形堆都求出来,然后直接根据输出对应的阶段图形堆即可
        init();
        Scanner in = new Scanner(System.in);
        // 优化输出
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int t = in.nextInt();
        while (t-- > 0) {
            int n = in.nextInt();
            // 计算当前阶段的宽度
            int curWidth = (int) Math.pow(2, n - 1);
            for (int i = 1; i <= curWidth; i++) {
                for (int j = 1; j <= curWidth; j++) {
                    bw.write(f[i][j] + " "); 
                }
                bw.newLine();
            }
        }
        
        // 关闭流
        bw.flush();
        bw.close();
        
        // 1    2    3    4        5 6 7 8 9  ...    10
        // 1^2  2^2  4^2  8^2                 ...    (2^9)^2 = 256 * 1024
    }    
}