int main() { // open the input file FILE * fp; fp = fopen("puzzle.in", "r"); int numCases, curCase, i, j; // read in the number of cases fscanf(fp, "%d", &numCases); // loop over the cases for(curCase = 1; curCase <= numCases; curCase++) { // read in this cases size, and tiles fscanf(fp, "%d", &tileSize); for(i=0; i<NUMTILES; i++) for(j=0; j<tileSize; j++) fscanf(fp, "%s ", tiles[i][j]); // find the number of valid configurations solutionsFound = 0; findSolutions(0); // print the answer printf("Puzzle #%d: ", curCase); if(solutionsFound == 0) printf("NO"); else if(solutionsFound == 1) printf("YES"); else printf("MULTIPLE"); printf("\n"); } // All done fclose(fp); return 0; }
void findSolutions(int pos) { int i; // if we placed all the values already, check feasibility if(pos == NUMTILES) { if(verify()) solutionsFound++; return; } // try placing each unused value at this position for(i=0; i<NUMTILES; i++) { if(!used[i]) { used[i] = TRUE; perm[pos] = i; findSolutions(pos+1); used[i] = FALSE; } } }
int main(int argc, char* argv[]) { addKnights(0, 0, 0, 0); maximiseSolutions(0, 1, 3, 0); maximiseSolutions(0, 1, 7, 0); tidySolutions(1, 1, 3, 0); tidySolutions(1, 1, 7, 0); addKnights(1, 1, 3, 0); addKnights(1, 1, 7, 0); maximiseSolutions(1, 2, 7, 0); maximiseSolutions(1, 2, 9, 0); maximiseSolutions(1, 2, 10, 0); maximiseSolutions(1, 2, 12, 0); maximiseSolutions(1, 2, 14, 0); maximiseSolutions(1, 2, 16, 0); tidySolutions(1, 2, 7, 0); tidySolutions(1, 2, 10, 0); tidySolutions(2, 2, 7, 0); tidySolutions(2, 2, 9, 0); tidySolutions(2, 2, 10, 0); tidySolutions(2, 2, 14, 0); tidySolutions(3, 2, 12, 0); tidySolutions(3, 2, 16, 0); //dumpSolutions(3, 2, 16, tidiedFile, true); int attack = 0; int knightSoFar = 0; int coverage = 0; int counter = 0; if (argc > 5) { attack = atoi(argv[2]); coverage = atoi(argv[3]); knightSoFar = atoi(argv[4]); counter = atoi(argv[5]); if (strcmp(argv[1], "-a") == 0) { addKnights(attack, knightSoFar, coverage, counter); } else if (strcmp(argv[1], "-m") == 0) { maximiseSolutions(attack, knightSoFar, coverage, counter); } else if (strcmp(argv[1], "-d") == 0) { bool showCoverage = false; if (argc > 7) { showCoverage = true; } if (argc > 6) { dumpSolutions(attack, knightSoFar, coverage, argv[5], showCoverage, counter); } else { dumpSolutions(attack, knightSoFar, coverage, tidiedFile, showCoverage, counter); } } else if (strcmp(argv[1], "-f") == 0) { findSolutions(); } } return 0; }