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(i,j)
- local s,pos;
- # Print(L,"\n");
- for s in [1..n] do
- if(ForAny([1..i-1],i2->L[i2][j]=s)) then continue; fi;
- if(ForAny([1..j-1],j2->L[i][j2]=s)) then continue; fi;
- if(ForAny([1..i-1],k->L[((i-k-1) mod n) + 1][((j-k-1) mod n) + 1]=s)) then continue; fi;
- if(ForAny([1..i-1],k->L[((i-k-1) mod n) + 1][((j+k-1) mod n) + 1]=s)) then continue; fi;
- L[i][j] := s;
- pos:=n*i+j;
- if(pos>pos_max) then
- Print("Best thus far: ",L,"\n");
- pos_max := pos;
- fi;
- if(j=n) then
- if(i=n) then
- Print(L,"\n");
- else
- Backtracking(i+1,1);
- fi;
- else
- Backtracking(i,j+1);
- fi;
- L[i][j] := 0;
- od;
- end;;
- Backtracking(1,1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement