Example #1
0
int main(){
	Sudoku ss;
	ss.readIn();
	ss.transform();
	return 0;

}
Example #2
0
int main(){
    Sudoku *solvedPuzzle;
    //    int i = 0;
    //    int n = 0; //length
    //
    //
    std::string input;
    string t;
    for (int i = 0; i < 9; i++) {
        cin >> input;
        if (input.size()>10) {
            t = input;
            break;
        }
        t = t + input;
    }
    
    
    Sudoku puzzle = Sudoku(t);
    std::cout << "\n";
    std::cout << puzzle << endl;
    //    std::cout << "\n" << endl;
    puzzle.finalize();
    //    cout << "hi" << endl;            //test
    solvedPuzzle = solve (&puzzle);
    if (solvedPuzzle == NULL) {
        std:: cerr << "ERROR: nonSolvable puzzle" << endl;
    }
    else
        std::cout << *solvedPuzzle;
    return 0;
}
Example #3
0
bool Sudoku::solve()
{
  if(fin()){
    return true; 
  }
  while(1){
    Move m = findMove();
    if(m.val == 0){
      //no more moves
      return false;
    }
    Sudoku scopy = *this;
    bool result = scopy.applyMove(m);
    if(!result){
      cand[m.ii][m.jj][m.val] = false;
      continue;
    }
    result = scopy.solve();
    if(result){
      *this = scopy;
      return true;
    }else{
      cand[m.ii][m.jj][m.val] = false;
    }
  }
  return false;
}
Example #4
0
void RecursiveSolver::solve(Sudoku& sudoku) {
	this->_field = sudoku.getField();
	options = findMissing();
	
	for(map<int,vector<int>>::iterator it = options.begin(); it != options.end(); ++it) {
		cout << it->first << " " << ": ";
		for (vector<int>::iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2) {
			cout << *it2 << ", ";
		}
		cout << endl;
	}
	map<int,vector<int>>::iterator it = options.begin();
	count = 0;
	
	int status = -1;
	do {
		status = guess(it->first);
		switch(status) {
		case RETURN_NEXT:
			it = std::next(it);
			break;
		case RETURN_PREV:
			it = std::prev(it);
			break;
		}
	} while(status != RETURN_EXIT);
	
	
	sudoku.setField(_field);
}
int main(int argc, const char * argv[]) {
    
    Sudoku ss;
    ss.giveQuestion();
    
    return 0;
}
Example #6
0
// smazani policka probiha tak, ze se zkopiruje cele sudoku az na pole, ktere mazeme. je to nutne kvuli obnove zakazanyvh poli v radcich/sloupcich/ctvercich
int Sudoku::smazat ( int x, int y )
{
  int u,v, smazano = false;
  if(!s.pole[y*9+x].zapsane || s.pole[y*9+x].pevne) // pokud na pozici nic neni nebo je tam pevne cislo, nic se nedeje
    return false;
  Sudoku temp (-1); // prazdne sudoku pro doplnovani
  // projdeme vse
  for (int i = 0;i < 81; i++)
  {
    // vynechavame pri kopirovani mazane policko
    if(i/9 == y && i%9 == x)
    {
      smazano = true;
      continue;
    }
    // nezapisujeme prazdna policka, doslo by k zaseknuti
    if (!s.pole[i].zapsane)
      continue;
    // doplnime zpet policko
    temp.doplnM (i%9, i/9, s.pole[i].zapsane);
    temp.s.pole[i].pevne = s.pole[i].pevne;
  }
  s = temp.s; // preneseni stavu do tohoto sudoku z docasneho
  return smazano;
}
Example #7
0
int main()
{ 
	time_t start = time(NULL);
	
	std::string line;
	std::ifstream in;
	in.open("sudoku.txt");
	Sudoku x;
	int result = 0;
	for(int i = 0; i < 50; ++i)
	{
		getline(in,line);

		in >> x;
		Point p = x.getFirstEmpty();
		solve(x,p.i,p.j);
		
		std::cout << x << std::endl;
		result += getFirstThree(x);
	}

	std::cout << result << std::endl;
	printf("It took %d seconds\n", (time(NULL)-start));
	
}
Example #8
0
/**
 * Main method that gets initialized upon start of the program and creates an instance of the class Sudoku. A function
 * belonging to the Sudoku class is called that will generate a completely solved sudoku board and then the pointer to
 * the class is deleted to free up the allocated memory.
 */
