Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- n:=9;;
- L:=List([1..n],i->List([1..n],j->0));
- pos_max:=0;
- Backtracking:=function(j)
- local i,r,c,s,clash;
- # Print(L,"\n");
- for i in [1..n] do
- clash := false;
- # fill in diagonal
- for r in [1..n] do
- c := ((j+r-2) mod n) + 1;
- s := ((i+r-2) mod n) + 1;
- L[r][c] := s;
- # repeated symbol in row
- if(ForAny([1..r-1],r2->L[r2][c]=s)) then clash:=true; continue; fi;
- # repeated symbol in col
- if(ForAny([1..c-1],c2->L[r][c2]=s)) then clash:=true; continue; fi;
- # repeated symbol in antidiagonal
- if(ForAny([2..n],k->L[((r-k) mod n) + 1][((c+k-2) mod n) + 1]=s)) then clash:=true; continue; fi;
- od;
- if(clash <> true) then
- if(j>pos_max) then
- Print("Best thus far: ",L,"\n");
- pos_max := j;
- fi;
- if(j=n) then
- Print(L,"\n");
- else
- Backtracking(j+1);
- fi;
- fi;
- for r in [1..n] do
- c := ((j+r-2) mod n) + 1;
- L[r][c] := 0;
- od;
- od;
- end;;
- Backtracking(1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement