結果
問題 | No.217 魔方陣を作ろう |
ユーザー |
![]() |
提出日時 | 2016-12-29 23:08:24 |
言語 | C90 (gcc 12.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,802 bytes |
コンパイル時間 | 667 ms |
コンパイル使用メモリ | 22,912 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-12-15 16:14:18 |
合計ジャッジ時間 | 1,509 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 13 WA * 5 |
コンパイルメッセージ
main.c: In function ‘main’: main.c:64:9: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 64 | scanf("%d", &n); | ^~~~~~~~~~~~~~~
ソースコード
#include <stdio.h>#include <stdbool.h>#define rep(i, n) for (i = 0; i < n; i++)#define rrep(i, n) for (i = n; i >= 0; i--)// (2n+1)x(2n+1) (ヒンズーの連続方式)void solve1(int ans[20][20], int n) {int x = n / 2, y = 0, t = 1;while (t <= n * n) {while (1) {while (x < 0) x += n;while (x >= n) x -= n;while (y < 0) y += n;while (y >= n) y -= n;if (ans[x][y] != 0) { x--; y += 2; }else break;}ans[x++][y--] = t++;}}// 4x4void solve2(int ans[20][20], int n) {int i, j;int t1 = 1, t2 = n * n;rep(i, n) rep(j, n) {bool f1 = i % 4 == 0 || (i + 1) % 4 == 0;bool f2 = j % 4 == 0 || (j + 1) % 4 == 0;if ((f1 && f2) || (!f1 && !f2)) ans[i][j] = t1++;else ans[i][j] = t2++;}}// (4n+2)x(4n+2) (LUX法)void solve3(int ans[20][20], int n) {int i, j, k, l;int t1[20][20] = {}, t2[20][20], tn = n / 2;int lm[2][2] = { { 4,2 },{ 1,3 } };int um[2][2] = { { 1,2 },{ 4,3 } };int xm[2][2] = { { 1,3 },{ 4,2 } };enum { L, U, X };solve1(t1, tn);rep(i, tn) rep(j, tn) {t1[i][j]--; t1[i][j] *= 4;if (i == tn / 2 && j == tn / 2) t2[i][j] = U;else if (i == tn / 2 && j == tn / 2 + 1) t2[i][j] = L;else if (j == tn / 2 + 1) t2[i][j] = U;else if (j < tn / 2 + 1) t2[i][j] = L;else t2[i][j] = X;}for (i = 0; i < n; i += 2) for (j = 0; j < n; j += 2) {int t3 = t1[i / 2][j / 2], t4;rep(k, 2) rep(l, 2) {if (t2[i / 2][j / 2] == L) t4 = lm[k][l];else if (t2[i / 2][j / 2] == U) t4 = um[k][l];else t4 = xm[k][l];ans[i + k][j + l] = t3 + t4;}}}int main() {int n;scanf("%d", &n);int ans[20][20] = {}, i, j;if (n % 2 == 1) solve1(ans, n);else if (n % 4 == 0) solve2(ans, n);else solve3(ans, n);rep(i, n) {rep(j, n) printf("%d ", ans[i][j]);printf("\n");}return 0;}