int main() {
    Sudoku * mySudoku = new Sudoku();
    mySudoku->solve();
    delete mySudoku;

    return 0;
}
Example #9
0
constexpr Cell next(const Sudoku& s)
{
  for(size_t i=0; i<s.dimension(); ++i)
    for(size_t j=0; j<s.dimension(); ++j)
      if (s(i,j) == 0) return {i,j};
  return {std::numeric_limits<Cell::first_type>::max(),std::numeric_limits<Cell::second_type>::max()};
}
Example #10
0
int main()
{
	Sudoku ss;
	ss.ReadIn();
	ss.Solve();
	return 0;
}
Example #11
0
constexpr bool finished(const Sudoku& s)
{
  for(size_t i=0; i<s.dimension(); ++i)
    for(size_t j=0; j<s.dimension(); ++j)
      if (s(i,j) == 0) return false;
  return true;
}
Example #12
0
int main(){
	Sudoku ss;
	ss.readIn();
	ss.solve();

	return 0;
}
Example #13
0
vector<PuzzleState*> Sudoku::getSuccessors() {
  assert(!isSolution()); // some move remains!

  vector<PuzzleState*> result;

  // find a blank square to fill in
  int row = 0;
  int col = 0;
  for (int i=0; i<9; i++) {
    for (int j=0; j<9; j++) {
      if (grid[i][j]==0) {
	row = i;
	col = j;
	// terminate the loop
	// no labelled breaks :(
	i = 9;
	j = 9;
      }
    }
  }

  for (int digit=1; digit<=9; digit++) {
    if (rowTally[row][digit]) continue; // digit already used in row
    if (colTally[col][digit]) continue; // digit already used in column
    if (zoneTally[row/3][col/3][digit]) continue; // digit already used in zone
    // This is a legal digit!  Add it to possible moves.
    Sudoku *temp = new Sudoku(*this);
    temp->applyMove(row,col,digit);
    result.push_back(temp);
  }

  return result;
}
Example #14
0
int main()
{
	Sudoku ss;
	int num;
	int i,j,k;
	int count = 0;
	while(cin >> num)
	{
		count++;
		if(count!=1 && ans_count != 0)
			cout << endl << endl;
		else if(count !=1)
			cout << endl;
		for(i=0; i<9; i++)
		{
			for(j=0; j<9; j++)
			{
				su[i][j] = 0;
				su3[i][j][0] = 0;
				ans[i][j] = 0;
				su2[i][j] = 0;
				for(k=1; k<10; k++)
				{
					su3[i][j][k] = 1;
				}
			}
		}
		have_zero = false;
		ans_count = 0;
		ss.readIn(num);
		ss.solve(num);
	}
}
Example #15
0
void sudokuTest() {
  vector< vector<int> > _grid = 
  {{3, 0, 6, 5, 0, 8, 4, 0, 0},
    {5, 2, 0, 0, 0, 0, 0, 0, 0},
    {0, 8, 7, 0, 0, 0, 0, 3, 1},
    {0, 0, 3, 0, 1, 0, 0, 8, 0},
    {9, 0, 0, 8, 6, 3, 0, 0, 5},
    {0, 5, 0, 0, 9, 0, 6, 0, 0},
    {1, 3, 0, 0, 0, 0, 2, 5, 0},
    {0, 0, 0, 0, 0, 0, 0, 7, 4},
    {0, 0, 5, 2, 0, 6, 3, 0, 0}};

  Sudoku* puzzle = new Sudoku( _grid );
  if(!puzzle->IsLegalBoard()) {
    drunkout("Illegal board.", cERROR);
  }
  puzzle->print();

  if(puzzle->solve())
    puzzle->print();
  else
    drunkout("No solution.", cINFO);

  delete puzzle;

}
Example #16
0
void Sudoku::findSudokuSolution(int sp){
    if(fixedcount == 81 ){
       /* for(int i = 0; i < 9; i++){
            for(int j = 0; j < 9;j++)
                cout<<cell[i * 9 + j].num<<" ";
        cout<<endl;
        }*/
    
        if(++answer < 2)
            for(int i = 0; i < 81; i++)
                ans1[i] = cell[i].num;
        return;
    }

    while(cell[sp].fixed == true){
        sp++;
        if(sp >= 81)
            return findSudokuSolution(sp);

    }
    Sudoku newState;
    set<int>::iterator it = cell[sp].candidators.begin();
    while(it != cell[sp].candidators.end()){
        newState = *this;
        if(newState.setCandidatorTofixed(sp, *it))    
            newState.findSudokuSolution(sp + 1);
        ++it;
        if(answer == 2)
            return;
    }
}
Example #17
0
bool XyzWing(Sudoku &sudoku)
{
    Log(Trace, "searching for xyz-wings\n");

    for (Index_t i = 0; i < 9; ++i) {
        for (Index_t j = 0; j < 9; ++j) {
            if (sudoku.GetCell(i, j).NumCandidates() != 3)
                continue;

            boost::array<Position, NUM_BUDDIES> buddies =
                sudoku.GetBuddies(i, j);

            boost::array<Position, NUM_BUDDIES>::const_iterator it0, it1;
            for (it0 = buddies.begin(); it0 != buddies.end(); ++it0) {
                if (sudoku.GetCell(*it0).NumCandidates() != 2)
                    continue;
                for (it1 = it0 + 1; it1 < buddies.end(); ++it1) {
                    if (XyzWingForCells(sudoku, i, j, it0->row, it0->col,
                                it1->row, it1->col)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Example #18
0
main(int argc, char **argv)
{
  string solve, output;
  Sudoku *sud;

  if (argc != 3) usage("");
  solve = argv[1];
  output = argv[2];
  if (solve != "yes" && solve != "no") usage("bad solve");
  if (output != "screen" && output != "convert") usage("bad output");

  sud = new Sudoku;
  if (solve == "yes") {
    if (!sud->Solve()) {
      printf("Cannot solve puzzle\n");
      exit(0);
    }
  }

  if (output == "screen") {
    sud->Print_Screen();
  } else {
    sud->Print_Convert();
  }
  exit(0);
}
Example #19
0
void Solver::guess(int index) { //Where slot=0-80
	Sudoku s = Sudoku();
	s.setField(_field);
	s.print();
	usleep(300);
	bool succes = false;
	for (int option : missing[index]) {
		if (_field.get(index) >= option) { // We have tried this slot before
			continue;
		}
		if (_field.fit(index, option)) {
			_field.set(index, option);
			succes = true;
			break;
		}
	}
	if (succes) {
		if (_field.countEmpty(true) > 0) {
			// Find the the next empty slot in the vector
			int curIndex = std::distance(emptySlots.begin(), std::find(emptySlots.begin(), emptySlots.end(), index));
			guess(emptySlots.at(curIndex + 1));
		} else {
			return;
		}
	} else {
		_field.set(index, 0);
		// Find the the previous tried slot in the vector
		int curIndex = std::distance(emptySlots.begin(), std::find(emptySlots.begin(), emptySlots.end(), index));
		guess(emptySlots.at(curIndex - 1));
	}
}
Example #20
0
		/********************************************
 		* main method to begin program 
 		*********************************************/
		int main(){
			Sudoku s;
			//s.backtrack();	
			s.printBoard();

			return 1;
		}
Example #21
0
int main()
{
	Sudoku sudoku;
	sudoku.readIn();
	sudoku.solve();
	
	return 0;
}
Example #22
0
int main()
{
	Sudoku sdk;
	sdk.readIn();
	sdk.solve();
	system("PAUSE");
	return 0;
}
Example #23
0
int main(int argc, const char * argv[]) {
    
    Sudoku ss;
    ss.readIn();
    ss.solve();
    
    return 0;
}
Example #24
0
void init(Sudoku &s) {
	int i;
	
	s.clear();
	
	for(i = 0; i < 9; i++)
		s.push_back({0, 0, 0, 0, 0, 0, 0, 0, 0});
}
Example #25
0
/**
* This program can solve sudokus of a normal size (9x9). The solved sudoku is shown on the console.\n
* The program has to be started this way:\n
*		./sudoku <mode> <filename/order>\n
*		- mode: Enter '-g' to generate an explicetely solveable sudoku. If you want to read a sudoku from a file and then solve
*		it, enter '-s'.
*		- filename/order: If you want to generate, enter a number between 24 and 35. This number determines how many numbers there
*		are already filled in when generating the sudoku is finished. If you want to read a sudoku from a file, enter a filename "*.txt".
* @param argc The amount of parameters handed over to the program.
* @param argv[][] A two-dimensional char-array, that holds the parameters.
*/
int main(int argc, char* argv[])
{
	string error = "Please start the program the following way:\n"
		"./sudoku <mode> <filename/order>\n"
		"- mode: Enter '-g' to generate an explicetely solveable sudoku. If you want to read a sudoku from a file and then solve"
		" it, enter '-s'.\n"
		"- filename/order: If you want to generate, enter a number between 24 and 35. This number determines how many numbers there"
		" are already filled in when generating the sudoku is finished. If you want to read a sudoku from a file, enter a filename '*.txt'.";
	string first_parameter = "";
	string second_parameter = "";

	if (check_parameters(argc, argv))
	{
		first_parameter = argv[1];
		second_parameter = argv[2];

		if (first_parameter.compare("-g") == 0)
		{
			Sudoku sudoku = Sudoku(strtol(second_parameter.c_str(), NULL, 0));
			cout << "\n";
			sudoku.show_sudoku();
		}
		else
		{
			Sudoku sudoku = Sudoku(second_parameter);
			sudoku.show_sudoku();

			/*int test_array[9][9] = { 
				{9, 0, 3, 0, 5, 0, 6, 0, 0}, 
				{0, 8, 0, 1, 0, 0, 0, 0, 0},
				{0, 0, 2, 0, 0, 0, 0, 8, 0},
				{0, 1, 0, 0, 0, 9, 0, 2, 4},
				{0, 0, 0, 0, 0, 0, 9, 0, 0},
				{0, 4, 0, 3, 0, 0, 0, 0, 7},
				{0, 0, 0, 0, 0, 0, 4, 5, 9},
				{0, 0, 0, 7, 0, 0, 0, 1, 0},
				{0, 6, 0, 0, 8, 0, 0, 0, 0}
			};
			vector<int> initializing_vector;
			initializing_vector.assign(9, 0);
			vector< vector<int> > test;
			test.assign(9, initializing_vector);
			for (unsigned int i = 0; i < 9; ++i)
				for (unsigned int j = 0; j < 9; ++j)
					test[i][j] = test_array[i][j];

			Sudoku sudoku = Sudoku(test);
			sudoku.show_sudoku();*/
		}
	}
	else
		cerr << error;

	getchar();

	return 0;
}
Example #26
0
Sudoku* solve (Sudoku* puzzle) {
    Sudoku* track;
    // puzzle->finalize();
    puzzle->horSolve();
    puzzle->verSolve();
    puzzle->boxSolve();
    int result = puzzle->finalize();
    //    cout << "fault?" << endl;   // test
    //    cout << result << endl;   // test
    
    if (result == -1) {
        //        cout << "" << endl;
        //        cerr << "ERROR" << endl;
        return NULL;
    }
    else if (puzzle->isSolved() == 1) {
        // cout << "111111111"<< endl;
        // cout << "111address :" << puzzle << endl;
        return puzzle;
    }
    else if (result == 0) {
        //        cout << "runhere?" << endl;   // run here test
        //        Sudoku* newpuzzle = puzzle->guess();
        Sudoku * newpuzzle = puzzle->guess();
        //        cout << "run here? newpuzzle" << endl;   // test
        //        cout << *newpuzzle << endl;     // test newpuzzle;
        track = solve(newpuzzle);
        
        //cout << newpuzzle<< endl;
        // solve (newpuzzle);   // re
        if (track == NULL) {
            //            cout << "track" << endl;   // test
            // cout << *track << endl;    // test
            //delete track;
            //            cout << "wronguess and solve the orginal puzzle" << endl;
            //            cout << *puzzle << endl;
            return solve(puzzle);
        }
        else if (track->isSolved()) {
            // cout << "444address: " << track << endl;
            return track;
        }
        else {
            //            cout << "333address: " << newpuzzle << endl;
            return solve (newpuzzle);
        }
        //   cout << "33333333" <<endl;
        //  cout << "333address: " << newpuzzle << endl;
        //   return newpuzzle;
        //cout << "caonima" << endl;
    }
    else {
        // cout << "4444444444" << endl;
        return solve(puzzle);   //re
    }
}
Example #27
0
int
main(int /*argc*/, char** /*argv*/)
{
	srand(system_time() % INT_MAX);

	Sudoku sudoku;
	sudoku.Run();

	return 0;
}
Example #28
0
constexpr bool rowValid(const Sudoku& s, size_t row, size_t pos = 0)
{
  if (pos > s.dimension()) return true;
  else if (s(row,pos) == 0) return rowValid(s,row,pos+1);
  else {
    for(size_t i=pos+1;i<s.dimension();++i)
      if (s(row,i) == s(row,pos)) return false;
    return rowValid(s,row,pos+1);
  }
}
Example #29
0
constexpr bool quadrantValid(const Sudoku& s, size_t x, size_t y, size_t pos = 0)
{
  if (pos > s.dimension()) return true;
  else if (qVal(s, x,y,pos) == 0) return quadrantValid(s, x, y, pos+1);
  else {
    for(size_t i=pos+1;i<s.dimension();++i)
      if (qVal(s, x,y,i) == qVal(s, x,y,pos)) return false;
    return quadrantValid(s,x,y,pos+1);
  }
}
Example #30
0
constexpr bool colValid(const Sudoku& s, size_t col, size_t pos = 0)
{
  if (pos > s.dimension()) return true;
  else if (s(pos,col) == 0) return colValid(s,col,pos+1);
  else {
    for(size_t i=pos+1;i<s.dimension();++i)
      if (s(i,col) == s(pos,col)) return false;
    return colValid(s,col,pos+1);
  }
}