Gym 100496A avangard Latin squares (matrix game)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.