結果

問題 No.367 ナイトの転身
ユーザー 14番14番
提出日時 2016-05-18 20:17:39
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 688 ms / 2,000 ms
コード長 5,144 bytes
コンパイル時間 985 ms
コンパイル使用メモリ 114,952 KB
実行使用メモリ 37,376 KB
最終ジャッジ日時 2024-04-15 22:57:16
合計ジャッジ時間 4,864 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
19,200 KB
testcase_01 AC 30 ms
19,200 KB
testcase_02 AC 30 ms
19,200 KB
testcase_03 AC 30 ms
19,328 KB
testcase_04 AC 31 ms
19,328 KB
testcase_05 AC 29 ms
19,072 KB
testcase_06 AC 30 ms
19,456 KB
testcase_07 AC 33 ms
19,456 KB
testcase_08 AC 30 ms
19,200 KB
testcase_09 AC 30 ms
19,328 KB
testcase_10 AC 448 ms
33,536 KB
testcase_11 AC 688 ms
37,376 KB
testcase_12 AC 278 ms
29,440 KB
testcase_13 AC 278 ms
27,776 KB
testcase_14 AC 262 ms
27,264 KB
testcase_15 AC 40 ms
23,168 KB
testcase_16 AC 241 ms
26,752 KB
testcase_17 AC 58 ms
23,328 KB
testcase_18 AC 70 ms
23,320 KB
testcase_19 AC 98 ms
23,936 KB
testcase_20 AC 64 ms
23,312 KB
testcase_21 AC 94 ms
23,680 KB
testcase_22 AC 31 ms
19,712 KB
testcase_23 AC 35 ms
21,376 KB
testcase_24 AC 37 ms
22,400 KB
testcase_25 AC 35 ms
20,992 KB
testcase_26 AC 32 ms
19,712 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.Text;
using System.Linq;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int[] inpt = Reader.ReadLine().Split(' ').Select(a=>int.Parse(a)).ToArray();
        int H = inpt[0];
        int W = inpt[1];
        this.Map = new char[H,W];
        this.Step = new MapStatus[H,W];
        int goalX = 0;
        int goalY = 0;
        
        TaskItem firstTask = new TaskItem();
        for(int i=0; i<H; i++) {
            string str = Reader.ReadLine();
            for(int j=0; j<str.Length; j++) {
                this.Map[i,j] = str[j];
                MapStatus newItem = new MapStatus();
                if(str[j] == 'S') {
                    newItem.Bishop = 0;
                    newItem.Knight = 0;
                    firstTask.X = j;
                    firstTask.Y = i;
                    firstTask.IsKnight = true;
                } else if(str[j] == 'G') {
                    goalX = j;
                    goalY = i;
                }
                this.Step[i,j] = newItem;
            }
        }
        
        Queue<TaskItem> task = new Queue<TaskItem>();
        task.Enqueue(firstTask);
        while (task.Count > 0)
        {
            TaskItem t = task.Dequeue();
            bool isKnight = t.IsKnight;
            int step = this.Step[t.Y, t.X].Knight;
            if(!isKnight) {
                step = this.Step[t.Y, t.X].Bishop;
            }
            if(Map[t.Y, t.X] == 'R') {
                isKnight = !isKnight;
            }
            if(this.Map[t.Y, t.X] == 'G') {
                continue;
            }
            List<Pos> next = this.GetNextMoveTo(new Pos(t.X, t.Y), isKnight);
            next.ForEach((a)=>{
                if(isKnight) {
                    if(Step[a.Y, a.X].Knight > step + 1) {
                        this.Step[a.Y, a.X].Knight = step + 1;
                        TaskItem newT = new TaskItem();
                        newT.Y = a.Y;
                        newT.X = a.X;
                        newT.IsKnight = true;
                        task.Enqueue(newT);
                    }
                } else
                {
                    if(Step[a.Y, a.X].Bishop > step + 1) {
                        this.Step[a.Y, a.X].Bishop = step + 1;
                        TaskItem newT = new TaskItem();
                        newT.Y = a.Y;
                        newT.X = a.X;
                        newT.IsKnight = false;
                        task.Enqueue(newT);
                    }
                }
            });
        }
        int ans = Math.Min(Step[goalY, goalX].Bishop, Step[goalY, goalX].Knight);
        if(ans == int.MaxValue) {
            ans = -1;
        }
        Console.WriteLine(ans);
        
    }
    
    private List<Pos> GetNextMoveTo(Pos nowPos, bool isKnight) {
        Pos[] tmp;
        if(isKnight) {
            tmp = new Pos[8];
            tmp[0] = new Pos(nowPos.X + 2, nowPos.Y + 1);
            tmp[1] = new Pos(nowPos.X + 2, nowPos.Y - 1);
            tmp[2] = new Pos(nowPos.X - 2, nowPos.Y + 1);
            tmp[3] = new Pos(nowPos.X - 2, nowPos.Y - 1);
            tmp[4] = new Pos(nowPos.X + 1, nowPos.Y + 2);
            tmp[5] = new Pos(nowPos.X - 1, nowPos.Y + 2);
            tmp[6] = new Pos(nowPos.X + 1, nowPos.Y - 2);
            tmp[7] = new Pos(nowPos.X - 1, nowPos.Y - 2);
        } else
        {
            tmp = new Pos[4];
            tmp[0] = new Pos(nowPos.X + 1, nowPos.Y + 1);
            tmp[1] = new Pos(nowPos.X + 1, nowPos.Y - 1);
            tmp[2] = new Pos(nowPos.X - 1, nowPos.Y + 1);
            tmp[3] = new Pos(nowPos.X - 1, nowPos.Y - 1);
        }
        List<Pos> ret = new List<Pos>();
        
        tmp.ToList().ForEach((a)=>{
           if(a.X >= 0 && a.X < Map.GetLength(1) && a.Y >= 0 && a.Y < Map.GetLength(0)) {
               ret.Add(a);
           } 
        });
        return ret;
    }
    
    private char[,] Map;
    
    private MapStatus[,] Step;
    
    public class Pos {
        public int X;
        public int Y;
        
        public Pos(int x, int y) {
            this.X = x;
            this.Y = y;
        }
    }

    public class TaskItem {
        public int X;
        public int Y;
        
        public bool IsKnight;
    }
    
    public class MapStatus {
        public int Bishop = int.MaxValue;
        public int Knight = int.MaxValue;
        
    }    

    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"


3 3
.R.
RGR
SR.

 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0