#include <stdio.h>#include <malloc.h>int binary[512][512];typedef struct queue *queue_pointer;struct queue { int x, y; int n; queue_pointer next; };void add(queue_pointer *front, queue_pointer *rear, int x, int y, int n){ if (*front) { (*rear)->next = malloc(sizeof(struct queue)); *rear = (*rear)->next; }else { *front = *rear = malloc(sizeof(struct queue)); } (*rear)->x = x; (*rear)->y = y; (*rear)->n = n; (*rear)->next = NULL;}void deleteq(queue_pointer *front, int *x, int *y, int *n){ queue_pointer temp = *front; if (*front) { *x = temp->x; *y = temp->y; *n = temp->n; } *front = temp->next; free(temp);}// 核实是否方格内的所有元素相同,并将二进制转换为十进制 int check(int x, int y, int n, int *sum){ int i, j; int ok = binary[x][y]; for (i = 0; i < n; i++) for (j = 0; j < n; j++) if (binary[x+i][y+j] != ok) { *sum = *sum * 2 + 1; return 1; } *sum = *sum * 4; if (ok) (*sum)++; return 0;}void dfs(int x, int y, int n){ int sum = 0; int fx, fy, fn; queue_pointer front, rear; front = rear = NULL; add(&front, &rear, x, y, n); while (front) { deleteq(&front, &fx, &fy, &fn); // 判断方格内元素是否全部相同,不同则分为4份压入队列 if (check(fx, fy, fn, &sum)) { add(&front, &rear, fx, fy, fn/2); add(&front, &rear, fx, fy+fn/2, fn/2); add(&front, &rear, fx+fn/2, fy, fn/2); add(&front, &rear, fx+fn/2, fy+fn/2, fn/2); } } // 十六进制打印结果 printf("%X\n", sum);}int main(){ int test, n; int i, j; scanf("%d", &test); while (test--) { scanf("%d", &n); for (i = 0; i < n; i++) for (j = 0; j < n; j++) scanf("%d", &binary[i][j]); dfs(0, 0, n); } return 0;}