結果

問題 No.367 ナイトの転身
ユーザー 紙ぺーぱー紙ぺーぱー
提出日時 2016-04-06 01:01:29
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 411 ms / 2,000 ms
コード長 3,142 bytes
コンパイル時間 1,128 ms
コンパイル使用メモリ 116,644 KB
実行使用メモリ 86,036 KB
最終ジャッジ日時 2024-04-14 22:13:59
合計ジャッジ時間 4,541 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
25,280 KB
testcase_01 AC 34 ms
25,412 KB
testcase_02 AC 34 ms
25,412 KB
testcase_03 AC 35 ms
27,396 KB
testcase_04 AC 34 ms
25,264 KB
testcase_05 AC 34 ms
27,448 KB
testcase_06 AC 35 ms
25,496 KB
testcase_07 AC 34 ms
27,404 KB
testcase_08 AC 34 ms
23,348 KB
testcase_09 AC 34 ms
25,348 KB
testcase_10 AC 360 ms
86,036 KB
testcase_11 AC 411 ms
81,836 KB
testcase_12 AC 188 ms
50,804 KB
testcase_13 AC 182 ms
53,104 KB
testcase_14 AC 174 ms
50,728 KB
testcase_15 AC 38 ms
27,480 KB
testcase_16 AC 166 ms
47,508 KB
testcase_17 AC 45 ms
29,292 KB
testcase_18 AC 55 ms
34,268 KB
testcase_19 AC 69 ms
35,408 KB
testcase_20 AC 45 ms
27,368 KB
testcase_21 AC 68 ms
33,224 KB
testcase_22 AC 34 ms
25,352 KB
testcase_23 AC 36 ms
27,184 KB
testcase_24 AC 35 ms
23,216 KB
testcase_25 AC 36 ms
25,288 KB
testcase_26 AC 35 ms
27,408 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