How do I modify this code so it counts how many times the recursive function is called upon?

5 Ansichten (letzte 30 Tage)
I'm working on a project to evaluate different sudoku, but I would like to be able to count how many times this recursive function is called upon for each sudoku. wondering if you could help me modify it so it uses a global variable to do so. Here the function I'm currently using?
function X = sudoku(X)
% SUDOKU Solve Sudoku using recursive backtracking.
% sudoku(X), expects a 9-by-9 array X.
% Fill in all “singletons”.
% C is a cell array of candidate vectors for each cell.
% s is the first cell, if any, with one candidate.
% e is the first cell, if any, with no candidates.
[C,s,e] = candidates(X);
while ~isempty(s) && isempty(e)
X(s) = C{s};
[C,s,e] = candidates(X);
end
% Return for impossible puzzles.
if ~isempty(e)
return
end
% Recursive backtracking.
if any(X(:) == 0)
Y = X;
z = find(X(:) == 0,1); % The first unfilled cell.
for r = [C{z}] % Iterate over candidates.
X = Y;
X(z) = r; % Insert a tentative value.
X = sudoku(X); % Recursive call.
if all(X(:) > 0) % Found a solution.
return
end
end
end
% ------------------------------
function [C,s,e] = candidates(X)
C = cell(9,9);
tri = @(k) 3*ceil(k/3-1) + (1:3);
for j = 1:9
for i = 1:9
if X(i,j)==0
z = 1:9;
z(nonzeros(X(i,:))) = 0;
z(nonzeros(X(:,j))) = 0;
z(nonzeros(X(tri(i),tri(j)))) = 0;
C{i,j} = nonzeros(z);
end
end
end
L = cellfun(@length,C); % Number of candidates.
s = find(X==0 & L==1,1);
e = find(X==0 & L==0,1);
end % candidates
end % sudoku

Antworten (2)

Guillaume
Guillaume am 6 Mär. 2019
You could also not bother with the persistent variable and just pass the count as input/output:
function [X, count] = sudoku(X, count)
if nargin < 2
count = 0;
else
count = count + 1;
end
%... rest of the code that leads to the recursive call
[X, count] = sudoku(X, count);
end

Matt J
Matt J am 5 Mär. 2019
Bearbeitet: Matt J am 5 Mär. 2019
Use a persistent counter variable:
function [X,count] = sudoku(X, restart)
persistent count
if nargin<2, restart=false; end
if isempty(count) || restart,
count=1;
else
count=count+1;
end
[C,s,e] = candidates(X);
.... %the rest of your code
  4 Kommentare
Guillaume
Guillaume am 6 Mär. 2019
It's much better if you just copy/paste the full text of the error rather than giving us a (misspelled) interpretation of it.
Assuming that the error is what I think it is, it is still true in the latest version of matlab. The persistent variable must be declared before it's used, and it can't be an output variable. So, while Matt's answer explained the correct concept, the actual execution is a bit off.
function [X, count] = sudoku(X, restart)
persistent internalcount; %can't be a return variable
if isempty(internalcount) || restart
internalcount = 1;
else
internalcount = internalcount + 1;
end
%... rest of the code
count = internalcount;
end
Peter Takach
Peter Takach am 7 Mär. 2019
Bearbeitet: Peter Takach am 7 Mär. 2019
yes i quite agree unfortuantely i was sending it from my phone at work. thanks so much for the help though. appreciate you working through my spelling errors.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Just for fun finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by