Test instructions: Enter an n, which indicates that the 1~n is filled in the n*n matrix, so that each row and all diagonal diagonals of each line appear 1~n once. If present, output Yes and matrix, otherwise output no;
Idea: No train of thought during the game. Change 1~n to 0~n better to handle some. Consider only 0 of the position, each row will be 0 in one direction to move a fixed number of squares, enumerate the required number of moving steps,
If the condition is satisfied (no peers with the same column diagonal) then the yes,1~n moves with the same steps;
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intN,m,flag;intpos[5050],pre[5050];intMain () {Freopen ("avangard.in","R", stdin); Freopen ("Avangard.out","W", stdout); inti,j,k,cnt; while(SCANF ("%d", &n)! =EOF) {CNT=0; for(i=2; i<n-1; i++)//enumerate the number of moving cells{flag=1; for(j=1; j<n;j++)//enumeration number of moves{cnt=i*j%n;//0 Location if(cnt==0) flag=0;//same column Else if(CNT==J) flag=0;//on a positive diagonal Else if((cnt+j) ==n) flag=0;//on the opposite diagonal } if(flag==1) {CNT=i; Break; } } if(n==1) {printf ("yes\n");p rintf ("1\n");Continue; } for(i=1; i<=n;i++) pos[i]=i; if(!flag) printf ("no\n"); Else{printf ("yes\n"); for(i=1; i<=n;i++) { for(j=1; j<=n;j++) pre[pos[j]]=J; for(j=1; j<=n;j++) { if(j==1) printf ("%d", Pre[j]); Elseprintf"%d", Pre[j]); }printf ("\ n"); for(j=1; j<=n;j++) {Pos[j]+=cnt;//position shifted right, that is, number left if(pos[j]>n) pos[j]-=N; } } } } return 0;}
Gym 100496A avangard Latin squares (matrix games)