結果
| 問題 |
No.217 魔方陣を作ろう
|
| コンテスト | |
| ユーザー |
沙耶花
|
| 提出日時 | 2021-11-04 22:20:13 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,164 bytes |
| コンパイル時間 | 5,620 ms |
| コンパイル使用メモリ | 262,048 KB |
| 最終ジャッジ日時 | 2025-01-25 11:46:09 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 14 WA * 4 |
ソースコード
#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000000000
void p(vector<vector<int>> ans){
rep(i,ans.size()){
rep(j,ans[i].size()){
if(j!=0)cout<<' ';
cout<<ans[i][j];
}
cout<<endl;
}
}
vector<vector<int>> solve(int n){
vector<vector<int>> ans(n,vector<int>(n,-1));
if(n%2==1){
int y = 0,x = n/2;
rep(i,n*n){
if(ans[y][x]!=-1){
i--;
}
else{
ans[y][x] = i+1;
}
y--;
x++;
x%=n;
y%=n;
if(y<0)y+=n;
if(ans[y][x]!=-1){
x--;
x%=n;
if(x<0)x+=n;
y+=2;
y%=n;
}
}
}
else if(n%4==0){
rep(i,n){
rep(j,n){
ans[i][j] = i*4 + j + 1;
}
}
rep(i,n/2){
rep(j,n){
int x = i%4,y = j%4;
if(x==0){
if(y==3||y==0)continue;
}
if(x==1){
if(y==1||y==2)continue;
}
if(x==2){
if(y==1||y==2)continue;
}
if(x==3){
if(y==0||y==3)continue;
}
int xx = n-1-x;
int yy = n-1-y;
swap(ans[xx][yy],ans[x][y]);
}
}
}
else{
auto temp = solve(n/2);
vector<string> s(temp.size(),string(temp.size(),'.'));
rep(i,s.size()){
if(i<=temp.size()/2)s[i] = string(temp.size(),'L');
else if(i==temp.size()/2+1)s[i] = string(temp.size(),'U');
else s[i] = string(temp.size(),'X');
}
swap(s[temp.size()/2+1][temp.size()/2],s[temp.size()/2][temp.size()/2]);
rep(i,temp.size()){
rep(j,temp.size()){
temp[i][j]--;
temp[i][j] *= 4;
}
}
vector<vector<int>> L = {{4,1},{2,3}};
vector<vector<int>> U = {{1,4},{2,3}};
vector<vector<int>> X = {{1,4},{3,2}};
ans.resize(0);
rep(i,temp.size()){
int sz = ans.size();
ans.push_back(vector<int>());
ans.push_back(vector<int>());
rep(j,temp[i].size()){
vector<vector<int>> aa;
if(s[i][j]=='L')aa = L;
else if(s[i][j]=='U')aa = U;
else aa = X;
rep(k,2){
rep(l,2){
ans[sz+k].push_back(temp[i][j] + aa[k][l]);
}
}
}
}
}
return ans;
}
int main(){
int n;
cin>>n;
auto ans = solve(n);
p(ans);
return 0;
}
沙耶花