Sudoku solver

This is a discussion on Sudoku solver within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm asked to make a program that will solved a 9 X 9 sudoku using backtracking. I am having ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    28

    Sudoku solver

    Hello, I'm asked to make a program that will solved a 9 X 9 sudoku using backtracking.
    I am having a problem in backtracking. It backtracks but it do not know if it already tested a number so it creates an infinite loop. I do not know what to do.
    Please help me.

    Code:
    //===============================================================================//
    void backtrack(int a, int b, int value)
    {
        bool check = true;
        if(sudoku2[a][b] == 0 ) // This will check the contents of the array "sudoku2" and change the ones with the value of zero
        {
            check = true;
            while(check == true && value < 9)
            {
                value++;
                sudoku2[a][b] = value; // assigns a value to sudoku2[a][b]
                if((check_row(a, b)) && (check_col(a, b)) && (check_box(a, b))) // if there is no conflict with the value. the previous while and if statements will terminate and moves to the next one.
                {
                    cout << "lol3";
                    check = false;
                }
                else // If the value is not compatible, it will try the next value
                {
                    if(value > 8)// If there is no value from 1 to 9 that is compatible, backtrack
                    {
                        sudoku2[a][b] = 0;
                        do
                        {
                            if(b > 0)
                            {
                                b = b - 1;
                            }
                            else
                            {
                                a = a - 1;
                                b = 0;
                            }
                        }while(sudoku[a][b] != 0);
                        value = sudoku2[a][b];
                        cout << value;
                        sudoku2[a][b] = 0;
                        backtrack(a, b, value);
                    }
                }
            }
        }
        if(b < 8)
        {
            backtrack(a, b + 1, 0);
        }
        else
        {
            backtrack(a + 1, 0, 0);
        }
    }
    
    
    //===============================================================================//
    Last edited by renz15; 03-07-2012 at 04:40 AM.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Can you outline the algorithm you're trying to use? Also what are the starting conditions with which you call backtrack()?
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with sudoku solver
    By kn1ghtmares in forum C Programming
    Replies: 6
    Last Post: 01-18-2012, 01:20 AM
  2. sudoku solver
    By kn1ghtmares in forum C Programming
    Replies: 7
    Last Post: 01-16-2012, 11:09 PM
  3. Please help me with my sudoku solver
    By Tharps in forum Game Programming
    Replies: 6
    Last Post: 12-17-2011, 10:45 PM
  4. Sudoku solver
    By M-S-H in forum C Programming
    Replies: 10
    Last Post: 12-15-2009, 02:26 PM
  5. Help for a sudoku Solver
    By axilleask in forum C Programming
    Replies: 3
    Last Post: 11-26-2007, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21