結果

問題 No.367 ナイトの転身
ユーザー 古寺いろは古寺いろは
提出日時 2016-05-13 18:51:24
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 3,368 bytes
コンパイル時間 1,024 ms
コンパイル使用メモリ 108,032 KB
実行使用メモリ 24,064 KB
最終ジャッジ日時 2024-04-15 16:27:31
合計ジャッジ時間 2,718 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
17,920 KB
testcase_01 AC 25 ms
17,920 KB
testcase_02 AC 25 ms
17,920 KB
testcase_03 AC 26 ms
17,792 KB
testcase_04 AC 25 ms
17,920 KB
testcase_05 AC 25 ms
17,792 KB
testcase_06 AC 27 ms
17,664 KB
testcase_07 AC 26 ms
17,664 KB
testcase_08 AC 26 ms
17,792 KB
testcase_09 AC 26 ms
17,920 KB
testcase_10 AC 86 ms
24,064 KB
testcase_11 AC 115 ms
24,064 KB
testcase_12 AC 36 ms
19,200 KB
testcase_13 AC 37 ms
19,584 KB
testcase_14 AC 54 ms
22,016 KB
testcase_15 AC 27 ms
18,048 KB
testcase_16 AC 55 ms
22,024 KB
testcase_17 AC 29 ms
18,432 KB
testcase_18 AC 33 ms
18,688 KB
testcase_19 AC 31 ms
18,640 KB
testcase_20 AC 27 ms
18,032 KB
testcase_21 AC 31 ms
18,736 KB
testcase_22 AC 25 ms
17,920 KB
testcase_23 AC 25 ms
17,920 KB
testcase_24 AC 26 ms
17,792 KB
testcase_25 AC 26 ms
18,048 KB
testcase_26 AC 27 ms
17,792 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.Linq;
using System.IO;

class Iroha
{
    public Iroha() { }
    public static int Main()
    {
        new Iroha().calc();
        return 0;
    }

    Scanner cin;

    int H, W;
    string[] board;
    void calc()
    {
        cin = new Scanner();
        H = cin.nextInt();
        W = cin.nextInt();
        board = new string[H];
        for (int i = 0; i < H; i++)
        {
            board[i] = cin.next();
        }

        int fx = 0, fy = 0;
        int gx = 0, gy = 0;
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                if (board[i][j] == 'S')
                {
                    fx = j;
                    fy = i;
                }
                if (board[i][j] == 'G')
                {
                    gx = j;
                    gy = i;
                }
            }
        }


        int[] vy = { 1, 1, -1, -1 };
        int[] vx = { 1, -1, 1, -1 };

        int[] vy2 = { 2, 2, 1, 1, -1, -1, -2, -2 };
        int[] vx2 = { 1, -1, 2, -2, 2, -2, 1, -1 };

        int MAX = 99999999;
        int[,,] dist = new int[H, W, 2];
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                for (int k = 0; k < 2; k++)
                {
                    dist[i, j, k] = MAX;
                }
            }
        }
        Queue<Tuple<int, int, int>> q = new Queue<Tuple<int, int, int>>();
        dist[fy, fx, 0] = 0;
        q.Enqueue(Tuple.Create(fy, fx, 0));
        while (q.Count > 0)
        {
            var now = q.Dequeue();
            int y = now.Item1;
            int x = now.Item2;
            int z = now.Item3;

            int[] v1, v2;
            if (z == 0)
            {
                v1 = vy2; v2 = vx2;
            }
            else
            {
                v1 = vy; v2 = vx;
            }
            for (int k = 0; k < v1.Length; k++)
            {
                int ny = y + v1[k];
                int nx = x + v2[k];
                int nz = z;
                if (!inside(ny, nx)) continue;
                if (board[ny][nx] == 'R') nz ^= 1;
                if (dist[ny, nx, nz] != MAX) continue;

                dist[ny, nx, nz] = dist[y, x, z] + 1;
                if (ny == gy && nx == gx)
                {
                    Console.WriteLine(dist[ny, nx, nz]);
                    return;
                }

                q.Enqueue(Tuple.Create(ny, nx, nz));
            }
        }
        Console.WriteLine(-1);
    }

    bool inside(int y, int x)
    {
        return y >= 0 && x >= 0 && y < H && x < W;
    } 
}

class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        i = 0;
        return next();
    }

    public int nextInt()
    {
        return int.Parse(next());
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }

}
0