結果

問題 No.367 ナイトの転身
ユーザー mbanmban
提出日時 2017-04-28 15:48:18
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 109 ms / 2,000 ms
コード長 4,023 bytes
コンパイル時間 4,033 ms
コンパイル使用メモリ 109,080 KB
実行使用メモリ 24,932 KB
最終ジャッジ日時 2023-10-11 18:53:38
合計ジャッジ時間 7,175 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,832 KB
testcase_01 AC 57 ms
20,796 KB
testcase_02 AC 59 ms
20,864 KB
testcase_03 AC 58 ms
18,852 KB
testcase_04 AC 59 ms
22,940 KB
testcase_05 AC 58 ms
22,716 KB
testcase_06 AC 58 ms
18,808 KB
testcase_07 AC 59 ms
20,784 KB
testcase_08 AC 58 ms
20,728 KB
testcase_09 AC 59 ms
20,684 KB
testcase_10 AC 90 ms
24,696 KB
testcase_11 AC 109 ms
24,932 KB
testcase_12 AC 91 ms
23,188 KB
testcase_13 AC 82 ms
23,220 KB
testcase_14 AC 86 ms
23,148 KB
testcase_15 AC 60 ms
20,820 KB
testcase_16 AC 86 ms
20,952 KB
testcase_17 AC 62 ms
20,928 KB
testcase_18 AC 66 ms
20,720 KB
testcase_19 AC 67 ms
20,888 KB
testcase_20 AC 62 ms
22,844 KB
testcase_21 AC 67 ms
20,792 KB
testcase_22 AC 58 ms
20,764 KB
testcase_23 AC 59 ms
20,820 KB
testcase_24 AC 60 ms
22,792 KB
testcase_25 AC 63 ms
20,800 KB
testcase_26 AC 58 ms
20,752 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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;

public class Program
{
    static private int H, W;
    static private string[] S;

    static int[] KnightNextX = new int[] { -2, -2, -1, -1, 1, 1, 2, 2 };
    static int[] KnightNextY = new int[] { -1, 1, -2, 2, -2, 2, -1, 1 };
    static int[] BishopNextX = new int[] { -1, -1, 1, 1 };
    static int[] BishopNextY = new int[] { 1, -1, 1, -1 };
    static int Sy, Sx, Gx, Gy;

    static void Scan()
    {
        string[] line = Console.ReadLine().Split(' ');
        H = int.Parse(line[0]);
        W = int.Parse(line[1]);
        S = new string[H];
        for (int i = 0; i < H; i++)
        {
            S[i] = Console.ReadLine();
        }
    }

    public static void Main(string[] args)
    {
        Scan();

        int[,] K = new int[H, W];
        int[,] B = new int[H, W];

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                K[i, j] = int.MaxValue;
                B[i, j] = int.MaxValue;
            }
        }

        SearchSG();

        K[Sy, Sx] = 0;

        var q = new Queue<P>();
        q.Enqueue(new P(Sy, Sx, 1));

        while (q.Count > 0)
        {
            var p = q.Dequeue();

            if (p.Type == 1)
            {
                for (int i = 0; i < 8; i++)
                {
                    int nextY = p.Y + KnightNextY[i];
                    int nextX = p.X + KnightNextX[i];
                    if (nextX < 0 || nextY < 0) continue;
                    if (nextX >= W || nextY >= H) continue;

                    if (S[nextY][nextX] == 'R')
                    {
                        if (B[nextY, nextX] != int.MaxValue) continue;
                        q.Enqueue(new P(nextY, nextX, -1));
                        B[nextY, nextX] = K[p.Y, p.X] + 1;
                    }
                    else
                    {
                        if (K[nextY, nextX] != int.MaxValue) continue;
                        q.Enqueue(new P(nextY, nextX, 1));
                        K[nextY, nextX] = K[p.Y, p.X] + 1;
                    }
                }
            }
            else
            {
                for (int i = 0; i < 4; i++)
                {
                    int nextY = p.Y + BishopNextY[i];
                    int nextX = p.X + BishopNextX[i];
                    if (nextX < 0 || nextY < 0) continue;
                    if (nextX >= W || nextY >= H) continue;

                    if (S[nextY][nextX] == 'R')
                    {
                        if (K[nextY, nextX] != int.MaxValue) continue;
                        K[nextY, nextX] = B[p.Y, p.X] + 1;
                        q.Enqueue(new P(nextY, nextX, 1));
                    }
                    else
                    {
                        if (B[nextY, nextX] != int.MaxValue) continue;
                        q.Enqueue(new P(nextY, nextX, -1));
                        B[nextY, nextX] = B[p.Y, p.X] + 1;
                    }
                }
            }
        }

        if(K[Gy,Gx] == int.MaxValue&&B[Gy,Gx] == int.MaxValue)
        {
            Console.WriteLine(-1);
            return;
        }
        else
        {
            Console.WriteLine(Math.Min(K[Gy, Gx], B[Gy, Gx]));
        }
    }

    private static void SearchSG()
    {
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (S[i][j] == 'S')
                {
                    Sy = i;
                    Sx = j;
                }

                if(S[i][j] == 'G')
                {
                    Gy = i;
                    Gx = j;
                }
            }
        }
    }
}

struct P
{
    public int Y, X;
    //1 K, -1 B
    public int Type;
    public P(int y, int x, int type)
    {
        Y = y;
        X = x;
        Type = type;
    }
}

0