結果

問題 No.367 ナイトの転身
ユーザー 紙ぺーぱー紙ぺーぱー
提出日時 2016-04-06 01:01:29
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 454 ms / 2,000 ms
コード長 3,142 bytes
コンパイル時間 5,297 ms
コンパイル使用メモリ 114,908 KB
実行使用メモリ 83,068 KB
最終ジャッジ日時 2023-07-27 09:32:55
合計ジャッジ時間 8,503 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
24,016 KB
testcase_01 AC 70 ms
24,032 KB
testcase_02 AC 72 ms
24,048 KB
testcase_03 AC 70 ms
20,068 KB
testcase_04 AC 71 ms
24,024 KB
testcase_05 AC 71 ms
21,988 KB
testcase_06 AC 71 ms
24,088 KB
testcase_07 AC 71 ms
22,036 KB
testcase_08 AC 70 ms
21,976 KB
testcase_09 AC 70 ms
21,988 KB
testcase_10 AC 401 ms
83,068 KB
testcase_11 AC 454 ms
83,004 KB
testcase_12 AC 226 ms
48,224 KB
testcase_13 AC 218 ms
50,248 KB
testcase_14 AC 211 ms
48,036 KB
testcase_15 AC 74 ms
22,076 KB
testcase_16 AC 203 ms
48,600 KB
testcase_17 AC 82 ms
24,004 KB
testcase_18 AC 93 ms
31,216 KB
testcase_19 AC 105 ms
32,464 KB
testcase_20 AC 82 ms
24,168 KB
testcase_21 AC 107 ms
30,372 KB
testcase_22 AC 70 ms
22,124 KB
testcase_23 AC 73 ms
24,004 KB
testcase_24 AC 74 ms
21,932 KB
testcase_25 AC 72 ms
23,980 KB
testcase_26 AC 71 ms
24,040 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.Linq;
public class otaku
{
    static void Main()
    {
        var hw = Console.ReadLine().Split(' ').Select(int.Parse).ToArray().ValidateArray(x => 1 <= x && x <= 500);
        var h = hw[0];
        var w = hw[1];
        var map = new string[h];
        var type = "SGR.";
        for (int i = 0; i < h; i++)
        {
            map[i] = Console.ReadLine().Validate(x => x.Length == w);
            map[i].ToCharArray().ValidateArray(x => type.Contains(x));
        }
        map.SelectMany(x=>x).Count(x=>x=='G').Validate(x=>x==1);
        map.SelectMany(x=>x).Count(x=>x=='S').Validate(x=>x==1);
        var G = new List<int>[h * w * 2 + 1];
        for(int i=0;i<=h*w*2;i++)G[i]=new List<int>();
        var D = Enumerable.Repeat(1000000000, h * w * 2 + 1).ToArray();
        var q = new Queue<int>();
        for (int i = 0; i < h; i++)
        {
            for (int j = 0; j < w; j++)
            {
                if (map[i][j] == 'S') { q.Enqueue(i * w + j); D[i * w + j] = 0; }
                if (map[i][j] == 'G') { G[i * w + j].Add(h * w * 2); G[i * w + j + h * w].Add(h * w * 2); }
                var from = i * w + j;
                for (int dy = -2; dy <= 2; dy++)
                    for (int dx = -2; dx <= 2; dx++)
                    {
                        if (Math.Abs(dy) == Math.Abs(dx)) continue;
                        if(dy==0||dx==0)continue;
                        var ny = i + dy;
                        var nx = j + dx;
                        if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
                        var next = ny * w + nx;
                        if (map[ny][nx] == 'R') next += w * h;
                        G[from].Add(next);
                    }
                from += w * h;
                for (int dy = -1; dy <= 1; dy += 2)
                    for (int dx = -1; dx <= 1; dx += 2)
                    {
                        var ny = i + dy;
                        var nx = j + dx;
                        if (ny < 0 || ny >= h || nx < 0 || nx >= w) continue;
                        var next = ny * w + nx;
                        if (map[ny][nx] != 'R') next += w * h;
                        G[from].Add(next);
                    }
            }
        }
        while (q.Any())
        {
            var p = q.Dequeue();
            foreach (var to in G[p])
            {
                if (D[to] > D[p] + 1)
                {
                    D[to] = D[p] + 1;
                    q.Enqueue(to);
                }
            }
        }
        if (D[h * w * 2] >= 1000000000)
            Console.WriteLine(-1);
        else Console.WriteLine(D[h * w * 2] - 1);
    }
}

static public class Validator
{
    static public T Validate<T>(this T input, Func<T, bool> f)
    {
        if (!f(input))
            throw new Exception("invalid input");
        return input;
    }
    static public T[] ValidateArray<T>(this T[] input, Func<T, bool> f)
    {
        foreach (var x in input)
            if (!f(x))
                throw new Exception("invalid input");
        return input;
    }
}
0