{$A8,B-,C+,D+,E-,F-,G+,H+,I+,J-,K-,L+,M-,N+,O+,P+,Q-,R-,S-,T-,U-,V+,W-,X+,Y+,Z1} {$MINSTACKSIZE $00004000} {$MAXSTACKSIZE $00100000} {$IMAGEBASE $00400000} {$APPTYPE GUI} {$WARN SYMBOL_DEPRECATED ON} {$WARN SYMBOL_LIBRARY ON} {$WARN SYMBOL_PLATFORM ON} {$WARN UNIT_LIBRARY ON} {$WARN UNIT_PLATFORM ON} {$WARN UNIT_DEPRECATED ON} {$WARN HRESULT_COMPAT ON} {$WARN HIDING_MEMBER ON} {$WARN HIDDEN_VIRTUAL ON} {$WARN GARBAGE ON} {$WARN BOUNDS_ERROR ON} {$WARN ZERO_NIL_COMPAT ON} {$WARN STRING_CONST_TRUNCED ON} {$WARN FOR_LOOP_VAR_VARPAR ON} {$WARN TYPED_CONST_VARPAR ON} {$WARN ASG_TO_TYPED_CONST ON} {$WARN CASE_LABEL_RANGE ON} {$WARN FOR_VARIABLE ON} {$WARN CONSTRUCTING_ABSTRACT ON} {$WARN COMPARISON_FALSE ON} {$WARN COMPARISON_TRUE ON} {$WARN COMPARING_SIGNED_UNSIGNED ON} {$WARN COMBINING_SIGNED_UNSIGNED ON} {$WARN UNSUPPORTED_CONSTRUCT ON} {$WARN FILE_OPEN ON} {$WARN FILE_OPEN_UNITSRC ON} {$WARN BAD_GLOBAL_SYMBOL ON} {$WARN DUPLICATE_CTOR_DTOR ON} {$WARN INVALID_DIRECTIVE ON} {$WARN PACKAGE_NO_LINK ON} {$WARN PACKAGED_THREADVAR ON} {$WARN IMPLICIT_IMPORT ON} {$WARN HPPEMIT_IGNORED ON} {$WARN NO_RETVAL ON} {$WARN USE_BEFORE_DEF ON} {$WARN FOR_LOOP_VAR_UNDEF ON} {$WARN UNIT_NAME_MISMATCH ON} {$WARN NO_CFG_FILE_FOUND ON} {$WARN MESSAGE_DIRECTIVE ON} {$WARN IMPLICIT_VARIANTS ON} {$WARN UNICODE_TO_LOCALE ON} {$WARN LOCALE_TO_UNICODE ON} {$WARN IMAGEBASE_MULTIPLE ON} {$WARN SUSPICIOUS_TYPECAST ON} {$WARN PRIVATE_PROPACCESSOR ON} {$WARN UNSAFE_TYPE OFF} {$WARN UNSAFE_CODE OFF} {$WARN UNSAFE_CAST OFF} program Square; {$APPTYPE CONSOLE} { Problema: Square Algoritm: Author: Denis Datsiuk } const inp='square.dat'; outp='square.sol'; MaxN=20; var f:text; n,nn,i,j,z:Word; a:array [1..MaxN,1..MaxN] of Word; c:array [1..MaxN] of Boolean; Sum:Integer; procedure find (x,y:Word; gsum:Integer); var i,j,k:Byte; b:boolean; vsum,d1,d2:Integer; begin if x>n then begin for i:=1 to n do begin for j:=1 to n-1 do write (f,a[i,j],' '); Writeln(f,a[i,j]); end; Close (f); Halt; end; for k:=1 to nn do if (not c[k]) then begin gsum:=gsum+k; b:=True; if sumgsum then b:=False; end else begin if sum=gsum then b:=False; end; if x=n then begin vsum:=0; for i:=1 to n do vsum:=vsum+a[i,y]; vsum:=vsum+k; if vsum<>Sum then b:=False; end; if x=y then begin d1:=0; for i:=1 to x do d1:=d1+a[i,i]; d1:=d1+k; if x=n then begin if sum<>d1 then b:=False; end else begin if sumd2 then b:=False; end else begin if sumn then begin y:=1; inc(x); end; if y-1=0 then find (x,y,0) else find (x,y,gsum); Dec (y); if y<1 then begin y:=n; Dec(x); end; a[x,y]:=0; c[k]:=False; end; Dec (gsum,k); end; end; procedure z1(v:Byte); var i:Word; begin inc (v); if v=n+1 then begin if n<>1 then begin sum:=0; for i:=1 to n do sum:=sum+a[1,i]; find(2,1,0); end else begin Writeln (f,a[1,1]); close (f); Halt; end; Exit; end; for i:=1 to nn do if not c[i] then begin a[1,v]:=i; c[i]:=True; z1(v); c[i]:=False; end; dec (v); end; begin assign (f,inp); reset (f); readln (f,n); nn:=n*n; close (f); z:=0; for i:=1 to n do for j:=1 to n do begin inc (z); c[z]:=False; a[i,j]:=0; end; assign (f,outp); rewrite (f); z1(0); end.