Beispiel #1
0
int main(int argc, char *argv[])
{
	int multi_flag, sols = 0;
	char sudoku_table[250];
	int sudoku_sols[50][81];
	SudokuSolver sudoku;
	
	fgets(sudoku_table, 250, stdin);
	
	if (input_check(sudoku_table))
		return 0;
		
	sudoku.start(sudoku_table);
	
	if (sudoku.invalid_check())
		return 0;
	
	sudoku.optimize1();
	
	if (sudoku.solve())
	{
		sudoku.print();

		for (int j = 0; j < 81; j++)
			sudoku_sols[sols][j] = sudoku.solution[j];

		sols++;

			while (1)
			{
				sudoku.clear();
				sudoku.optimize1();
				sudoku.solve();
				
				for (int i = 0; i < sols; i++)
				{
					multi_flag = 0;
					for (int j = 0; j < 81; j++)
					{
						if (sudoku_sols[i][j] == sudoku.solution[j])
							multi_flag++;
					}
							
					if (multi_flag == 81)
						return 0;
				}
						
				sudoku.print();
				
				for (int j = 0; j < 81; j++)
					sudoku_sols[sols][j] = sudoku.solution[j];
					
				sols++;
			}
	}
	
	return 0;
} // main
Beispiel #2
0
//Main Flow
//Grab arguments from command line
//Turn on options flags
//Generate sudoku from input file
//Save to output file
//Solve sudoku puzzle
//Generate log for solution
//If at any time the runtime is longer than timeout, then end for generation and for solving
int main(int argc, char *argv[]) {
//    char* inputFiles[61] = {
//        "input.txt",
//        "PE1.txt",
//        "PE2.txt",
//        "PE3.txt",
//        "PE4.txt",
//        "PE5.txt",
//        "PE6.txt",
//        "PE7.txt",
//        "PE8.txt",
//        "PE9.txt",
//        "PE10.txt",
//        "PE11.txt",
//        "PE12.txt",
//        "PE13.txt",
//        "PE14.txt",
//        "PE15.txt",
//        "PE16.txt",
//        "PE17.txt",
//        "PE18.txt",
//        "PE19.txt",
//        "PE20.txt",
//        "PE21.txt",
//        "PE22.txt",
//        "PE23.txt",
//        "PE24.txt",
//        "PE25.txt",
//        "PE26.txt",
//        "PE27.txt",
//        "PE28.txt",
//        "PE29.txt",
//        "PE30.txt",
//        "PE31.txt",
//        "PE32.txt",
//        "PE33.txt",
//        "PE34.txt",
//        "PE35.txt",
//        "PE36.txt",
//        "PE37.txt",
//        "PE38.txt",
//        "PE39.txt",
//        "PE40.txt",
//        "PE41.txt",
//        "PE42.txt",
//        "PE43.txt",
//        "PE44.txt",
//        "PE45.txt",
//        "PE46.txt",
//        "PE47.txt",
//        "PE48.txt",
//        "PE49.txt",
//        "PE50.txt",
//        "PH1.txt",
//        "PH2.txt",
//        "PH3.txt",
//        "PH4.txt",
//        "PH5.txt",
//        "PM1.txt",
//        "PM2.txt",
//        "PM3.txt",
//        "PM4.txt",
//        "PM5.txt"
//    };
    int m = 0,n = 0,p = 0,q = 0;
    float timeout = 300;
    char* inputFileName = "input.txt";
    char* outputFileName = "output.txt";
    vector<char*> options;
    if (argc >= 3) {
        inputFileName = argv[0];
        outputFileName = argv[1];
        timeout = atol(argv[2]);
        int opNum = argc - 3;
        for (std::size_t i = 0; (std::size_t)i < (std::size_t)opNum; ++i) {
            options[i] = argv[3 + i];
        }
    }
//    SudokuSolver S;
//    S.setFlag(HeuristicFlag::kFC,true);
//    for (int i = 0; i < 61; ++i) {
////        SudokuReader R (inputFileName,"");
//        SudokuReader R(inputFiles[i],"");
//        char** sudoku = R.loadSudoku(n,p,q,timeout);
//        cout << "Sudoku Puzzle: " << inputFiles[i] << " @ i=" << i << " with parameters:\n"
////             << "m: " << m << endl
//             << "n: " << n << endl
//             << "p: " << p << endl
//             << "q: " << q << endl;
//        char e = '0';
//    //    cout << "Creating Sudoku Puzzle" << endl;
//        SudokuPuzzle P (sudoku,m,n,p,q);
//        cout << "Initial Puzzle~\n";
//        P.display ();
//    //    cout << "Solving" << endl;
//        LogBundle B = S.solve(P,timeout,e);
//    //    cout << "Creating Logger" << endl;
//        Logger L(B);
//        L.logInfo(outputFileName);
//        cout << "Finished Puzzle~\n";
//        P.display ();
//        cout << "Output Info~\n";
//        L.printInfo();
//        cin.get();
//    }
    SudokuSolver S;
    for (std::size_t i = 0; i < (std::size_t)options.size(); ++i) {
        if (options[i] == "FC") {
            S.setFlag(HeuristicFlag::kFC,true);
        }
        else {

        }
    }

    SudokuReader R (inputFileName,outputFileName);
//    SudokuReader R(inputFiles[i],"");
    char** sudoku = R.loadSudoku(n,p,q,timeout);
//    cout << "Sudoku Puzzle: " << inputFiles[i] << " @ i=" << i << " with parameters:\n"
//             << "m: " << m << endl
    cout << "n: " << n << endl
         << "p: " << p << endl
         << "q: " << q << endl;
    char e = '0';
//    cout << "Creating Sudoku Puzzle" << endl;
    SudokuPuzzle P (sudoku,m,n,p,q);
    cout << "Initial Puzzle~\n";
    P.display ();
//    cout << "Solving" << endl;
    LogBundle B = S.solve(P,timeout,e);
//    cout << "Creating Logger" << endl;
    Logger L(B);
    L.logInfo(outputFileName);
    cout << "Finished Puzzle~\n";
    P.display ();
    cout << "Output Info~\n";
    L.printInfo();
    cin.get();
    return 0;
}
Beispiel #3
0
int main (int argc, char** argv)
{
  SudokuSolver solver;
  std::string infile;
  std::string outfile;
  int i = 1;
  int technique = -1;
  
  if (argc <= 2 || (argc == 2 && (strcmp (argv[1], "-h") == 0 || strcmp (argv[1], "--help") == 0)))
  {
    display_usage ();
    return 0;
  }
  else
  {
    while (i < argc)
    {
      if ((strcmp (argv[i], "-p") == 0 || strcmp (argv[i], "--processing") == 0))
      {
        solver.toggle_print_time (true);
      }
      else if ((strcmp (argv[i], "-d") == 0 || strcmp (argv[i], "--display") == 0))
      {
        solver.toggle_terminal_output (true);
      }
      else if ((strcmp (argv[i], "-f") == 0 || strcmp (argv[i], "--file") == 0))
      {
        if (i + 1 == argc)
        {
          std::cout << "Missing input filename" << std::endl;
          display_usage ();
          return 0;
        }
        infile = argv [i + 1];
        ++i;
      }
      else if ((strcmp (argv[i], "-o") == 0 || strcmp (argv[i], "--output") == 0))
      {
        if (i + 1 == argc)
        {
          std::cout << "Missing output filename" << std::endl;
          display_usage ();
          return 0;
        }
        outfile = argv [i + 1];
        ++i;
      }
      else if ((strcmp (argv[i], "-t") == 0 || strcmp (argv[i], "--technique") == 0))
      {
        if (i + 1 == argc)
        {
          std::cout << "Missing technique code" << std::endl;
          display_usage ();
          return 0;
        }
        technique = atoi (argv [i + 1]);
        ++i;
      }
      else
      {
        display_usage ();
        return 0;
      }
      ++i;
    }
  }

  if (infile.empty ())
  {
    display_usage ();
    return 0;
  }
  solver.init ();
  if (technique != -1)
  {
    solver.set_technique (technique);
  }
  if (!outfile.empty ())
  {
    solver.solve (infile, outfile);
  }
  else
  {
    solver.solve (infile);
  }
  return 0;
}
Beispiel #4
0
//Main Flow
//Grab arguments from command line
//Turn on options flags
//Generate sudoku from input file
//Save to output file
//Solve sudoku puzzle
//Generate log for solution
//If at any time the runtime is longer than timeout, then end for generation and for solving
int main(int argc, char *argv[]) {
    char* inputFiles[61] = {
        "input.txt",
        "PE1.txt",
        "PE2.txt",
        "PE3.txt",
        "PE4.txt",
        "PE5.txt",
        "PE6.txt",
        "PE7.txt",
        "PE8.txt",
        "PE9.txt",
        "PE10.txt",
        "PE11.txt",
        "PE12.txt",
        "PE13.txt",
        "PE14.txt",
        "PE15.txt",
        "PE16.txt",
        "PE17.txt",
        "PE18.txt",
        "PE19.txt",
        "PE20.txt",
        "PE21.txt",
        "PE22.txt",
        "PE23.txt",
        "PE24.txt",
        "PE25.txt",
        "PE26.txt",
        "PE27.txt",
        "PE28.txt",
        "PE29.txt",
        "PE30.txt",
        "PE31.txt",
        "PE32.txt",
        "PE33.txt",
        "PE34.txt",
        "PE35.txt",
        "PE36.txt",
        "PE37.txt",
        "PE38.txt",
        "PE39.txt",
        "PE40.txt",
        "PE41.txt",
        "PE42.txt",
        "PE43.txt",
        "PE44.txt",
        "PE45.txt",
        "PE46.txt",
        "PE47.txt",
        "PE48.txt",
        "PE49.txt",
        "PE50.txt",
        "PH1.txt",
        "PH2.txt",
        "PH3.txt",
        "PH4.txt",
        "PH5.txt",
        "PM1.txt",
        "PM2.txt",
        "PM3.txt",
        "PM4.txt",
        "PM5.txt"
    };
    int m = 0,n = 0,p = 0,q = 0;
    long timeout = 300;
    char* inputFileName = "input.txt";
    char* outputFileName = "output.txt";
//    char* inputFileName;
//    char* outputFileName;
    vector<char*> options;
    if (argc >= 4) {
        inputFileName = argv[1];
        outputFileName = argv[2];
        timeout = atol(argv[3]);
        int opNum = argc - 4;
        for (std::size_t i = 0; (std::size_t)i < (std::size_t)opNum; ++i) {
            options.insert(options.end(),argv[4 + i]);
        }
    }
    SudokuSolver S;
    for (std::size_t i = 0; i < (std::size_t)options.size(); ++i) {
		//cout << options[i] << endl;
		std::string option(options[i]);
		//cout << "Setting...";
        if (option == "FC"){
			//cout << "FC" << endl;
            S.setFlag(HeuristicFlag::kFC,true);
        }
        else if (option == "MRV"){
			//cout << "MRV" << endl;
            S.setFlag(HeuristicFlag::kMRV,true);
        }
        else if (option == "DH") {
			//cout << "DH" << endl;
            S.setFlag(HeuristicFlag::kDH,true);
        }
        else if (option == "LCV") {
			//cout << "LCV" << endl;
            S.setFlag(HeuristicFlag::kLCV,true);
        }
        else if (option == "MAC") {
			//cout << "MAC" << endl;
            S.setFlag(HeuristicFlag::kMAC,true);
        }
        else if (option == "ACP") {
			//cout << "ACP" << endl;
            S.setFlag(HeuristicFlag::kACP,true);
        }
        else {
			//cout << "None" << endl;
        }
    }
//    S.setFlag(HeuristicFlag::kMRV,true);
//    S.setFlag(HeuristicFlag::kDH,true);
//    S.setFlag(HeuristicFlag::kACP,true);
//    S.setFlag(HeuristicFlag::kLCV,true);
//    S.setFlag(HeuristicFlag::kMAC,true);
//    S.setFlag(HeuristicFlag::kFC,true);
    int stop = 1;
//    int stop = 61;
//    int stop = 5;
    for (int i = 0; i < stop; ++i) {
            SudokuReader R (inputFileName,outputFileName);
        //    SudokuReader R(inputFiles[i],"");
            char** sudoku = R.loadSudoku(n,p,q,timeout);
            cout << "Sudoku Puzzle: " << inputFiles[i] << " @ i=" << i << " with parameters:\n"
        //    cout
        //             << "m: " << m << endl
                 << "n: " << n << endl
                 << "p: " << p << endl
                 << "q: " << q << endl;
            char e = '0';
        //    cout << "Creating Sudoku Puzzle" << endl;
            SudokuPuzzle P (sudoku,m,n,p,q);
            cout << "Initial Puzzle~\n";
            P.display ();
        //    cout << "Solving" << endl;
            LogBundle B = S.solve(P,timeout,e);
        //    cout << "Creating Logger" << endl;
            Logger L(B);
            L.logInfo(outputFileName);
            cout << "Finished Puzzle~\n";
            P.display ();
            cout << "Output Info~\n";
            L.printInfo();
//            cin.get();
    }

    return 0;
}