// IDEA: just try all shapes recursively & try to fit them together #include #include #include #include char A[10][9]; int used[10]; int N; // # of shapes int quit; ofstream out; char shapes[11] [10][9]; int put (int x, int y, int cnt) { // try to put piece j at (x, y) // check for (int i=0; i < 10; i++) for (int j=0; j < 9; j++) { if ((i+y >= 10 || j+x >= 9) && (shapes[cnt][i][j] != ' ')) return 0; if (A[i+y][j+x] != ' ' && shapes[cnt][i][j] != ' ') return 0; } for (i=0; i < 10; i++) for (j=0; j < 9; j++) { if (shapes[cnt][i][j] != ' ') { A[i+y][j+x] = shapes[cnt][i][j]; } } return 1; } void unput (int x, int y, int cnt) { for (int i=0; i < 10; i++) for (int j=0; j < 9; j++) { if (shapes[cnt][i][j] != ' ') A[i+y][j+x] = ' '; } } void fit() { if (!quit) { // search for next for (int j=0; j < N; j++) if (!used[j]) break; if (j == N) { for (int a=0; a < 10; a++) { for (int b=0; b < 9; b++) out << A[a][b]; out << endl; } out << endl; quit = 1; // exit(1); } used[j] = 1; // try to fit piece j for (int y=0; y < 10; y++) for (int x=0; x < 9; x++) if (put(x, y, j)) {fit(); unput(x, y, j);}; // JUICER used[j] = 0; } } void main() { ifstream in; in.open("shapes.in"); out.open("shapes.out"); int nn; in >> nn; for (int i=0; i < nn; i++) { in >> N; char buf[10]; in.getline(buf, 80); memset(shapes, ' ', sizeof(shapes) ); memset(A, ' ', sizeof(A) ); for (int k=0; k < N; k++) { used[k] = 0; int j=0; while (1) { in.getline(shapes[k][j], 100); shapes[k][j][strlen(shapes[k][j])] = ' '; if (shapes[k][j][0] == '0') break; j++; } shapes[k][j][0] = ' '; } quit = 0; fit(); } in.close(); out.close(); } Downloader failed! Response object 006~ASP 0159~Buffering Off~Buffering must be on.