結果
問題 | No.367 ナイトの転身 |
ユーザー | 14番 |
提出日時 | 2016-05-18 20:17:39 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 574 ms / 2,000 ms |
コード長 | 5,144 bytes |
コンパイル時間 | 856 ms |
コンパイル使用メモリ | 116,004 KB |
実行使用メモリ | 45,372 KB |
最終ジャッジ日時 | 2024-10-06 05:37:32 |
合計ジャッジ時間 | 4,564 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 29 ms
24,992 KB |
testcase_01 | AC | 26 ms
23,220 KB |
testcase_02 | AC | 27 ms
26,980 KB |
testcase_03 | AC | 28 ms
25,116 KB |
testcase_04 | AC | 27 ms
25,312 KB |
testcase_05 | AC | 26 ms
25,184 KB |
testcase_06 | AC | 27 ms
25,140 KB |
testcase_07 | AC | 28 ms
23,604 KB |
testcase_08 | AC | 28 ms
27,224 KB |
testcase_09 | AC | 28 ms
27,228 KB |
testcase_10 | AC | 393 ms
41,268 KB |
testcase_11 | AC | 574 ms
45,372 KB |
testcase_12 | AC | 245 ms
37,276 KB |
testcase_13 | AC | 241 ms
36,016 KB |
testcase_14 | AC | 232 ms
35,448 KB |
testcase_15 | AC | 37 ms
31,112 KB |
testcase_16 | AC | 207 ms
32,796 KB |
testcase_17 | AC | 52 ms
31,336 KB |
testcase_18 | AC | 60 ms
31,260 KB |
testcase_19 | AC | 84 ms
32,000 KB |
testcase_20 | AC | 58 ms
29,520 KB |
testcase_21 | AC | 83 ms
31,860 KB |
testcase_22 | AC | 28 ms
24,992 KB |
testcase_23 | AC | 32 ms
26,872 KB |
testcase_24 | AC | 31 ms
25,208 KB |
testcase_25 | AC | 30 ms
25,116 KB |
testcase_26 | AC | 28 ms
25,244 KB |
コンパイルメッセージ
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.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(); } }