function SudokuFree(X) %von Marina Braun %___________________________________________________________ %Einführung: %Eingabe in Matlab: %eine Matrix X -> steht im Workspace %SudokuFree(X) %Dieses Programm testet, ob die Matrix X generell als Sudoku gelöst %werden kann, nicht, wie viele Lösungen es gibt oder ob es eindeutig %lösbar ist. Also wäre auch die Nullmatrix ein Sudoku, da für alle %leeren Stellen Zahlen als Sudoku eingetragen werden können. %Einteilung der Matrix in Quadrate: % I II III %Matrix X= IV V VI % VII VIII IX %____________________________________________________________ %Variablen xok1 =0; xok2 =0; xok3 =0; xok4 =0; xok5 =0; xok6 =0; %______________________________________________ %header Zeit = clock; Std = num2str(Zeit(4)); Min = num2str(Zeit(5)); disp('_____________________________________') disp(' ') disp (['#' 'Test, ob Matrix X ein 9x9-Sudoku ist' ]) disp('#') disp('#Marina Braun') disp('#') disp (['#' date ' ' Std ':' Min]) disp('_____________________________________') %________________________________________________ %test auf 9x9 [x,y] = size (X); if x~=9 || y ~= 9 xok1 = 1; end if xok1 == 1 disp ('X ist keine 9x9 Matrix'); return; end %________________________________________________ %test auf zahlen im Bereich 0-9 for a=1:x for b=1:y if X(a,b) < 0 || X(a,b)> 9 xok2 = 2; end end end if xok2 == 2 disp ('mindestens eine Zahl liegt nicht im Bereich 0 bis 9'); end %__________________________________________________ %test auf ganze Zahlen for a=1:x for b=1:y if X(a,b)~= ceil(X(a,b)); xok3 = 3; end end end if xok3 == 3 disp ('die Matrix enthält eine nicht natürliche Zahl'); end %__________________________________________________ %test auf keine doppelte Zahl in Zeilen (außer 0) doppelte = 0; c = 0; a= 0; zeile= 0; for zeile=1:x %eine zeile wird geprüft %Schleife bis Ende der Matrix for c =1:x %Test auf doppelte Zahl c for a=1:x %stelle der zahl in der zeile if X(zeile,a)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich in Zeile: '); disp (zeile); xok4=xok4+1; end doppelte=0; end end %__________________________________________________ %test auf keine doppelte Zahl in Spalten (außer 0) doppelte = 0; c = 0; a= 0; spalte= 0; for spalte = 1:x % eine spalte wird geprüft %Schleife bis Ende der Matrix for c =1:x %Test auf doppelte Zahl c for a=1:x %stelle der zahl in der spalte if X(a,spalte)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich in Spalte: '); disp(spalte); xok5=xok5+1; end doppelte=0; end end %__________________________________________________ %test auf keine doppelten Zahlen in den 9 Quadraten %Quadrat I doppelte = 0; c = 0; a= 0; for c =1:x %Test auf Zahl c for a=1:3 %zeile if X(a,1)== c doppelte = doppelte +1; end if X(a,2)== c doppelte = doppelte +1; end if X(a,3)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich im Quadrat I.'); xok6=xok6+1; end doppelte=0; end %Quadrat II doppelte = 0; c = 0; a= 0; for c =1:x %Test auf Zahl c for a=1:3 %zeile if X(a,4)== c doppelte = doppelte +1; end if X(a,5)== c doppelte = doppelte +1; end if X(a,6)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich im Quadrat II.'); xok6=xok6+1; end doppelte=0; end %Quadrat III doppelte = 0; c = 0; a= 0; for c =1:x %Test auf Zahl c for a=1:3 %zeile if X(a,7)== c doppelte = doppelte +1; end if X(a,8)== c doppelte = doppelte +1; end if X(a,9)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich im Quadrat III.'); xok6=xok6+1; end doppelte=0; end %Quadrat IV doppelte = 0; c = 0; a= 0; for c =1:x %Test auf Zahl c for a=4:6 %zeile if X(a,1)== c doppelte = doppelte +1; end if X(a,2)== c doppelte = doppelte +1; end if X(a,3)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich im Quadrat IV.'); xok6=xok6+1; end doppelte=0; end %Quadrat V doppelte = 0; c = 0; a= 0; for c =1:x %Test auf Zahl c for a=4:6 %zeile if X(a,4)== c doppelte = doppelte +1; end if X(a,5)== c doppelte = doppelte +1; end if X(a,6)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich im Quadrat V.'); xok6=xok6+1; end doppelte=0; end %Quadrat VI doppelte = 0; c = 0; a= 0; for c =1:x %Test auf Zahl c for a=4:6 %zeile if X(a,7)== c doppelte = doppelte +1; end if X(a,8)== c doppelte = doppelte +1; end if X(a,9)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich im Quadrat VI.'); xok6=xok6+1; end doppelte=0; end %Quadrat VII doppelte = 0; c = 0; a= 0; for c =1:x %Test auf Zahl c for a=7:9 %zeile if X(a,1)== c doppelte = doppelte +1; end if X(a,2)== c doppelte = doppelte +1; end if X(a,3)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich im Quadrat VII.'); xok6=xok6+1; end doppelte=0; end %Quadrat VIII doppelte = 0; c = 0; a= 0; for c =1:x %Test auf Zahl c for a=7:9 %zeile if X(a,4)== c doppelte = doppelte +1; end if X(a,5)== c doppelte = doppelte +1; end if X(a,6)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich im Quadrat VIII.'); xok6=xok6+1; end doppelte=0; end %Quadrat IX doppelte = 0; c = 0; a= 0; for c =1:x %Test auf Zahl c for a=7:9 %zeile if X(a,7)== c doppelte = doppelte +1; end if X(a,8)== c doppelte = doppelte +1; end if X(a,9)== c doppelte = doppelte +1; end end if doppelte > 1 disp('mindestes eine doppelte Zahl befindet sich im Quadrat IX.'); xok6=xok6+1; end doppelte=0; end %________________________________________ %Das Fazit: if xok1==0 && xok2==0 && xok3==0 && xok4==0 && xok5==0 && xok6==0 disp ('Alles ok. Die Matrix X kann als Sudoku ausgefüllt werden.'); else disp ('Die Matrix lässt sich nicht als Sudoku ausfüllen.'); end end %gehört zu function