結果

問題 No.424 立体迷路
ユーザー noriocnorioc
提出日時 2016-09-22 22:49:48
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 2,253 bytes
コンパイル時間 2,633 ms
コンパイル使用メモリ 110,304 KB
実行使用メモリ 24,128 KB
最終ジャッジ日時 2023-09-18 16:47:43
合計ジャッジ時間 5,263 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
23,968 KB
testcase_01 AC 62 ms
24,016 KB
testcase_02 AC 63 ms
23,944 KB
testcase_03 AC 61 ms
19,868 KB
testcase_04 AC 61 ms
19,884 KB
testcase_05 AC 63 ms
22,112 KB
testcase_06 AC 62 ms
23,996 KB
testcase_07 AC 62 ms
24,040 KB
testcase_08 AC 63 ms
24,040 KB
testcase_09 AC 62 ms
21,816 KB
testcase_10 AC 62 ms
23,932 KB
testcase_11 AC 63 ms
23,980 KB
testcase_12 AC 62 ms
22,044 KB
testcase_13 AC 63 ms
22,004 KB
testcase_14 AC 62 ms
21,880 KB
testcase_15 AC 63 ms
21,992 KB
testcase_16 AC 63 ms
24,036 KB
testcase_17 AC 64 ms
22,020 KB
testcase_18 AC 64 ms
22,004 KB
testcase_19 AC 63 ms
24,068 KB
testcase_20 AC 63 ms
21,960 KB
testcase_21 AC 64 ms
24,128 KB
testcase_22 AC 64 ms
22,012 KB
testcase_23 AC 63 ms
23,896 KB
testcase_24 AC 65 ms
24,004 KB
testcase_25 AC 66 ms
24,052 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.Linq;

class Program {
    static int ReadInt() { return int.Parse(Console.ReadLine()); }
    static int[] ReadInts() { return Console.ReadLine().Split().Select(int.Parse).ToArray(); }
    static string[] ReadStrings() { return Console.ReadLine().Split(); }

    struct D {
        public int r;
        public int c;
    }

    static bool Calc(int srow, int scol, int grow, int gcol, int[,] g) {
        int rows = g.GetLength(0);
        int cols = g.GetLength(1);

        Func<int, int, bool> inRange = (r, c) => 0 <= r && r < rows && 0 <= c && c < cols;

        var delta = new[,] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };

        var used = new bool[rows, cols];
        var q = new Queue<D>();
        q.Enqueue(new D { r = srow-1, c = scol-1 });
        while (q.Count > 0) {
            var d = q.Dequeue();

            if (d.r == grow-1 && d.c == gcol-1) return true;
            if (used[d.r, d.c]) continue;
            used[d.r, d.c] = true;

            for (int i = 0; i < 4; i++) {
                int r = d.r + delta[i, 0];
                int c = d.c + delta[i, 1];
                if (inRange(r, c) && Math.Abs(g[r, c] - g[d.r, d.c]) <= 1) {
                    q.Enqueue(new D { r = r, c = c });
                }

                // 2 つ先のブロックに移動する
                if (inRange(r, c) && g[d.r, d.c] > g[r, c]) {
                    int r2 = d.r + delta[i, 0] * 2;
                    int c2 = d.c + delta[i, 1] * 2;
                    if (inRange(r2, c2) && g[r2, c2] == g[d.r, d.c]) {
                        q.Enqueue(new D { r = r2, c = c2 });
                    }
                }
            }
        }
        return false;
    }

    static void Main() {
        var hw = ReadInts();
        var xs = ReadInts();

        int rows = hw[0], cols = hw[1];
        int srow = xs[0], scol = xs[1], grow = xs[2], gcol = xs[3];
        var g = new int[rows, cols];
        for (int i = 0; i < rows; i++) {
            var s = Console.ReadLine();
            for (int j = 0; j < cols; j++) {
                g[i, j] = s[j] - '0';
            }
        }

        Console.WriteLine(Calc(srow, scol, grow, gcol, g) ? "YES" : "NO");
    }
}
0