結果

問題 No.217 魔方陣を作ろう
ユーザー mbanmban
提出日時 2017-05-09 01:40:45
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 60 ms / 5,000 ms
コード長 5,809 bytes
コンパイル時間 3,201 ms
コンパイル使用メモリ 106,328 KB
実行使用メモリ 22,804 KB
最終ジャッジ日時 2023-10-12 19:20:22
合計ジャッジ時間 4,626 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,688 KB
testcase_01 AC 59 ms
20,788 KB
testcase_02 AC 58 ms
20,784 KB
testcase_03 AC 59 ms
20,672 KB
testcase_04 AC 58 ms
20,732 KB
testcase_05 AC 60 ms
20,676 KB
testcase_06 AC 59 ms
22,704 KB
testcase_07 AC 60 ms
22,756 KB
testcase_08 AC 60 ms
20,792 KB
testcase_09 AC 60 ms
22,804 KB
testcase_10 AC 60 ms
20,772 KB
testcase_11 AC 58 ms
20,636 KB
testcase_12 AC 59 ms
20,656 KB
testcase_13 AC 59 ms
18,624 KB
testcase_14 AC 60 ms
22,796 KB
testcase_15 AC 60 ms
18,636 KB
testcase_16 AC 59 ms
22,708 KB
testcase_17 AC 60 ms
22,804 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        new Magatro().Solve();
    }
}



public class Magatro
{
    private int N;
    private string[] S;

    private void Scan()
    {
        N = int.Parse(Console.ReadLine());
    }

    public void Solve()
    {
        Scan();
        if (N % 2 == 1)
        {
            int[][] ans = new int[N][];
            for (int i = 0; i < N; i++)
            {
                ans[i] = new int[N];
            }
            int posY = 0;
            int posX = N / 2;
            for (int i = 1; i <= N * N; i++)
            {
                if (ans[posY][posX] != 0)
                {
                    posY += 2;
                    posY %= N;
                    posX += N - 1;
                    posX %= N;
                }
                ans[posY][posX] = i;
                posY += N - 1;
                posY %= N;
                posX++;
                posX %= N;
            }
            for (int i = 0; i < N; i++)
            {
                Console.WriteLine(string.Join(" ", ans[i]));
            }
        }
        else if (N % 4 == 0)
        {
            int[][] ans = new int[N][];
            for (int i = 0; i < N; i++)
            {
                ans[i] = new int[N];
            }
            int cnt = 1;
            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    int ii = i % 4;
                    int jj = j % 4;
                    if (ii == 0 || ii == 3)
                    {
                        if (jj == 0 || jj == 3)
                        {
                            ans[i][j] = cnt;
                        }
                    }
                    if (ii == 1 || ii == 2)
                    {
                        if (jj == 1 || jj == 2)
                        {
                            ans[i][j] = cnt;
                        }
                    }
                    cnt++;
                }
            }
            cnt = 1;
            for (int i = N - 1; i >= 0; i--)
            {
                for (int j = N - 1; j >= 0; j--)
                {
                    if (ans[i][j] == 0)
                    {
                        ans[i][j] = cnt;
                    }
                    cnt++;
                }
            }
            for (int i = 0; i < N; i++)
            {
                Console.WriteLine(string.Join(" ", ans[i]));
            }
        }
        else
        {
            int[][] ans = new int[N / 2][];
            for (int i = 0; i < N / 2; i++)
            {
                ans[i] = new int[N / 2];
            }
            int posY = 0;
            int posX = N / 4;
            for (int i = 1; i <= N * N / 4; i++)
            {
                if (ans[posY][posX] != 0)
                {
                    posY += 2;
                    posY %= N / 2;
                    posX += N / 2 - 1;
                    posX %= N / 2;
                }
                ans[posY][posX] = i;
                posY += N / 2 - 1;
                posY %= N / 2;
                posX++;
                posX %= N / 2;
            }
            for (int i = 0; i < N / 2; i++)
            {
                for (int j = 0; j < N / 2; j++)
                {
                    ans[i][j] = (ans[i][j] - 1) * 4;
                }
            }
            int[][] res = new int[N][];
            for (int i = 0; i < N; i++)
            {
                res[i] = new int[N];
            }

            char[][] LUX = new char[N / 2][];
            for (int i = 0; i < N / 2; i++)
            {
                LUX[i] = new char[N / 2];
            }
            for (int i = 0; i < N / 2; i++)
            {
                for (int j = 0; j < N / 2; j++)
                {
                    if (i == N / 4 + 1)
                    {
                        LUX[i][j] = 'U';
                    }
                    else if (i > N / 4 + 1)
                    {
                        LUX[i][j] = 'X';
                    }
                    else
                    {
                        LUX[i][j] = 'L';
                    }
                }
            }
            LUX[N / 4][N / 4] = 'U';
            LUX[N / 4 + 1][N / 4] = 'L';

            for (int i = 0; i < N / 2; i++)
            {
                for (int j = 0; j < N / 2; j++)
                {
                    if (LUX[i][j] == 'L')
                    {
                        res[i * 2][j * 2] = ans[i][j] + 4;
                        res[i * 2][j * 2 + 1] = ans[i][j] + 1;
                        res[i * 2 + 1][j * 2] = ans[i][j] + 2;
                        res[i * 2 + 1][j * 2 + 1] = ans[i][j] + 3;
                    }
                    if (LUX[i][j] == 'U')
                    {
                        res[i * 2][j * 2] = ans[i][j] + 1;
                        res[i * 2][j * 2 + 1] = ans[i][j] + 4;
                        res[i * 2 + 1][j * 2] = ans[i][j] + 2;
                        res[i * 2 + 1][j * 2 + 1] = ans[i][j] + 3;
                    }
                    if (LUX[i][j] == 'X')
                    {
                        res[i * 2][j * 2] = ans[i][j] + 1;
                        res[i * 2][j * 2 + 1] = ans[i][j] + 4;
                        res[i * 2 + 1][j * 2] = ans[i][j] + 3;
                        res[i * 2 + 1][j * 2 + 1] = ans[i][j] + 2;
                    }
                }
            }
            for (int i = 0; i < N; i++)
            {
                Console.WriteLine(string.Join(" ", res[i]));
            }
        }
    }
}
0