My sudoku backtrack algorithm isn't working after the 7th line. What am I doing wrong?

Viewed 60

My code runs but when I try to print the board using a function and when I check if the algorithm had aleast solved the board, it only solves up to the 7th line then stops.

In my process to see what I was doing wrong I printed my board right after the if statement that includes checkforzero in the last function that is initialized. That is how I found out that my code only solves until the seventh line of the puzzle. It also confuses me on how even through I reference the sodoku_board vector it still doesn't output a value of the changed 7 lines after solving it through the algorithm in the main function.

#include<iostream>
#include<vector>
#include<algorithm>

void print_board(std::vector<std::vector<int>> sudoku_board);
std::vector<std::vector<int>> missing_nums(std::vector<std::vector<int>> sudoku_board);
bool check_viable(std::vector<std::vector<int>> sudoku_board, int row, int column, int number);
bool checkforzero(std::vector<std::vector<int>> sudoku_board, int &row, int &column);
bool solve_board(std::vector<std::vector<int>> &sudoku_board, std::vector<std::vector<int>> missing);

int main(){
    std::vector<std::vector<int>> missing;
    
    //created board
    std::vector<std::vector<int>> sudoku_board = {
        {1, 0, 8, 0, 0, 5, 0, 0, 6},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {5, 0, 3, 8, 2, 0, 7, 0, 0},
        {2, 0, 0, 1, 5, 0, 9, 0, 8},
        {0, 0, 0, 0, 8, 0, 0, 0, 0},
        {8, 0, 9, 0, 4, 2, 0, 0, 5},
        {0, 0, 5, 0, 9, 8, 2, 0, 4},
        {0, 0, 0, 0, 0, 0, 0, 0, 0},
        {9, 0, 0, 2, 0, 0, 6, 0, 1}
    };

    print_board(sudoku_board);
    missing = missing_nums(sudoku_board);
    bool end = solve_board(sudoku_board, missing);
    print_board(sudoku_board);
}
//simply printing the board inputed
void print_board(std::vector<std::vector<int>> sudoku_board){
   std::cout<<"\n";
    for(auto c : sudoku_board){
      for(auto d : c){
         std::cout<<d<<" ";
      }
      std::cout<<"\n";
   }
    std::cout<<"\n";
}
//find the missing nums in each row
std::vector<std::vector<int>> missing_nums(std::vector<std::vector<int>> sudoku_board){
  std::vector<std::vector<int>> r(9);
  for(int c = 0; c < 9; c++)
     for(int i = 1; i <= 9; i++)
        if(find(sudoku_board[c].begin(), sudoku_board[c].end(), i) == sudoku_board[c].end())
                    r[c].push_back(i);
  return r;
}
//checks if the number can be inputed on board
bool check_viable(std::vector<std::vector<int>> sudoku_board, int row, int column, int number){
    int row_change;
    //checks vertical
    for(int rowed : sudoku_board[row])if(rowed == number)return false;
    //checks horizontal
    for(int i = 0; i < sudoku_board.size(); i++)if(sudoku_board[i][column] == number)return false;
    //checks box
    while(row != 0 && row != 3 && row != 6)row--;
    while(column != 0 && column != 3 && column != 6)column--;
    for(int v = column; v <= column + 2; v++)if(sudoku_board[row+1][v] == number || sudoku_board[row+2][v] == number || sudoku_board[row][v] == number)return false;
    return true;
}

bool checkforzero(std::vector<std::vector<int>> sudoku_board, int &row, int &column){
    for(int i = 0; i < 9; i++){
        for(int v = 0; v < 9; v++){
            if(sudoku_board[i][v] == 0){
                row = i;
                column = v;
                return true;
            }
        }
    }
    return false;
}

bool solve_board(std::vector<std::vector<int>> &sudoku_board, std::vector<std::vector<int>> missing){
    int row, column;
    if(!checkforzero(sudoku_board, row, column))
        return true;
    print_board(sudoku_board);
    for(int d = 0; d < missing[row].size(); d++){
        if(check_viable(sudoku_board, row, column, missing[row][d])){
            sudoku_board[row][column] = missing[row][d];
            if(solve_board(sudoku_board, missing))
                return true;
            sudoku_board[row][column] = 0;
        }
    }
    return false;
}
0 Answers
Related