結果
| 問題 | No.367 ナイトの転身 | 
| コンテスト | |
| ユーザー |  紙ぺーぱー | 
| 提出日時 | 2016-04-06 01:01:29 | 
| 言語 | C#(csc) (csc 3.9.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 419 ms / 2,000 ms | 
| コード長 | 3,142 bytes | 
| コンパイル時間 | 1,198 ms | 
| コンパイル使用メモリ | 116,052 KB | 
| 実行使用メモリ | 85,816 KB | 
| 最終ジャッジ日時 | 2024-10-04 02:06:57 | 
| 合計ジャッジ時間 | 4,199 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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;
    }
}
            
            
            
        