DLX算法及应用(二)Matlab解数独-数组模拟链表

这个是用数组模拟链表,解数独专用,非DLX模板

function [ans,times] = DLX_solve_sudoku(problem)
global S L R U D colRoot rowRoot temp times
problem = problem - '0';

times = 0;
S = zeros(1,325);
L = [325,1:324];
R = [2:325,1];
U = 1:325;
D = 1:325;
colRoot = 1:325;
rowRoot = zeros(1,325);

cnt = 325;
for ix = 1:81
    row = ceil(ix/9);
    col = mod(ix-1,9)+1;
    box = 3*(ceil(row/3)-1)+ceil(col/3);
    if problem(ix) == 0
        for val = 1:9
            
            current_col = [ix, 81+9*(row-1)+val, 162+9*(col-1)+val,...
                243+9*(box-1)+val]+1;
            
            L = [L, cnt+4, cnt+1:cnt+3];
            R = [R, cnt+2:cnt+4, cnt+1];
            
            D(U(current_col)) = cnt+1:cnt+4;
            D = [D, current_col];
            U = [U, U(current_col)];
            U(current_col) = cnt+1:cnt+4;
            S(current_col) = S(current_col)+1;
            colRoot = [colRoot, current_col];
            rowRoot = [rowRoot, ones(1,4)*

你可能感兴趣的:(CPP)