結果
| 問題 |
No.223 1マス指定の魔方陣
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-08-01 10:20:45 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 5,000 ms |
| コード長 | 2,498 bytes |
| コンパイル時間 | 981 ms |
| コンパイル使用メモリ | 101,032 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-18 00:21:45 |
| 合計ジャッジ時間 | 3,126 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 46 |
ソースコード
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;
bool makeMagicSquare(int n, vector<vector<int> >& square)
{
if(n < 3)
return false;
square.assign(n, vector<int>(n));
if(n % 2 == 1){
int y = 0;
int x = n / 2;
int a = 0;
for(int i=0; i<n; ++i){
for(int j=0; j<n; ++j){
square[y][x] = ++ a;
y = (y + n - 1) % n;
x = (x + 1) % n;
}
y = (y + 2) % n;
x = (x + n - 1) % n;
}
}
else if(n % 4 == 0){
queue<int> q;
int a = 0;
for(int y=0; y<n; ++y){
for(int x=0; x<n; ++x){
if((y % 4 == x % 4) || (y % 4 == 3 - x % 4))
square[y][x] = ++ a;
else
q.push(++ a);
}
}
for(int y=n-1; y>=0; --y){
for(int x=n-1; x>=0; --x){
if(!((y % 4 == x % 4) || (y % 4 == 3 - x % 4))){
square[y][x] = q.front();
q.pop();
}
}
}
}
else{
const int add[][4] = {{4, 1, 2, 3}, {1, 4, 3, 2}, {1, 4, 2, 3}};
int m = n / 2;
vector<vector<int> > tmp;
makeMagicSquare(m, tmp);
for(int y=0; y<m; ++y){
for(int x=0; x<m; ++x){
int k;
if(y < m / 2 || (y == m / 2 && x != m / 2) || (y == m / 2 + 1 && x == m / 2))
k = 0;
else if(y > m / 2 + 1)
k = 1;
else
k = 2;
for(int i=0; i<4; ++i)
square[y*2+i/2][x*2+i%2] = (tmp[y][x] - 1) * 4 + add[k][i];
}
}
}
return true;
}
int main()
{
int n, x, y, z;
cin >> n >> x >> y >> z;
vector<vector<int> > a;
makeMagicSquare(n, a);
int b = (a[y-1][x-1] - 1) ^ (z - 1);
for(int i=0; i<n; ++i){
for(int j=0; j<n; ++j){
-- a[i][j];
a[i][j] ^= b;
++ a[i][j];
cout << a[i][j] << ' ';
}
cout << endl;
}
return 0;
}