Sudoku Solving algorithm

Hi guys! I'm trying to make a Sudoku Solving program for a couple of days but I'm stuck with the steps. I google an algorithm which can help me. I found this algorithm:
1. start at the first empty cell, and put 1 in it.
2. Check the entire board, and see if there are any conflicts
3. If there are coflicts on the board, increase the number in the current cell by 1 (so change 1 to 2, 2 to 3, etc)
4. If the board is clean move, start at step one again.
5. If all nine possible numbers on a given cell cause a conflict in the board, then you set this cell back to empty, go back to the previous cell, and start again from step 3 (this is where the 'backtracking' comes in).


But maybe I don't understand it well. Can someone explain this to me? Maybe you would want to see my code. Here it is. My function for checking for conflicts is not ready yet, but I can see that there is a mistake in my solving function but I don't understand what it is :/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
    #include <iostream>
    #include <iomanip>
    #include <time.h>
    #include <cstdlib>
    #include <windows.h>
    using namespace std;
    
    class Sudoku
      {
        private:
        int board[9][9];
        int change[9][9];
        public:
        Sudoku();
        void Print_Board();  
        void Add_First_Cord();  
        void Solve();
        void Help_Solve(int i, int j);
        bool Check_Conflicts(int p, int i, int j);
      };
      
    Sudoku Game;  
      
    void setcolor(unsigned short color)                 //The function that you'll use to
    {                                                   //set the colour
        HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hcon,color);
    }
      
    Sudoku::Sudoku()
      {
        for(int i = 1; i <= 9; i++)
          for(int j = 1; j <= 9; j++)
            board[i][j] = 0;            
      }
      
    void Sudoku::Print_Board()
      {
        for(int i = 1; i <= 9; i++)
          {
            for(int j = 1; j <= 9; j++)
              {
                if(change[i][j] == 1) 
                  {
                    setcolor(12);
                    cout << board[i][j] << " ";
                    setcolor(7);           
                  }
                  else cout << board[i][j] << " ";  
                  if(j%3 == 0) cout << "| ";
              }
            cout << endl;
            if(i%3 == 0) cout << "------+-------+---------" << endl;
                    
          }                    
      }
      
    void Sudoku::Add_First_Cord()
      {
        board[1][1] = 5; change[1][1] = 1;
        board[1][2] = 3; change[1][2] = 1;     
        board[1][5] = 7; change[1][5] = 1;      
        board[2][1] = 6; change[2][1] = 1;  
        board[2][4] = 1; change[2][4] = 1;       
        board[2][5] = 9; change[2][5] = 1;  
        board[2][6] = 5; change[2][6] = 1; 
        board[3][2] = 9; change[3][2] = 1;      
        board[3][3] = 8; change[3][3] = 1;   
        board[3][8] = 6; change[3][8] = 1;     
        board[4][1] = 8; change[4][1] = 1;    
        board[4][5] = 6; change[4][5] = 1;    
        board[4][9] = 3; change[4][9] = 1;    
        board[5][1] = 4; change[5][1] = 1; 
        board[5][4] = 8; change[5][4] = 1;  
        board[5][6] = 3; change[5][6] = 1;  
        board[5][9] = 1; change[5][9] = 1;   
        board[6][1] = 7; change[6][1] = 1; 
        board[6][5] = 2; change[6][5] = 1;   
        board[6][9] = 6; change[6][9] = 1;  
        board[7][2] = 6; change[7][2] = 1;  
        board[7][7] = 2; change[7][7] = 1;  
        board[7][8] = 8; change[7][8] = 1;  
        board[8][4] = 4; change[8][4] = 1; 
        board[8][5] = 1; change[8][5] = 1;   
        board[8][6] = 9; change[8][6] = 1; 
        board[8][9] = 5; change[8][9] = 1;   
        board[9][5] = 8; change[9][5] = 1;  
        board[9][8] = 7; change[9][8] = 1;  
        board[9][9] = 9; change[9][9] = 1;  
      }
    
    bool Sudoku::Check_Conflicts(int p, int i, int j)
      {
        for(int k = 1; k <= 9; k++)
          if(board[i][k] == p) return false;
          
        for(int q = 1; q <= 9; q++)
          if(board[q][j] == p) return false;
        
        /*
          *00
          000
          000
        */
        if((j == 1 || j == 4 || j == 7) && (i == 1 || i == 4 || i == 7))
          {
             if(board[i][j+1] == p || board[i][j+2] == p || board[i+1][j] == p || 
                 board[i+2][j] == p || board[i+1][j+1] == p || board[i+1][j+2] == p || 
                     board[i+2][j+1] == p || board[i+2][j+2] == p)return false;     
          } 
        
        
        /*
          000
          000
          *00
        */  
        if((j == 1 || j == 4 || j == 7) && (i == 3 || i == 6 || i == 9))
          {
             if(board[i-1][j] == p || board[i-2][j] == p || board[i][j+1] == p || 
                 board[i][j+2] == p || board[i-1][j+1] == p || board[i-1][j+2] == p || 
                     board[i-2][j+1] == p || board[i-2][j+2] == p)return false;   
          }
         
        /*
          000
          *00
          000
        */            
        if((j == 1 || j == 4 || j == 7) && (i == 2 || i == 5 || i == 8))
          {
             if(board[i-1][j] == p || board[i+1][j] == p || board[i-1][j+1] == p || 
                 board[i][j+1] == p || board[i+1][j+1] == p || board[i+1][j+2] == p || 
                     board[i][j+2] == p || board[i+1][j+2] == p)return false;  
          } 
          
        
        /*
          0*0
          000
          000
        */            
        if((j == 2 || j == 5 || j == 8) && (i == 1 || i == 5 || i == 7))
          {
             if(board[i-1][j] == p || board[i+1][j] == p || board[i-1][j+1] == p || 
                 board[i][j+1] == p || board[i+1][j+1] == p || board[i+1][j+2] == p || 
                     board[i][j+2] == p || board[i+1][j+2] == p)return false;  
          }
          
        /*
          000
          0*0
          000
        */            
        if((j == 2 || j == 5 || j == 8) && (i == 2 || i == 5 || i == 8))
          {
             if(board[i-1][j] == p || board[i-1][j-1] == p || board[i-1][j+1] == p || 
                 board[i][j+1] == p || board[i][j-1] == p || board[i+1][j+1] == p || 
                     board[i][j] == p || board[i+1][j-1] == p)return false;  
          }
          
        
        /*
          000
          000
          0*0
        */            
        if((j == 2 || j == 5 || j == 8) && (i == 3 || i == 6 || i == 9))
          {
             if(board[i][j-1] == p || board[i][j+1] == p || board[i-1][j] == p || 
                 board[i-1][j+1] == p || board[i-1][j-1] == p || board[i-2][j] == p || 
                     board[i-1][j+1] == p || board[i-2][j-1] == p) return false;  
          }        
        
        return true;                          
      }
     
    void Sudoku::Help_Solve(int i, int j)
      {
        if(j <= 0) 
          {
            i = i-1;
            j = 9;
          }
        if(change[i][j] == 1) return Game.Help_Solve(i, j-1);
        for(int p = 1; p <= 9; p++)
          if(Game.Check_Conflicts(p, i, j)) 
            {
              board[i][j] = p;
              return;
            }
        return Game.Help_Solve(i, j-1);
          
      }
      
    void Sudoku::Solve()
      {                          
          for(int i = 1; i <= 9; i++)
            {
              for(int j = 1; j <= 9; j++)
                {
                  if(board[i][j] == 0 && change[i][j] == 0)
                    {
                      Game.Help_Solve(i, j);           
                    }      
                }      
            }
                          
      }
    
    
    int main()
    {
      Game.Add_First_Cord();
      Game.Solve();
      Game.Print_Board();  
        
      system("pause");
      return 0;
    }
