結果
| 問題 |
No.223 1マス指定の魔方陣
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-01-27 15:09:00 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 1,915 bytes |
| コンパイル時間 | 2,065 ms |
| コンパイル使用メモリ | 184,576 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-06-28 01:37:31 |
| 合計ジャッジ時間 | 4,200 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 46 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
int N, Y, X, V;
cin >> N >> X >> Y >> V;
X--, Y--;
vector<vector<int>> A(N, vector<int>(N));
int cnt = 1;
vector<vector<bool>> B = {{1,0,0,1},{0,1,1,0},{0,1,1,0},{1,0,0,1}};
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
A[i][j] = (B[i % 4][j % 4] ? i * N + j + 1: N * N - (i * N + j));
}
}
auto f = [&](vector<vector<int>> &C){
vector<int> S(N + N + 2);
for(int y = 0; y < N; y++){
for(int x = 0; x < N; x++){
S[y] += C[y][x];
S[x + N] += C[y][x];
}
S[N + N] += C[y][y];
S[N + N + 1] += C[y][N - 1 - y];
}
return (count(S.begin(), S.end(), S[0]) == S.size());
};
auto rotate = [&](vector<vector<int>> &C){
vector<vector<int>> D(N, vector<int>(N));
for(int y = 0; y < N; y++){
for(int x = 0; x < N; x++){
D[x][N - 1 - y] = C[y][x];
}
}
C = D;
};
auto answer = [&](vector<vector<int>> &C){
for(int y = 0; y < N; y++){
for(int x = 0; x < N; x++){
cout << C[y][x] << (x + 1 == N ? '\n' : ' ');
}
}
exit(0);
};
for(int dy = 0; dy <= N; dy += 2){
for(int dx = 0; dx <= N; dx += 2){
vector<vector<int>> C(N, vector<int>(N));
for(int y = 0; y < N; y++){
for(int x = 0; x < N; x++){
C[(y + dy) % N][(x + dx) % N] = A[y][x];
}
}
if(!f(C))continue;
if(C[Y][X] == V)answer(C);
rotate(C);
if(C[Y][X] == V)answer(C);
rotate(C);
if(C[Y][X] == V)answer(C);
rotate(C);
if(C[Y][X] == V)answer(C);
}
}
}