Skip to content

Commit 6eebb19

Browse files
committed
Backtracking method
1 parent f3e26bc commit 6eebb19

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

BackTrackAlgo.m

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FilterAndOCR; %ImageProcess and fix bugs maunally
2+
SettingOutputMatrix; %Set a Matrix to be fed to SudokuSolver
3+
ANSWER=OUT; %ANSWER is answer matrix while OUT is question matrix
4+
[minimumarray,all_elements_possible]=MoreAccurateSpace(ANSWER); %Function finds all coordinates where least no. of elements can exist
5+
[x,ANSWER]= SolveSudoku(ANSWER,minimumarray,all_elements_possible);
6+
while x~=1
7+
[x,ANSWER]= SolveSudoku(ANSWER,minimumarray,all_elements_possible);
8+
end
9+
ANSWER

CheckForSUDOKUconsistency.m

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
function[flag]=CheckForSUDOKUconsistency(ANSWER)
2-
flag=0;
1+
%this function checks for row,column,3x3box consistency and the next
2+
%possible elements must not be zero. i.e. no further solution
3+
function[flag]=CheckForSUDOKUconsistency(ANSWER,all_possible_elements)
4+
flag=0;%flag 1 means inconsistent
35
for i=1:9
46
k=1;
57
l=1;
@@ -42,9 +44,18 @@
4244
clear elements;
4345
clear element_array;
4446
end
47+
s=size(all_possible_elements);
48+
for i=1:s(1)
49+
if all_possible_elements(i,3)==0
50+
flag=1;
51+
return
52+
end
53+
end
54+
4555
clear row;
4656
clear column;
4757
clear u_row;
4858
clear u_column;
4959
end
60+
clear all_possible_elements;
5061
end

SolveSudoku.m

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function [flag,ANSWER]=SolveSudoku(ANSWER,minimumarray,all_possible_elements)%minimumarray contains coordinates with lease possible candidates
2+
%all_possible_elements has all empty coordinates and possible candidates size
3+
flag2=0; %Flag2 to notify that a swap has been done in this function earlier
4+
if CheckForEmptyBox(ANSWER)==0 %if flag is 1 loop ends and answer is displayed
5+
flag=1;
6+
return
7+
end
8+
s3=size(minimumarray);
9+
for i=1:s3(1)
10+
if CheckForSUDOKUconsistency(ANSWER,all_possible_elements)==0 %true
11+
if flag2==1
12+
[minimumarray1,all_possible_elements1]=MoreAccurateSpace(ANSWER);%If it was already swapped from previous assigned value then no need to check for next coordinate
13+
else
14+
min_array=FindMinArray(minimumarray(i,1),minimumarray(i,2)); %Finds which smaller 3x3 array where element (x,y) belongs
15+
elements=CollectElements(min_array,ANSWER); %extract that 3x3array
16+
possible_elements=PossibleValues(elements,minimumarray(i,1),minimumarray(i,2),ANSWER);%finds XOR of all elements in row,column and 3x3 array with 1-9 characters
17+
ANSWER(minimumarray(i,1),minimumarray(i,2))=possible_elements(1,1);%amend ANSWER array with possible element or supposed possible element
18+
swappingtemp=possible_elements(1,1); %temporary variable
19+
[minimumarray1,all_possible_elements1]=MoreAccurateSpace(ANSWER);%calculate all parameters calculated above for recursion
20+
end
21+
if(SolveSudoku(ANSWER,minimumarray1,all_possible_elements1)==1)
22+
flag=1; %if recursion ends in flag=1 then return 1
23+
return
24+
end
25+
s2=size(possible_elements); %if recursion does not give 1 return then check if stepped out function had two elements or one
26+
if s2(2)==1
27+
flag=2; %if one then no swapping possible, simply back trace further till a two possible element arrives
28+
return
29+
else
30+
possible_elements=setxor(swappingtemp,possible_elements); %xor of swapppingtemp with possible_elements gives element not put in that box
31+
if numel(possible_elements)==0 %in case a swap was performed earlier xor output will have no elements
32+
flag=2; %back trace further
33+
return
34+
end
35+
ANSWER(minimumarray(i,1),minimumarray(i,2))=possible_elements; %change or swap element
36+
flag2=1; %flag a change
37+
end
38+
end
39+
end
40+
flag=0;
41+
return
42+
end

0 commit comments

Comments
 (0)