結果
| 問題 |
No.1169 Row and Column and Diagonal
|
| コンテスト | |
| ユーザー |
Atomic67
|
| 提出日時 | 2020-09-03 00:16:32 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,767 bytes |
| コンパイル時間 | 1,700 ms |
| コンパイル使用メモリ | 176,996 KB |
| 実行使用メモリ | 258,228 KB |
| 最終ジャッジ日時 | 2024-11-22 02:53:57 |
| 合計ジャッジ時間 | 40,099 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 1 TLE * 12 |
ソースコード
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
using namespace std;
using ll = long long;
template<class T> inline bool chmin(T& a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
using Field = vector<vector<int>>;
void solver(int n, vector<vector<int>> &field, vector<Field> &res) {
int empty_i = -1;
int empty_j = -1;
for(int i = 0; i < n && empty_i == -1; ++i) {
for(int j = 0; j < n && empty_j == -1; ++j) {
if(field[i][j] == -1) {
empty_i = i;
empty_j = j;
break;
}
}
}
if(empty_i == -1 || empty_j == -1) {
res.push_back(field);
return;
}
// どの数値を入れるか
vector<bool> can_use(n+1, true);
for(int i = 0; i < n; ++i) {
if(field[empty_i][i] != -1) can_use[field[empty_i][i]] = false;
if(field[i][empty_j] != -1) can_use[field[i][empty_j]] = false;
}
for(int num = 1; num <= n; ++num) {
if(!can_use[num]) continue;
field[empty_i][empty_j] = num;
solver(n, field, res);
}
field[empty_i][empty_j] = -1;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
Field field(n, vector<int>(n, -1));
rep(i, n) {
rep(j, n) {
if(i == j) {
field[i][j] = i+1;
}
}
}
vector<Field> res;
solver(n, field, res);
Field ans = res[0];
rep(i, n) {
rep(j, n) {
printf("%d ",ans[i][j]);
}
printf("\n");
}
}
Atomic67