結果

問題 No.217 魔方陣を作ろう
ユーザー 14番14番
提出日時 2016-05-04 01:53:18
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 29 ms / 5,000 ms
コード長 5,857 bytes
コンパイル時間 972 ms
コンパイル使用メモリ 110,880 KB
実行使用メモリ 26,136 KB
最終ジャッジ日時 2024-04-15 11:10:29
合計ジャッジ時間 2,584 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
23,524 KB
testcase_01 AC 25 ms
21,944 KB
testcase_02 AC 25 ms
25,960 KB
testcase_03 AC 27 ms
24,212 KB
testcase_04 AC 25 ms
23,916 KB
testcase_05 AC 24 ms
23,960 KB
testcase_06 AC 25 ms
25,572 KB
testcase_07 AC 28 ms
24,112 KB
testcase_08 AC 26 ms
23,788 KB
testcase_09 AC 25 ms
24,056 KB
testcase_10 AC 25 ms
25,576 KB
testcase_11 AC 29 ms
26,136 KB
testcase_12 AC 26 ms
25,700 KB
testcase_13 AC 25 ms
21,688 KB
testcase_14 AC 24 ms
23,672 KB
testcase_15 AC 27 ms
24,112 KB
testcase_16 AC 25 ms
23,672 KB
testcase_17 AC 25 ms
24,188 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.Generic;
using System.Text;
using System.Linq;
 
class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int len = int.Parse(Reader.ReadLine());

        int[,] ans;
        if(len % 2 == 1) {
            ans = this.CreateMahoujinKisu(len);
        } else if(len % 4 == 0) {
            ans = this.CreateMahoujin4(len);
        } else
        {
            ans = this.CreateMahoujin4_2(len);
        }
        for(int i=0; i<len; i++) {
            StringBuilder sb = new StringBuilder();
            for(int j=0; j<len; j++) {
                sb.Append(ans[i,j] + " ");
            }
            Console.WriteLine(sb.ToString());
        }

    }
    
    public int[,] CreateMahoujin4_2(int len) {
        int baseLen = ((len - 2) / 4) * 2 + 1;
        char[,] flag = new char[baseLen, baseLen];
        for(int i=0; i<=baseLen / 2; i++) {
            for(int j=0; j<baseLen; j++) {
                flag[i,j] = 'L';
            }
        }
        for(int i=baseLen/2+1; i<baseLen; i++) {
            for(int j=0; j<baseLen; j++) {
                flag[i,j] = 'U';
            }
        }
        for(int i=baseLen/2+2; i<baseLen; i++) {
            for(int j=0; j<baseLen; j++) {
                flag[i,j] = 'X';
            }
        }
        flag[baseLen / 2, baseLen / 2] = 'U';
        flag[baseLen / 2 + 1, baseLen/ 2] = 'L';
        int[,] baseMap = this.CreateMahoujinKisu(baseLen);
        List<List<int>> list = new List<List<int>>();
        int[,] ans = new int[len, len];
        for(int i=0; i<baseLen; i++) {
            list.Add(new List<int>());
            list.Add(new List<int>());
            for(int j=0; j<baseLen; j++) {
                int num = (baseMap[i,j] - 1)*4;
                if(flag[i,j] == 'L') {
                    list[list.Count - 2].Add(num + 4);
                    list[list.Count - 2].Add(num + 1);
                    list[list.Count - 1].Add(num + 2);
                    list[list.Count - 1].Add(num + 3);
                } else if(flag[i,j] == 'U') {
                    list[list.Count - 2].Add(num + 1);
                    list[list.Count - 2].Add(num + 4);
                    list[list.Count - 1].Add(num + 2);
                    list[list.Count - 1].Add(num + 3);
                } else
                {
                    list[list.Count - 2].Add(num + 1);
                    list[list.Count - 2].Add(num + 4);
                    list[list.Count - 1].Add(num + 3);
                    list[list.Count - 1].Add(num + 2);
                }
            }
        }
        for(int i=0; i<list.Count; i++) {
            for(int j=0; j<list[i].Count; j++) {
                ans[i,j] = list[i][j];
            }
        }
        return ans;
    }
    public int[,] CreateMahoujin4(int len) {
        bool[,] baseFlag = new bool[,]{
            {true, false, false, true},
            {false, true, true, false},
            {false, true, true, false},
            {true, false, false, true}
        };
        
        bool[,] flag = new bool[len, len];
        for(int i=0; i<len; i++) {
            for(int j=0; j<len; j++) {
                flag[i,j] = baseFlag[i%4, j%4];
            }
        }
        
        int[,] map1 = new int[len, len];
        int[,] map2 = new int[len, len];
        int num = 1;
        for(int i=0; i<len; i++) {
            for(int j=0; j<len; j++) {
                if(flag[i,j]) {
                    map1[i,j] = num; 
                } else
                {
                    map2[i,j] = num;
                }
                num++;
            }
        }
        
        for(int i=0; i<len; i++) {
            for(int j=0; j<len; j++) {
                if(map2[i,j] > 0) {
                    int convY = len + ((i+1) * -1);
                    int convX = len + ((j+1) * -1);
                    map1[convY, convX] = map2[i,j];
                }
            }
        }
        return map1;
    }
    public int[,] CreateMahoujinKisu(int len) {
        int posY = 0;
        int posX = len / 2;
        int[,] ans = new int[len, len];
        
        for(int i=1; i<=len * len; i++) {
            ans[posY, posX] = i;
            int newPosY = posY - 1;
            int newPosX = posX + 1;
            if(newPosY < 0) {
                newPosY = len - 1;
            }
            if(newPosX >= len) {
                newPosX = 0; 
            }
            if(ans[newPosY, newPosX] > 0) {
                posY++;
                if(posY >= len) {
                    posY = 0;
                }
            } else
            {
                posY = newPosY;
                posX = newPosX;
            }
        }
        return ans;
    }

    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"



6

 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        public static int[] GetInt(char delimiter = ' ', bool trim = false)
        {
            string inptStr = ReadLine();
            if (trim)
            {
                inptStr = inptStr.Trim();
            }
            string[] inpt = inptStr.Split(delimiter);
            int[] ret = new int[inpt.Length];
            for (int i = 0; i < inpt.Length; i++)
            {
                ret[i] = int.Parse(inpt[i]);
            }
            return ret;
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0