#include <stdio.h> #include <string.h> #define MAX 16 #define N 4 typedef struct node { int shape[N][N]; int startx, starty; int row, col; } node; node inode[MAX]; int shapenum; int vis[N][N]; int dfs(int cur, int v[][N]); int is_vis(int v[][N], int i, int j, int m, int n, int startx, int starty); int main() { int n; int row, col; int i, j; char ch; int first; int iCase = 0; #ifndef ONLINE_JUDGE freopen("d:\\UVa\\uva_in.txt", "r", stdin); /*freopen("d:\\UVa\\uva_out.txt", "w", stdout);*/ #endif while (scanf("%d", &n) && n != 0) { shapenum = 0; memset(vis, 0, sizeof(vis)); memset(inode, 0, sizeof(inode)); while (n--) { scanf("%d%d", &row, &col); inode[shapenum].row = row; inode[shapenum].col = col; getchar(); first = 1; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { scanf("%c", &ch); if (ch == '1') { inode[shapenum].shape[i][j] = 1; if (first) { inode[shapenum].startx = i; inode[shapenum].starty = j; first = 0; } } else inode[shapenum].shape[i][j] = 0; } getchar(); } #ifndef ONLINE_JUDGE for (i = 0; i < row; i++) { for (j = 0; j < col; j++) printf("%d ", inode[shapenum].shape[i][j]); printf("\n"); } printf("\n"); #endif shapenum++; } /*printf("shapenum=%d\n", shapenum);*/ if (iCase++) printf("\n"); if (dfs(0, vis)) ; else printf("No solution possible\n"); } return 0; } int dfs(int cur, int v[][N]) { int i, j; int temp[N][N]; int m, n; int ok; int row, col; int startx, starty; if (cur == shapenum) { for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { if (!v[i][j]) return 0; } } for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("%d", v[i][j]); } printf("\n"); } return 1; } row = inode[cur].row; col = inode[cur].col; startx = inode[cur].startx; starty = inode[cur].starty; /*memcpy(temp, v, sizeof(int) * N * N);*/ for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { memcpy(temp, v, sizeof(int) * N * N); if (!temp[i][j]) { ok = 1; for (m = 0; m < row && ok; m++) { for (n = 0; n < col && ok; n++) { if (inode[cur].shape[m][n]) { if (is_vis(temp, i, j, m, n, startx, starty) == 0) ok = 0; } } } if (ok) { for (m = 0; m < row; m++) { for (n = 0; n < col; n++) { if (inode[cur].shape[m][n]) temp[i + m - startx][j + n - starty] = cur + 1; } } #ifndef ONLINE_JUDGE for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { printf("%d ", temp[i][j]); } printf("\n"); } printf("\n"); #endif if (dfs(cur + 1, temp)) return 1; #ifndef ONLINE_JUDGE for (m = 0; m < N; m++) { for (n = 0; n < N; n++) { if (temp[i + m - startx][j + n - starty] == cur + 1) temp[m][n] = 0;; } } #endif } } } } return 0; } int is_vis(int v[][N], int i, int j, int m, int n, int startx, int starty) { if (i + m - startx >= N || i + m - startx < 0 || j + n - starty >= N || j + n - starty < 0 || v[i + m - startx][j + n - starty]) return 0; return 1; }