結果
問題 | No.367 ナイトの転身 |
ユーザー | mban |
提出日時 | 2017-04-28 15:48:18 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 80 ms / 2,000 ms |
コード長 | 4,023 bytes |
コンパイル時間 | 4,536 ms |
コンパイル使用メモリ | 114,496 KB |
実行使用メモリ | 26,552 KB |
最終ジャッジ日時 | 2024-09-13 17:48:07 |
合計ジャッジ時間 | 6,863 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 27 ms
24,020 KB |
testcase_01 | AC | 28 ms
23,984 KB |
testcase_02 | AC | 28 ms
24,020 KB |
testcase_03 | AC | 28 ms
23,900 KB |
testcase_04 | AC | 27 ms
23,988 KB |
testcase_05 | AC | 28 ms
21,948 KB |
testcase_06 | AC | 27 ms
24,152 KB |
testcase_07 | AC | 27 ms
24,032 KB |
testcase_08 | AC | 28 ms
25,904 KB |
testcase_09 | AC | 28 ms
23,896 KB |
testcase_10 | AC | 60 ms
25,816 KB |
testcase_11 | AC | 80 ms
25,568 KB |
testcase_12 | AC | 59 ms
24,408 KB |
testcase_13 | AC | 50 ms
24,152 KB |
testcase_14 | AC | 56 ms
26,464 KB |
testcase_15 | AC | 29 ms
24,152 KB |
testcase_16 | AC | 55 ms
26,552 KB |
testcase_17 | AC | 30 ms
23,952 KB |
testcase_18 | AC | 33 ms
26,068 KB |
testcase_19 | AC | 35 ms
24,028 KB |
testcase_20 | AC | 30 ms
24,024 KB |
testcase_21 | AC | 37 ms
26,076 KB |
testcase_22 | AC | 28 ms
26,060 KB |
testcase_23 | AC | 28 ms
23,780 KB |
testcase_24 | AC | 28 ms
26,164 KB |
testcase_25 | AC | 28 ms
26,036 KB |
testcase_26 | AC | 28 ms
26,036 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; 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; } }