Advertisement
bojandam1

Cas 05.11.24 : Sudoku

Nov 5th, 2024 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.33 KB | None | 0 0
  1. %sep_first_col(X,Ost,R) X | Ost (vizuelno) poveke nameneto za R da e vlez a X i Ost izlez
  2. sep_first_col([X],[Ost],[[X|Ost]]):-!.
  3. sep_first_col([X|OstX],[Ost|OstRest],[[X|Ost]|OstR]):-sep_first_col(OstX,OstRest,OstR).
  4.  
  5. %Za da moze transponirano da se koristi za naoganje na transponiranata matrica
  6. empty([[]]):-!.
  7. empty([[]|Ost]):-empty(Ost).
  8. e_list([]).
  9. e_list([_|_]).
  10.  
  11. transponirano([X],R):-sep_first_col(X,Ost,R),empty(Ost),!. %Ova so empty e za da moze da se napise ?- so nepoznato R
  12. transponirano([X|Ost],R):-sep_first_col(X,OstT,R),transponirano(Ost,OstT).
  13.  
  14. count(_,[],0):-!.
  15. count(X,[X|Ost],R):-!,count(X,Ost,R1),R is R1+1.
  16. count(X,[_|Ost],R):-count(X,Ost,R).
  17.  
  18. len([],0):-!.
  19. len([_|Ost],R) :-len(Ost,R1), R is R1+1.
  20.  
  21. red_ok(L):-len(L,R),red_ok(L,R).
  22. red_ok(L,1):-!,count(1,L,1).
  23. red_ok(L,N):-count(N,L,1),N1 is N-1, red_ok(L,N1).
  24.  
  25. redovi_ok([]):-!.
  26. redovi_ok([X|Ost]):-red_ok(X),redovi_ok(Ost).
  27.  
  28. col_ok(L):-transponirano(L,R),redovi_ok(R).
  29.  
  30. seci_red([],[[]],0.0,_):-!.
  31. seci_red(L,[[]|Ost],0.0,Len):-seci_red(L,Ost,Len,Len),!.
  32. seci_red([X|Ost],[[X|Ost1]|Ost2],N,Len):- N1 is N-1,seci_red(Ost,[Ost1|Ost2],N1,Len).
  33. seci_red(L,R):-len(L,N1),N is sqrt(N1),seci_red(L,R,N,N).
  34.  
  35. seci_redovi([],[]):-!.
  36. seci_redovi([L|OstL],[R|OstR]):-seci_red(L,R),seci_redovi(OstL,OstR).
  37.  
  38. trgni_nivo([],[]):-!.
  39. trgni_nivo([[X|Ost1]|Ost],[X|OstR]):-trgni_nivo([Ost1|Ost],OstR),!.
  40. trgni_nivo([[]|Ost],R):-trgni_nivo(Ost,R),!.
  41. trgni_nivo([X|Ost],R):-not(e_list(X)),trgni_nivo(Ost,R).
  42.  
  43. spoi_nadnivo([[]],[]):-!.
  44. spoi_nadnivo([[]|Ost],R):-spoi_nadnivo(Ost,R),!.
  45. spoi_nadnivo([[X|Ost]|Ost2],[X|Ost1]):-spoi_nadnivo([Ost|Ost2],Ost1).
  46.  
  47. spoi_nadnivoa([],[]):-!.
  48. spoi_nadnivoa([X|Ost],[R|OstR]):-spoi_nadnivo(X,R),spoi_nadnivoa(Ost,OstR).
  49.  
  50. kvadratiziraj(L,R):- seci_redovi(L,L1),transponirano(L1,L2),seci_redovi(L2,R1),trgni_nivo(R1,R2 ),spoi_nadnivoa(R2,R).
  51. kvad_ok(L):-kvadratiziraj(L,R),redovi_ok(R).
  52.  
  53. sudoku_ok(R):-redovi_ok(R),col_ok(R),kvad_ok(R).
  54.  
  55.  
  56. sudoku(L,R):- len(L,Len),LenP is Len+1, gen_sudoku(L,R,LenP),sudoku_ok(R),!.
  57.  
  58. gen_sudoku([],[],_).
  59. gen_sudoku([[a|Ost]|Ost1], [[X|OstR]|OstR1], LenP) :-between(1,LenP,X), gen_sudoku([Ost|Ost1], [OstR|OstR1], LenP).
  60. gen_sudoku([[]|Ost],[[]|OstR],LenP):- gen_sudoku(Ost,OstR,LenP).
  61. gen_sudoku( [[X|Ost]|Ost1], [[X|OstR]|OstR1], LenP):-  not(X = a),        gen_sudoku([Ost|Ost1], [OstR|OstR1], LenP).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement