結果
問題 | No.217 魔方陣を作ろう |
ユーザー |
![]() |
提出日時 | 2015-05-26 23:35:58 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 5,000 ms |
コード長 | 1,979 bytes |
コンパイル時間 | 534 ms |
コンパイル使用メモリ | 66,192 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-06 10:30:08 |
合計ジャッジ時間 | 1,369 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 18 |
ソースコード
#include <iostream>#include <algorithm>#include <functional>#include <string>#include <limits.h>#include <vector>#include <numeric>using namespace std;void func(int n, int field[20][20]){int nextx,nexty;int y,x;y = 0;x = n / 2;field[y][x] = 1;for(int i=2; i<=n*n; i++){//次へnextx = (x + 1) % n;nexty = y - 1;if(nexty == -1){nexty = n - 1;}if(field[nexty][nextx] == 0){field[nexty][nextx] = i;y = nexty;x = nextx;}else{y = (y + 1) % n;field[y][x] = i;}}}int main(){int field[20][20] = {0};int field2[20][20] = {0};int n; //3-20int x,y;int nextx,nexty;bool first[4][4] = {{1,0,0,1},{0,1,1,0},{0,1,1,0},{1,0,0,1}};int last[20][20] = {0}; //0L 1U 2Xint adjust[3][2][2] ={{{4,1},{2,3}},{{1,4},{2,3}},{{1,4},{3,2}}};int i;cin >> n;if(n % 2 == 1){func(n, field);}else if(n % 4 == 0){i = 0;for(int iy=0; iy<n; iy++){for(int ix=0; ix<n; ix++){i ++;if(first[iy%4][ix%4]){field[iy][ix] = i;}}}i = 0;for(int iy=n-1; iy>=0; iy--){for(int ix=n-1; ix>=0; ix--){i ++;if(!first[iy%4][ix%4]){field[iy][ix] = i;}}}}else if(n % 4 == 2){ //4n+2func(n/2, field2);for(int iy=0; iy<n/2; iy++){for(int ix=0; ix<n/2; ix++){field2[iy][ix] = (field2[iy][ix] - 1) * 4;}}//last表作り(n/2)//0L 1U 2Xn = n/2;for(int ix=0; ix<n; ix++){last[n/2+1][ix] = 1;}for(int iy=n/2+2; iy<n; iy++){for(int ix=0; ix<n; ix++){last[iy][ix] = 2;}}swap(last[n/2][n/2],last[n/2+1][n/2]);//作成n *= 2;int hy,hx;for(int iy=0; iy<n; iy++){for(int ix=0; ix<n; ix++){hy = iy / 2;hx = ix / 2;field[iy][ix] = field2[hy][hx] + adjust[last[hy][hx]][iy%2][ix%2];}}}//出力for(int iy=0; iy<n; iy++){for(int ix=0; ix<n; ix++){cout << field[iy][ix] << " ";}cout << endl;}cout << endl;return 0;}