Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2022
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. n:=9;;
  2. L:=List([1..n],i->List([1..n],j->0));
  3. pos_max:=0;
  4.  
  5. Backtracking:=function(i,j)
  6. local s,pos;
  7.  
  8. # Print(L,"\n");
  9.  
  10. for s in [1..n] do
  11. if(ForAny([1..i-1],i2->L[i2][j]=s)) then continue; fi;
  12. if(ForAny([1..j-1],j2->L[i][j2]=s)) then continue; fi;
  13.  
  14. if(ForAny([1..i-1],k->L[((i-k-1) mod n) + 1][((j-k-1) mod n) + 1]=s)) then continue; fi;
  15. if(ForAny([1..i-1],k->L[((i-k-1) mod n) + 1][((j+k-1) mod n) + 1]=s)) then continue; fi;
  16.  
  17. L[i][j] := s;
  18.  
  19. pos:=n*i+j;
  20. if(pos>pos_max) then
  21. Print("Best thus far: ",L,"\n");
  22. pos_max := pos;
  23. fi;
  24.  
  25. if(j=n) then
  26. if(i=n) then
  27. Print(L,"\n");
  28. else
  29. Backtracking(i+1,1);
  30. fi;
  31. else
  32. Backtracking(i,j+1);
  33. fi;
  34.  
  35. L[i][j] := 0;
  36. od;
  37. end;;
  38.  
  39. Backtracking(1,1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement