Submission #39262599


Source Code Expand

using System;
using System.Collections.Generic;
using System.Linq;

namespace xxx
{
    internal class Program
    {
        static void Main(string[] args)
        {
            AL.SetCase();
            var (N, M) = AL.ReadInt2();
            var S = new List<List<int>>();
            S.Add(new List<int>());//S[0] dummy
            for (int i = 0; i < N; i++)
            {
                var s = new List<int>();
                var ics = AL.ReadIntChars();
                for (int j = 0; j < ics.Length; j++)
                {
                    if (ics[j] ==1)
                    {
                        s.Add(j+1);
                    }
                }
                S.Add(s);
            }

            // スタートからのコストを計算
            var costFromStart = Enumerable.Repeat<int>(1000000, N+1).ToArray();
            costFromStart[1] = 0;
            for (int i = 1; i <= N; i++)
            {
                var s = S[i];
                var cost = costFromStart[i] + 1;
                foreach (var span in s)
                {
                    var dst = i + span;
                    if (dst > N) continue;
                    costFromStart[dst] = Math.Min(costFromStart[dst], cost);
                }
            }

            // ゴールからのコストを計算
            var costFromGoal = Enumerable.Repeat<int>(1000000, N + 1).ToArray();
            costFromGoal[N] = 0;
            for (int i = N - 1; i >= 1; i--)
            {
                var s = S[i];
                foreach (var span in s)
                {
                    var dst = i + span;
                    if (dst > N) continue;
                    costFromGoal[i] = Math.Min(costFromGoal[dst]+1, costFromGoal[i]);
                }
            }

            var anss = new List<int>();
            for (int k = 2; k < N; k++)
            {
                int mincost = 1000000;
                for (int start = k + 1 - M; start < k; start++)
                {
                    if (start < 1) continue;
                    var s = S[start];
                    foreach (var span in s)
                    {
                        var dst = start + span;
                        if (dst <= k) continue;
                        if (dst > N) continue;
                        int cost = costFromStart[start] + costFromGoal[dst] + 1;
                        if (cost < mincost)
                        {
                            mincost = cost;
                        }
                    }

                }
                if (mincost >= 1000000)
                {
                    mincost = -1;
                }
                anss.Add(mincost);
            }
            Console.WriteLine(string.Join(" ", anss.Select(a => a.ToString())));
        }
    }
    static class AL
    {
        public static void SetCase()
        {
#if DEBUG
            Console.WriteLine("Case?");
            var name = Console.ReadLine();
            if (string.IsNullOrWhiteSpace(name))
            {
                Console.WriteLine("Using stdin");
                return;
            }
            var exStdIn = new System.IO.StreamReader($"../../../{name}.txt");
            System.Console.SetIn(exStdIn);
#endif
        }
        public static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }

        public static int[] ReadIntChars()
        {
            // 102 => int[]{1, 0, 2}
            return Console.ReadLine().ToCharArray().Select(c => int.Parse(c.ToString())).ToArray();
        }


        public static int[] ReadIntArray()
        {
            // 12 34 56 => int[]{12, 34, 56}
            return Console.ReadLine().Split().Select(int.Parse).ToArray();
        }

        public static (int a, int b) ReadInt2()
        {
            var ia = ReadIntArray();
            if (ia.Length != 2)
            {
                throw new InvalidCastException();
            }
            return (ia[0], ia[1]);
        }
        public static (int a, int b, int c) ReadInt3()
        {
            var ia = ReadIntArray();
            if (ia.Length != 3)
            {
                throw new InvalidCastException();
            }
            return (ia[0], ia[1], ia[2]);
        }
    }
    public class DefaultDictionary<TKey, TValue> : Dictionary<TKey, TValue> where TValue : new()
    {
        public new TValue this[TKey key]
        {
            get
            {
                if (!TryGetValue(key, out TValue val))
                {
                    val = new TValue();
                    Add(key, val);
                }
                return val;
            }
            set { base[key] = value; }
        }
    }
}

Submission Info

Submission Time
Task F - Teleporter and Closed off
User bugtori
Language C# (.NET Core 3.1.201)
Score 500
Code Size 4878 Byte
Status AC
Exec Time 304 ms
Memory 58704 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 50
Set Name Test Cases
Sample example_00.txt, example_01.txt
All colored_00.txt, colored_01.txt, colored_02.txt, colored_03.txt, colored_04.txt, colored_05.txt, colored_06.txt, colored_07.txt, colored_08.txt, colored_09.txt, colored_10.txt, colored_11.txt, colored_12.txt, colored_13.txt, colored_14.txt, colored_15.txt, colored_16.txt, colored_17.txt, colored_18.txt, colored_19.txt, colored_20.txt, colored_21.txt, colored_22.txt, colored_23.txt, colored_24.txt, colored_25.txt, example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt
Case Name Status Exec Time Memory
colored_00.txt AC 245 ms 54520 KiB
colored_01.txt AC 265 ms 54920 KiB
colored_02.txt AC 264 ms 55172 KiB
colored_03.txt AC 247 ms 55072 KiB
colored_04.txt AC 263 ms 54736 KiB
colored_05.txt AC 277 ms 56572 KiB
colored_06.txt AC 242 ms 54004 KiB
colored_07.txt AC 241 ms 54760 KiB
colored_08.txt AC 262 ms 55224 KiB
colored_09.txt AC 249 ms 53768 KiB
colored_10.txt AC 231 ms 55196 KiB
colored_11.txt AC 277 ms 54808 KiB
colored_12.txt AC 236 ms 54072 KiB
colored_13.txt AC 239 ms 54640 KiB
colored_14.txt AC 286 ms 56900 KiB
colored_15.txt AC 222 ms 54224 KiB
colored_16.txt AC 243 ms 54800 KiB
colored_17.txt AC 251 ms 55112 KiB
colored_18.txt AC 240 ms 53928 KiB
colored_19.txt AC 254 ms 54712 KiB
colored_20.txt AC 291 ms 57940 KiB
colored_21.txt AC 238 ms 54212 KiB
colored_22.txt AC 261 ms 54892 KiB
colored_23.txt AC 260 ms 55000 KiB
colored_24.txt AC 259 ms 53820 KiB
colored_25.txt AC 304 ms 57868 KiB
example_00.txt AC 83 ms 28052 KiB
example_01.txt AC 92 ms 28460 KiB
hand_00.txt AC 297 ms 58432 KiB
hand_01.txt AC 205 ms 51976 KiB
hand_02.txt AC 211 ms 54064 KiB
hand_03.txt AC 225 ms 54160 KiB
hand_04.txt AC 282 ms 58704 KiB
hand_05.txt AC 164 ms 51064 KiB
hand_06.txt AC 225 ms 54320 KiB
hand_07.txt AC 217 ms 54004 KiB
random_00.txt AC 253 ms 55136 KiB
random_01.txt AC 275 ms 57536 KiB
random_02.txt AC 90 ms 28052 KiB
random_03.txt AC 279 ms 57944 KiB
random_04.txt AC 265 ms 55404 KiB
random_05.txt AC 84 ms 28124 KiB
random_06.txt AC 272 ms 57072 KiB
random_07.txt AC 281 ms 56880 KiB
random_08.txt AC 82 ms 28464 KiB
random_09.txt AC 233 ms 54012 KiB
random_10.txt AC 250 ms 54588 KiB
random_11.txt AC 90 ms 27940 KiB
random_12.txt AC 230 ms 53648 KiB
random_13.txt AC 215 ms 53776 KiB