結果

問題 No.217 魔方陣を作ろう
ユーザー kosakkunkosakkun
提出日時 2016-12-06 18:37:57
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,527 bytes
コンパイル時間 916 ms
コンパイル使用メモリ 99,240 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-28 03:11:13
合計ジャッジ時間 1,705 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 14 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
using namespace std;
typedef long long ll;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define FOREACH(i,Itr) for(auto (i)=(Itr).begin();(i)!=(Itr).end();(i)++)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
#define LBOUND(Itr,val) lower_bound((Itr).begin(),(Itr).end(),(val))
#define UBOUND(Itr,val) upper_bound((Itr).begin(),(Itr).end(),(val))


int main(){
    
    int N; cin>>N;
    int ans[100][100];
    
    if(N%2==1){
        int cnt=1;
        int sy=0,sx=N/2;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                ans[sy][sx]=cnt;
                cnt++;
                sy=(sy-1+N)%N;
                sx=(sx+1)%N;
            }
            sy=(sy+2)%N;
            sx=(sx-1+N)%N;
        }
    }
    
    if((N-2)%4==0){
        int Nt=N/2;
        int add[100][100];
        int cnt=1;
        int sy=0,sx=Nt/2;
        for(int i=0;i<Nt;i++){
            for(int j=0;j<Nt;j++){
                add[sy][sx]=cnt;
                cnt++;
                sy=(sy-1+Nt)%Nt;
                sx=(sx+1)%Nt;
            }
            sy=(sy+2)%Nt;
            sx=(sx-1+Nt)%Nt;
        }
        REP(i,N)REP(j,N){
            ans[i][j]=add[i/2][j/2];
        }
        
    }
    
    if(N%4==0){
        REP(i,N/4)REP(j,N/4){
            ans[i*4  ][j*4  ]=1;
            ans[i*4  ][j*4+3]=1;
            ans[i*4+1][j*4+1]=1;
            ans[i*4+1][j*4+2]=1;
            ans[i*4+2][j*4+1]=1;
            ans[i*4+2][j*4+2]=1;
            ans[i*4+3][j*4  ]=1;
            ans[i*4+3][j*4+3]=1;
        }
        
        int cnt=1;
        set<int> used;
        REP(i,N)REP(j,N){
            if(ans[i][j]==1){
                ans[i][j]=cnt;
                used.insert(cnt);
            }
            cnt++;
        }
        
        cnt=1;
        RREP(i,N)RREP(j,N){
            if(ans[i][j]==0){
                while(used.find(cnt)!=used.end())cnt++;
                ans[i][j]=cnt;
                used.insert(cnt);
            }
        }
        
    }
    
    REP(i,N){
        REP(j,N-1)cout<<ans[i][j]<<" ";
        cout<<ans[i][N-1]<<endl;
    }
    
    return 0;
}



0