Leptonica 1.68
C Image Processing Library
|
00001 /*====================================================================* 00002 - Copyright (C) 2001 Leptonica. All rights reserved. 00003 - This software is distributed in the hope that it will be 00004 - useful, but with NO WARRANTY OF ANY KIND. 00005 - No author or distributor accepts responsibility to anyone for the 00006 - consequences of using this software, or for whether it serves any 00007 - particular purpose or works at all, unless he or she says so in 00008 - writing. Everyone is granted permission to copy, modify and 00009 - redistribute this source code, for commercial or non-commercial 00010 - purposes, with the following restrictions: (1) the origin of this 00011 - source code must not be misrepresented; (2) modified versions must 00012 - be plainly marked as such; and (3) this notice may not be removed 00013 - or altered from any source or modified source distribution. 00014 *====================================================================*/ 00015 00016 /* 00017 * sudokutest.c 00018 * 00019 * Tests sudoku solver and generator. 00020 */ 00021 00022 #include "allheaders.h" 00023 00024 static const char *startsol = "3 8 7 2 6 4 1 9 5 " 00025 "2 6 5 8 9 1 4 3 7 " 00026 "1 4 9 5 3 7 6 8 2 " 00027 "5 2 3 7 1 6 8 4 9 " 00028 "7 1 6 9 4 8 2 5 3 " 00029 "8 9 4 3 5 2 7 1 6 " 00030 "9 7 2 1 8 5 3 6 4 " 00031 "4 3 1 6 7 9 5 2 8 " 00032 "6 5 8 4 2 3 9 7 1"; 00033 00034 main(int argc, 00035 char **argv) 00036 { 00037 l_int32 unique; 00038 l_int32 *array; 00039 L_SUDOKU *sud; 00040 static char mainName[] = "sudokutest"; 00041 00042 if (argc != 1 && argc != 2) 00043 return ERROR_INT(" Syntax: sudokutest [filein]", mainName, 1); 00044 00045 if (argc == 1) { 00046 /* Generate a new sudoku by element elimination */ 00047 array = sudokuReadString(startsol); 00048 sud = sudokuGenerate(array, 3693, 28, 7); 00049 sudokuDestroy(&sud); 00050 lept_free(array); 00051 return 0; 00052 } 00053 00054 /* Solve the input sudoku */ 00055 if ((array = sudokuReadFile(argv[1])) == NULL) 00056 return ERROR_INT("invalid input", mainName, 1); 00057 if ((sud = sudokuCreate(array)) == NULL) 00058 return ERROR_INT("sud not made", mainName, 1); 00059 sudokuOutput(sud, L_SUDOKU_INIT); 00060 startTimer(); 00061 sudokuSolve(sud); 00062 fprintf(stderr, "Time: %7.3f sec\n", stopTimer()); 00063 sudokuOutput(sud, L_SUDOKU_STATE); 00064 sudokuDestroy(&sud); 00065 00066 /* Test for uniqueness */ 00067 sudokuTestUniqueness(array, &unique); 00068 if (unique) 00069 fprintf(stderr, "Sudoku is unique\n"); 00070 else 00071 fprintf(stderr, "Sudoku is NOT unique\n"); 00072 lept_free(array); 00073 00074 return 0; 00075 } 00076 00077