結果

問題 No.1149 色塗りゲーム
ユーザー 👑 terry_u16terry_u16
提出日時 2020-08-07 23:03:51
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 11,672 bytes
コンパイル時間 4,077 ms
コンパイル使用メモリ 115,360 KB
実行使用メモリ 40,188 KB
平均クエリ数 19.06
最終ジャッジ日時 2023-09-24 04:54:52
合計ジャッジ時間 11,109 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
37,556 KB
testcase_01 AC 94 ms
40,024 KB
testcase_02 AC 87 ms
37,440 KB
testcase_03 AC 88 ms
37,396 KB
testcase_04 AC 92 ms
39,544 KB
testcase_05 AC 91 ms
39,980 KB
testcase_06 AC 89 ms
38,004 KB
testcase_07 AC 89 ms
36,160 KB
testcase_08 AC 91 ms
37,996 KB
testcase_09 AC 91 ms
37,772 KB
testcase_10 AC 91 ms
39,716 KB
testcase_11 AC 91 ms
37,736 KB
testcase_12 AC 91 ms
39,372 KB
testcase_13 AC 93 ms
37,428 KB
testcase_14 AC 91 ms
37,280 KB
testcase_15 AC 94 ms
37,556 KB
testcase_16 AC 91 ms
39,580 KB
testcase_17 AC 91 ms
37,476 KB
testcase_18 AC 93 ms
37,624 KB
testcase_19 AC 90 ms
37,544 KB
testcase_20 AC 90 ms
37,972 KB
testcase_21 AC 90 ms
38,072 KB
testcase_22 AC 92 ms
40,188 KB
testcase_23 AC 96 ms
38,012 KB
testcase_24 AC 99 ms
37,908 KB
testcase_25 AC 91 ms
38,168 KB
testcase_26 AC 92 ms
37,840 KB
testcase_27 AC 94 ms
37,956 KB
testcase_28 AC 93 ms
37,704 KB
testcase_29 AC 97 ms
40,172 KB
testcase_30 AC 127 ms
37,448 KB
testcase_31 AC 121 ms
36,020 KB
testcase_32 AC 123 ms
37,560 KB
testcase_33 AC 130 ms
39,752 KB
testcase_34 AC 128 ms
37,624 KB
testcase_35 AC 128 ms
39,600 KB
testcase_36 AC 140 ms
37,936 KB
testcase_37 AC 137 ms
37,704 KB
testcase_38 AC 132 ms
35,612 KB
testcase_39 AC 137 ms
38,112 KB
testcase_40 AC 138 ms
37,960 KB
testcase_41 AC 160 ms
37,916 KB
testcase_42 AC 158 ms
38,208 KB
testcase_43 AC 141 ms
37,852 KB
testcase_44 AC 146 ms
38,120 KB
testcase_45 AC 141 ms
39,596 KB
testcase_46 AC 146 ms
39,992 KB
testcase_47 AC 156 ms
35,936 KB
testcase_48 AC 155 ms
37,192 KB
testcase_49 AC 153 ms
35,944 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.IO;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using YukicoderContest260.Extensions;
using YukicoderContest260.Questions;
using System.Diagnostics;

namespace YukicoderContest260.Questions
{
    public class QuestionA : AtCoderQuestionBase
    {
        int[] _grundy;

        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var n = inputStream.ReadInt();
            var painted = new bool[n];
            _grundy = GetGrundy(n);

            while (true)
            {
                var (x, k) = Think(painted);

                yield return $"{k} {x + 1}";
                ShowBoard(painted);
                ShowGrundy(painted);

                // 敵の処理
                var t = inputStream.ReadInt();
                if (t == 3)
                {
                    var (enemyK, enemyX) = inputStream.ReadValue<int, int>();
                    enemyX--;
                    Paint(painted, enemyX, enemyK);
                    ShowBoard(painted);
                    ShowGrundy(painted);
                }
                else
                {
                    break;
                }
            }
        }

        private int[] GetGrundy(int n)
        {
            var grundies = Enumerable.Repeat(-1, n + 1).ToArray();
            grundies[0] = 0;
            grundies[1] = 1;

            for (int length = 2; length < grundies.Length; length++)
            {
                var nextGrundies = new bool[grundies.Length];
                for (int x = 0; x < n; x++)
                {
                    for (int k = 1; k <= 2; k++)
                    {
                        if (x + k > length)
                        {
                            continue;
                        }

                        var left = x;
                        var right = length - (x + k);
                        nextGrundies[grundies[left] ^ grundies[right]] = true;
                    }
                }
                grundies[length] = GetMex(nextGrundies);
            }
            return grundies;
        }