Here is the full Check_Conflicts(...) function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
bool Sudoku::Check_Conflicts(int p, int i, int j)
      {
        for(int k = 1; k <= 9; k++)
          if(board[i][k] == p) return false;
          
        for(int q = 1; q <= 9; q++)
          if(board[q][j] == p) return false;
        
        /*
          *00
          000
          000
        */
        if((j == 1 || j == 4 || j == 7) && (i == 1 || i == 4 || i == 7))
          {
             if(board[i][j+1] == p || board[i][j+2] == p || board[i+1][j] == p || 
                 board[i+2][j] == p || board[i+1][j+1] == p || board[i+1][j+2] == p || 
                     board[i+2][j+1] == p || board[i+2][j+2] == p)return false;     
          } 
        
        
        /*
          000
          000
          *00
        */  
        if((j == 1 || j == 4 || j == 7) && (i == 3 || i == 6 || i == 9))
          {
             if(board[i-1][j] == p || board[i-2][j] == p || board[i][j+1] == p || 
                 board[i][j+2] == p || board[i-1][j+1] == p || board[i-1][j+2] == p || 
                     board[i-2][j+1] == p || board[i-2][j+2] == p)return false;   
          }
         
        /*
          000
          *00
          000
        */            
        if((j == 1 || j == 4 || j == 7) && (i == 2 || i == 5 || i == 8))
          {
             if(board[i-1][j] == p || board[i+1][j] == p || board[i-1][j+1] == p || 
                 board[i][j+1] == p || board[i+1][j+1] == p || board[i+1][j+2] == p || 
                     board[i][j+2] == p || board[i+1][j+2] == p)return false;  
          } 
          
        
        /*
          0*0
          000
          000
        */            
        if((j == 2 || j == 5 || j == 8) && (i == 1 || i == 5 || i == 7))
          {
             if(board[i-1][j] == p || board[i+1][j] == p || board[i-1][j+1] == p || 
                 board[i][j+1] == p || board[i+1][j+1] == p || board[i+1][j+2] == p || 
                     board[i][j+2] == p || board[i+1][j+2] == p)return false;  
          }
          
        /*
          000
          0*0
          000
        */            
        if((j == 2 || j == 5 || j == 8) && (i == 2 || i == 5 || i == 8))
          {
             if(board[i-1][j] == p || board[i-1][j-1] == p || board[i-1][j+1] == p || 
                 board[i][j+1] == p || board[i][j-1] == p || board[i+1][j+1] == p || 
                     board[i][j] == p || board[i+1][j-1] == p)return false;  
          }
          
        
        /*
          000
          000
          0*0
        */            
        if((j == 2 || j == 5 || j == 8) && (i == 3 || i == 6 || i == 9))
          {
             if(board[i][j-1] == p || board[i][j+1] == p || board[i-1][j] == p || 
                 board[i-1][j+1] == p || board[i-1][j-1] == p || board[i-2][j] == p || 
                     board[i-1][j+1] == p || board[i-2][j-1] == p) return false;  
          }  
          
        /*
          00*
          000
          000
        */            
        if((j == 3 || j == 6 || j == 9) && (i == 1 || i == 4 || i == 7))
          {
             if(board[i][j-1] == p || board[i][j-2] == p || board[i+1][j] == p || 
                 board[i+1][j-1] == p || board[i+1][j-2] == p || board[i+2][j] == p || 
                     board[i+2][j-1] == p || board[i+2][j-2] == p) return false;  
          } 
          
        /*
          000
          00*
          000
        */            
        if((j == 3 || j == 6 || j == 9) && (i == 2 || i == 5 || i == 8))
          {
             if(board[i-1][j] == p || board[i-1][j-1] == p || board[i-1][j-2] == p || 
                 board[i][j-1] == p || board[i][j-2] == p || board[i+1][j] == p || 
                     board[i+1][j-1] == p || board[i+1][j-2] == p) return false;  
          }
          
        /*
          000
          000
          00*
        */            
        if((j == 3 || j == 6 || j == 9) && (i == 3 || i == 6 || i == 9))
          {
             if(board[i][j-1] == p || board[i][j-1] == p || board[i-1][j] == p || 
                 board[i-1][j-1] == p || board[i-1][j-2] == p || board[i-2][j] == p || 
                     board[i-2][j-1] == p || board[i-2][j-2] == p) return false;  
          }      
        
        return true;                          
      }
One thing to remember is that valid indices for arrays are 0 to size-1. So for a two dimensional array where the size of each dimension is 9, the maximum index for each dimension is 8.

If you look through your code, no element of change is modified anywhere under any condition (aside from the initial assignment when you set all elements to 1.) I don't think it's really suited well to help with your backtracking, anyway. You might consider a stack of the board positions you've changed.
Topic archived. No new replies allowed.