function X=SudokuFast(X) %Programm zum möglichst schnellen Lösen von Sudokus von Moritz Weber while (sum(sum(X==0))~=0) action=1; while (action==1) action=0; for ii=1:9 z=(ceil(ii/3)*3)-2; Spalte=X(:,ii); for jj=1:9 if (sum(Spalte==jj)==0) moeglich=0; anzahlMoeglich=0; for kk=1:9 s=(ceil(kk/3)*3)-2; if(X(kk,ii)==0 && sum(X(kk,:)==jj)==0 && sum(sum(X(s:s+2,z:z+2)==jj))==0) moeglich=kk; anzahlMoeglich=anzahlMoeglich+1; if (anzahlMoeglich==2) break; end end end if (anzahlMoeglich==1) X(moeglich,ii)=jj; action=1; end if (sum(sum(X==0))==0) return; end end end end for ii=1:9 s=(ceil(ii/3)*3)-2; Zeile=X(ii,:); if (sum(Zeile==0)==9) break; end for jj=1:9 if (sum(Zeile==jj)==0) moeglich=0; anzahlMoeglich=0; for kk=1:9 z=(ceil(kk/3)*3)-2; if(X(ii,kk)==0 && sum(X(:,kk)==jj)==0 && sum(sum(X(s:s+2,z:z+2)==jj))==0) moeglich=kk; anzahlMoeglich=anzahlMoeglich+1; if (anzahlMoeglich==2) break; end end end if (anzahlMoeglich==1) X(ii,moeglich)=jj; action=1; end if (sum(sum(X==0))==0) return; end end end end for ii=1:3:7 for iii=1:3:7 for jj=1:9 Mini=X(ii:ii+2,iii:iii+2); if (sum(sum(Mini==jj))==0) moeglichz=0; moeglichs=0; anzahlMoeglich=0; for mm=ii:ii+2 for nn=iii:iii+2 if (X(mm,nn)==0 && sum(X(mm,:)==jj)==0 && sum(X(:,nn)==jj)==0) moeglichz=mm; moeglichs=nn; anzahlMoeglich=anzahlMoeglich+1; if (anzahlMoeglich==2) break; end end end end if (anzahlMoeglich==1) X(moeglichz,moeglichs)=jj; action=1; end if (sum(sum(X==0))==0) return; end end end end end end C=cell(9); numC=zeros(9); for ii=1:81 z=(mod(ii-1,9)+1); s=(ceil(ii/9)); zm=(ceil(z/3)*3)-2; sm=(ceil(s/3)*3)-2; anzahlMoeglich=0; for jj=1:9 if (X(z,s)==0 && sum(X(z,:)==jj)==0 && sum(X(:,s)==jj)==0 && sum(sum(X(zm:zm+2,sm:sm+2)==jj))==0) moeglich=jj; anzahlMoeglich=anzahlMoeglich+1; C{z,s}(anzahlMoeglich)=jj; numC(z,s)=anzahlMoeglich; end end if (anzahlMoeglich==1) X(z,s)=moeglich; C{z,s}=[]; numC(z,s)=0; end end if (sum(sum(X==0))~=0) k=10; pos=0; if(numC==zeros(9)) return; end for ii=1:81 if (k>numC(ii) && numC(ii)>0) k=numC(ii); pos=ii; if (k==2) break; end end end if (pos>0) for hh=1:k X(pos)=C{pos}(hh); Yl=SudokuFast(X); if (sum(sum(Yl==0))==0) X=Yl; break; end end else return; end end end end