結果

問題 No.1169 Row and Column and Diagonal
ユーザー Nzt3Nzt3
提出日時 2020-08-14 23:28:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 396 ms / 2,000 ms
コード長 975 bytes
コンパイル時間 1,861 ms
コンパイル使用メモリ 168,060 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-18 23:15:21
合計ジャッジ時間 4,676 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 11 ms
5,376 KB
testcase_07 AC 37 ms
5,376 KB
testcase_08 AC 73 ms
5,376 KB
testcase_09 AC 125 ms
5,376 KB
testcase_10 AC 318 ms
5,376 KB
testcase_11 AC 356 ms
5,376 KB
testcase_12 AC 386 ms
5,376 KB
testcase_13 AC 390 ms
5,376 KB
testcase_14 AC 396 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

bool check(vector<vector<int>>&a,int i,int j,int n){
    bool ans=true;
    for(int k=0;k<a.size();k++){
        if(a.at(k).at(j)==n||a.at(i).at(k)==n)
        ans=false;
    }
    return ans;
}
int main(){
    int n;
    cin >> n;
    if(n==1){
        cout << 1 << endl;
        return 0;
    }
    vector<vector<int>>a(n,vector<int>(n,-1));
    for(int i=0;i<n;i++)
        a.at(i).at(i)=i;
    int e=1,f=0;
    for(int i=0;i<n;i++){
        f+=2;
        int g=f%n;
        for(int j=i+1;j<n;j++){
            while(!check(a,i,j,g)){
                g=(g+1)%n;
            }
            a.at(i).at(j)=g;
            a.at(j).at(i)=g;
            g=(g+1)%n;
        }
        a.at(i).at(n-1)=e;
        a.at(n-1).at(i)=e;
        e=(e+2)%n;
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            cout << a.at(i).at(j)+1;
            if(j!=n-1)
            cout << " ";
        }
        cout << endl;
    }
}
0