#include #include #define INF (1<<26) #define MAXN 1025 #define MAXL 11 char img[MAXN][MAXN]; int desc[MAXN][MAXN][MAXL]; int d[4][2] = {{0,0},{0,1},{1,0},{1,1}}; int solve(int r, int c, int logsize) { if(logsize == 0) { return 0; } int& description = desc[r][c][logsize]; // pole sub: ceny zakomprimovani jednotlivych ctvrtin // pole white: pocet bilych pixelu v jednotlivych ctvrtinach int sub[4], white[4]; int half = 1<<(logsize-1); for(int k = 0; k < 4; k++) { sub[k] = solve(r+half*d[k][0], c+half*d[k][1], logsize-1); white[k] = 0; // kolik je v teto ctvrtine bilych pixelu? for(int ii = 0; ii < half; ii++) { for(int jj = 0; jj < half; jj++) { if(img[r+half*d[k][0]+ii][c+half*d[k][1]+jj] == 0) { white[k]++; } } } } int bestCost = INF; for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++) { // ctvrtina i bude bila, ctvrtina j cerna if(i == j) continue; // prebarvit musime bile pixely v ctvrtine j, a cerne v i int currCost = white[j] + (half*half-white[i]); // pricteme predpocitane ceny komprimovani zbylych ctvrtin for(int k = 0; k < 4; k++) { if(k == i || k == j) continue; currCost += sub[k]; } if(currCost < bestCost) { bestCost = currCost; description = (i << 4) + j; } } } return bestCost; } void printDescription(int r, int c, int logsize) { if(logsize == 0) { printf("%d", img[r][c]); return; } int description = desc[r][c][logsize]; int white = description >> 4, black = description % 16; printf("("); for(int quarter = 0; quarter < 4; quarter++) { if(quarter == black) { printf("1"); } else if(quarter == white) { printf("0"); } else { int half = 1<<(logsize-1); printDescription(r+half*d[quarter][0], c+half*d[quarter][1], logsize-1); } } printf(")"); } int main(int argc, char** argv) { char header[10]; int width, height; scanf("%s %d %d", header, &width, &height); for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { int val; scanf("%d", &val); img[i][j] = val; } } int depth = 0; while((1<