Recursive backtracking with cell arrays?
I’m working on a function to recursively generate solutions to a given sudoku puzzle represented by a 9×9 array with NaN representing blank spaces utilizing backtracking.
function cellSolutions = solveSudoku(PuzzleA)
cellSolutions = {};
if ~reject(PuzzleA)
if accept(PuzzleA)
disp(‘solution found’)
cellSolutions{end+1} = PuzzleA;
end
[nP, Idx] = firstChild(PuzzleA);
while (~isempty(nP))
possibleSolution = solveSudoku(nP);
if (~isempty(possibleSolution))
cellSolutions{end+1} = possibleSolution{1};
end
nP = nextChild(nP,Idx);
end
end
end
The return cellSolutions is a cell array that contains 0, 1, or more solutions depending on how many exist. The trouble I’m having is actually getting the solutions that are found to appear in the cellSolution returned with the initial call. What I’m hoping for is:
cellSolutions
{[9×9 double] [9×9 double]} or {} or {[9×9 double]}
I can indeed confirm that one or more solutions are found with the disp call, but I’m not sure how to manipulate the cell array. I’m pretty sure the problem lies with:
while (~isempty(nP))
possibleSolution = solveSudoku(nP);
if (~isempty(possibleSolution))
cellSolutions{end+1} = possibleSolution{1};
end
nP = nextChild(nP,Idx);
end
Some other info: reject determines whether the current puzzle satisfies the rules of sudoku; accept checks additionally that there are no empty spaces; firstChild finds and instance of NaN in a given sudoku puzzle, and changes it to 1 and returns linear index it changed and the new puzzle; nextChild takes a sudoku puzzle and index, if the value at that index is not 9, then it increments the value and returns the new puzzle. Thanks!I’m working on a function to recursively generate solutions to a given sudoku puzzle represented by a 9×9 array with NaN representing blank spaces utilizing backtracking.
function cellSolutions = solveSudoku(PuzzleA)
cellSolutions = {};
if ~reject(PuzzleA)
if accept(PuzzleA)
disp(‘solution found’)
cellSolutions{end+1} = PuzzleA;
end
[nP, Idx] = firstChild(PuzzleA);
while (~isempty(nP))
possibleSolution = solveSudoku(nP);
if (~isempty(possibleSolution))
cellSolutions{end+1} = possibleSolution{1};
end
nP = nextChild(nP,Idx);
end
end
end
The return cellSolutions is a cell array that contains 0, 1, or more solutions depending on how many exist. The trouble I’m having is actually getting the solutions that are found to appear in the cellSolution returned with the initial call. What I’m hoping for is:
cellSolutions
{[9×9 double] [9×9 double]} or {} or {[9×9 double]}
I can indeed confirm that one or more solutions are found with the disp call, but I’m not sure how to manipulate the cell array. I’m pretty sure the problem lies with:
while (~isempty(nP))
possibleSolution = solveSudoku(nP);
if (~isempty(possibleSolution))
cellSolutions{end+1} = possibleSolution{1};
end
nP = nextChild(nP,Idx);
end
Some other info: reject determines whether the current puzzle satisfies the rules of sudoku; accept checks additionally that there are no empty spaces; firstChild finds and instance of NaN in a given sudoku puzzle, and changes it to 1 and returns linear index it changed and the new puzzle; nextChild takes a sudoku puzzle and index, if the value at that index is not 9, then it increments the value and returns the new puzzle. Thanks! I’m working on a function to recursively generate solutions to a given sudoku puzzle represented by a 9×9 array with NaN representing blank spaces utilizing backtracking.
function cellSolutions = solveSudoku(PuzzleA)
cellSolutions = {};
if ~reject(PuzzleA)
if accept(PuzzleA)
disp(‘solution found’)
cellSolutions{end+1} = PuzzleA;
end
[nP, Idx] = firstChild(PuzzleA);
while (~isempty(nP))
possibleSolution = solveSudoku(nP);
if (~isempty(possibleSolution))
cellSolutions{end+1} = possibleSolution{1};
end
nP = nextChild(nP,Idx);
end
end
end
The return cellSolutions is a cell array that contains 0, 1, or more solutions depending on how many exist. The trouble I’m having is actually getting the solutions that are found to appear in the cellSolution returned with the initial call. What I’m hoping for is:
cellSolutions
{[9×9 double] [9×9 double]} or {} or {[9×9 double]}
I can indeed confirm that one or more solutions are found with the disp call, but I’m not sure how to manipulate the cell array. I’m pretty sure the problem lies with:
while (~isempty(nP))
possibleSolution = solveSudoku(nP);
if (~isempty(possibleSolution))
cellSolutions{end+1} = possibleSolution{1};
end
nP = nextChild(nP,Idx);
end
Some other info: reject determines whether the current puzzle satisfies the rules of sudoku; accept checks additionally that there are no empty spaces; firstChild finds and instance of NaN in a given sudoku puzzle, and changes it to 1 and returns linear index it changed and the new puzzle; nextChild takes a sudoku puzzle and index, if the value at that index is not 9, then it increments the value and returns the new puzzle. Thanks! recursion, sudoku, recursive backtracking, cell array MATLAB Answers — New Questions