        private int GetMex(bool[] nextGrundies)
        {
            for (int i = 0; i < nextGrundies.Length; i++)
            {
                if (!nextGrundies[i])
                {
                    return i;
                }
            }
            return nextGrundies.Length;
        }

        private (int x, int k) Think(bool[] painted)
        {
            for (int x = 0; x < painted.Length; x++)
            {
                for (int k = 1; k <= 2; k++)
                {
                    if (x + k > painted.Length)
                    {
                        continue;
                    }

                    if (CanPaint(painted, x, k))
                    {
                        Paint(painted, x, k);
                        if (GetGrundy(painted) == 0)
                        {
                            return (x, k);
                        }
                        else
                        {
                            UnPaint(painted, x, k);
                        }
                    }
                }
            }

            return (-1, -1);
        }

        private bool CanPaint(bool[] painted, int x, int k)
        {
            var ok = true;
            for (int i = 0; i < k; i++)
            {
                ok &= !painted[x + i];
            }
            return ok;
        }

        private int GetGrundy(bool[] painted)
        {
            var current = 0;
            var grundy = 0;

            for (int i = 0; i < painted.Length; i++)
            {
                if (painted[i])
                {
                    grundy ^= _grundy[current];
                    current = 0;
                }
                else
                {
                    current++;
                }
            }

            return grundy ^ _grundy[current];
        }

        private void Paint(bool[] painted, int x, int k)
        {
            for (int i = 0; i < k; i++)
            {
                painted[x + i] = true;
            }
        }

        private void UnPaint(bool[] painted, int x, int k)
        {
            for (int i = 0; i < k; i++)
            {
                painted[x + i] = false;
            }
        }

        [Conditional("DEBUG")]
        private void ShowBoard(bool[] painted)
        {
            Console.WriteLine(painted.Select(b => b ? '■' : '□').Join());
        }

        [Conditional("DEBUG")]
        private void ShowGrundy(bool[] painted)
        {
            Console.WriteLine($"Grundy: {GetGrundy(painted)}");
        }
    }
}

namespace YukicoderContest260
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionA();
            var answers = question.Solve(Console.In);

            //var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            //Console.SetOut(writer);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
            Console.Out.Flush();
        }
    }
}

#region Base Class

namespace YukicoderContest260.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }
}

#endregion

#region Extensions

namespace YukicoderContest260.Extensions
{
    public static class StringExtensions
    {
        public static string Join<T>(this IEnumerable<T> source) => string.Concat(source);
        public static string Join<T>(this IEnumerable<T> source, char separator) => string.Join(separator, source);
        public static string Join<T>(this IEnumerable<T> source, string separator) => string.Join(separator, source);
    }

    public static class TextReaderExtensions
    {
        public static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        public static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        public static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        public static string ReadString(this TextReader reader) => reader.ReadLine();

        public static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        public static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        public static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        public static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);

        // Supports primitive type only.
        public static T1 ReadValue<T1>(this TextReader reader) => (T1)Convert.ChangeType(reader.ReadLine(), typeof(T1));

        public static (T1, T2) ReadValue<T1, T2>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            return (v1, v2);
        }

        public static (T1, T2, T3) ReadValue<T1, T2, T3>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            return (v1, v2, v3);
        }

        public static (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            return (v1, v2, v3, v4);
        }

        public static (T1, T2, T3, T4, T5) ReadValue<T1, T2, T3, T4, T5>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            return (v1, v2, v3, v4, v5);
        }

        public static (T1, T2, T3, T4, T5, T6) ReadValue<T1, T2, T3, T4, T5, T6>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            return (v1, v2, v3, v4, v5, v6);
        }

        public static (T1, T2, T3, T4, T5, T6, T7) ReadValue<T1, T2, T3, T4, T5, T6, T7>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7));
            return (v1, v2, v3, v4, v5, v6, v7);
        }

        public static (T1, T2, T3, T4, T5, T6, T7, T8) ReadValue<T1, T2, T3, T4, T5, T6, T7, T8>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7));
            var v8 = (T8)Convert.ChangeType(inputs[7], typeof(T8));
            return (v1, v2, v3, v4, v5, v6, v7, v8);
        }
    }
}

#endregion
0