Matlab等价于Python的数独问题“+=”算子

2024-06-25 05:37:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找Python中运算符+=的Matlab等效函数。我一直在寻找,我意识到在Matlab中没有真正的等价物,但我不明白为什么。然而,我正在寻找一种方法或函数,它也可以做到这一点。这是用回溯法解数独的。它从一个9x9矩阵开始,生成半随机数,直到Seq0=0(不再有零)。这是我到目前为止的代码,它不会抛出任何错误,但不能正常工作,因为它不执行累加操作

clc;
S=[0,0,0,0,0,0,0,7,6; 0,2,0,9,0,0,0,0,0; 0,3,8,0,5,4,1,0,0; 
    9,0,0,5,0,0,0,3,0; 0,0,0,0,1,8,0,6,7; 4,0,0,0,0,0,2,0,0;
    7,1,3,8,6,2,0,5,0; 5,0,3,6,2,7,0,0,0; 6,0,2,0,3,0,8,9,4]

hS1=S(1,1:9); hS2=S(2,1:9); hS3=S(3,1:9);
hS4=S(6,1:9); hS5=S(5,1:9); hS6=S(6,1:9);
hS7=S(7,1:9); hS8=S(8,1:9); hS9=S(9,1:9);

L9=1:9; iteraciones=0;
S1=(reshape(S,1,[])); S1eq0=~ismember(S1,L9);
Seq0=1; remS=find(S1eq0); lenS=length(remS)

while Seq0 > 0
    iteraciones=iteraciones+1
    for kk=9:89
        fila=fix(kk/9);
        columna=rem(kk,9)+1;
        if columna<=9 && columna<=9
            if S(fila, columna)==0
                for value=1:9
                    if ~ismember(value,S(fila,:))
                        if ~ismember ( value,cat(2, hS1(:,columna),...
                                hS2(:,columna), hS3(:,columna),...
                                hS4(:,columna), hS5(:,columna),...
                                hS6(:,columna), hS7(:,columna),...
                                hS8(:,columna), hS9(:,columna)) )
                            square1 = [];
                            square2 = [];
                            square3 = [];
                            if fila<=3
                                if columna<=3
                                    for ii=1:3
                                        square1=[S(ii,1:3)]; %Here is the problem
                                    end
                                elseif columna<=6
                                    for ii=1:3
                                        square1=[S(ii,4:6)]; %here is just the same
                                    end
                                else
                                    for ii=1:3
                                        square1=[S(ii,7:9)];
                                    end
                                end

                            elseif fila<=6
                                if columna<=3
                                    for ii=4:6
                                        square2=[S(ii,1:3)];
                                    end
                                elseif columna<=6
                                    for ii=4:6
                                        square2=[S(ii,4:6)];
                                    end
                                else
                                    for ii=4:9
                                        square2=[S(ii,7:9)];
                                    end
                                end

                            else
                                if columna<=3
                                    for ii=7:9
                                        square3=[S(ii,1:3)];
                                    end
                                elseif columna<=6
                                    for ii=7:9
                                        square3=[S(ii,4:6)];
                                    end
                                else
                                    for ii=7:9
                                        square3=[S(ii,7:9)];
                                    end
                                end

                            if ~ismember(value, cat(2,square1,square2,square3))
                                S(fila,columna)=value;
                            end
                            end
                        end
                    end
                end
            end
        end
    end
    S1=(reshape(S,1,[])); S1eq0=~ismember(S1,L9);
    remS=find(S1eq0); Seq0=length(remS)
    S
    pause(2.0)
end

Tags: forifvalueendiis1columnaismember