/* ecsol2frans30x30.cpp This program reads the (42) solutions from the Exact Cover problem for the frans30x30 Nonogram and produces the file 'frans30x30sol_l.bmp'. This include file is refered to at http://www.iwriteiam.nl/D0906.html#13 Copyright (C) 2009 Frans Faase This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. GNU General Public License: http://www.iwriteiam.nl/GNU.txt */ #include #include #include "BMPimage.h" // see http://www.iwriteiam.nl/BMPimage_h.txt #define MAX_SIZE 100 int main(int argc, char *argv[]) { CharTableBMPimage image; image.width = 35 * 6 + 5; image.height = 35 * 7 + 5; image.allocate(); image.define(' ', 127, 127, 127); image.define('0', 255, 255, 255); image.define('1', 0, 0, 0); FILE *f = stdin; int px = 0; int py = 0; char ch = fgetc(f); while (!feof(f)) { // parse a sinle line char col[MAX_SIZE][MAX_SIZE]; char row[MAX_SIZE][MAX_SIZE]; for (int i = 0; i < MAX_SIZE; i++) for (int j = 0; j < MAX_SIZE; j++) col[i][j] = row[i][j] = ' '; int nr_row = 0; int nr_col = 0; while (!feof(f) && ch != '\n') { char buf[9]; for (int i = 0; i < 8 && !feof(f); i++) { buf[i] = ch; ch = fgetc(f); } buf[8] = '\0'; int nr = atoi(buf+4); if (buf[0] == 'r' && buf[1] == 'o' && buf[2] == 'w') { if (nr_row < nr) nr_row = nr; if (nr > MAX_SIZE) { printf("Error row %d > %d\n", nr, MAX_SIZE); return 1; } nr--; int i = 0; while (!feof(f) && (ch == '0' || ch == '1')) { row[nr][i++] = ch; ch = fgetc(f); if (i >= MAX_SIZE) { printf("Error col %d > %d\n", i+1, MAX_SIZE); return 1; } } } else if (buf[0] == 'c' && buf[1] == 'o' && buf[2] == 'l') { if (nr_col < nr) nr_col = nr; if (nr > MAX_SIZE) { printf("Error col %d > %d\n", nr, MAX_SIZE); return 1; } nr--; int i = 0; while (!feof(f) && (ch == '0' || ch == '1')) { col[i++][nr] = ch; ch = fgetc(f); if (i >= MAX_SIZE) { printf("Error row %d > %d\n", i+1, MAX_SIZE); return 1; } } } else { printf("Error: |%s|\n", buf); return 0; } while (!feof(f) && ch != '|' && ch != '\n') ch = fgetc(f); if (!feof(f) && ch == '|') ch = fgetc(f); } if (!feof(f) && ch == '\n') ch = fgetc(f); for (int i = 0; i < nr_row; i++) for (int j = 0; j < nr_col; j++) if (col[i][j] == ' ' || col[i][j] != row[i][j]) { printf("Error %d,%d differ %c %c\n", i, j,col[i][j], row[i][j]); for (int i = 0; i < nr_row; i++) { for (int j = 0; j < nr_col; j++) printf("%c", row[i][j]); printf("\n"); } printf("\n"); for (int i = 0; i < nr_row; i++) { for (int j = 0; j < nr_col; j++) printf("%c", col[i][j]); printf("\n"); } printf("\n"); return 1; } for (int i = 0; i < nr_row; i++) { for (int j = 0; j < nr_col; j++) { image.set(5 + px * 35 + j, 5 + py * 35 + i, row[i][j]); //image.set(5 + px + j * 7, 5 + py + i * 8, row[i][j]); printf("%c", row[i][j]); } printf(" "); } printf("\n"); px++; if (px == 6) { py++; px = 0; } } image.save("frans30x30sol.bmp"); return 0; }