結果

問題 No.34 砂漠の行商人
ユーザー noriocnorioc
提出日時 2015-11-21 11:16:22
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 553 ms / 5,000 ms
コード長 2,141 bytes
コンパイル時間 2,446 ms
コンパイル使用メモリ 109,908 KB
実行使用メモリ 31,776 KB
最終ジャッジ日時 2023-09-10 19:17:30
合計ジャッジ時間 7,095 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
19,860 KB
testcase_01 AC 61 ms
21,780 KB
testcase_02 AC 61 ms
19,640 KB
testcase_03 AC 61 ms
21,888 KB
testcase_04 AC 73 ms
26,024 KB
testcase_05 AC 85 ms
26,128 KB
testcase_06 AC 64 ms
19,764 KB
testcase_07 AC 104 ms
26,052 KB
testcase_08 AC 125 ms
26,260 KB
testcase_09 AC 232 ms
29,760 KB
testcase_10 AC 78 ms
28,192 KB
testcase_11 AC 134 ms
26,212 KB
testcase_12 AC 68 ms
23,812 KB
testcase_13 AC 553 ms
31,684 KB
testcase_14 AC 455 ms
31,776 KB
testcase_15 AC 65 ms
23,908 KB
testcase_16 AC 83 ms
26,160 KB
testcase_17 AC 63 ms
21,696 KB
testcase_18 AC 63 ms
21,852 KB
testcase_19 AC 186 ms
29,772 KB
testcase_20 AC 266 ms
27,804 KB
testcase_21 AC 65 ms
21,836 KB
testcase_22 AC 66 ms
23,920 KB
testcase_23 AC 64 ms
19,704 KB
testcase_24 AC 301 ms
27,704 KB
testcase_25 AC 82 ms
26,044 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;
using System.Diagnostics;

struct D {
    public int row, col, vitality, step;
}

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

    static IEnumerable<int[]> Next(int row, int col) {
        var ret = new int[2];
        var dxy = new[] { 0, 1, 0, -1 };
        for (int i = 0; i < dxy.Length; i++) {
            ret[0] = row + dxy[i];
            ret[1] = col + dxy[3-i];
            yield return ret;
        }
    }

    static int BFS(int n, int vitality, int srow, int scol, int grow, int gcol, int[,] g) {
        var memo = new int[n, n];
        var q = new Queue<D>();
        q.Enqueue(new D { row=srow, col=scol, vitality=vitality, step=0 });
        while (q.Count > 0) {
            var d = q.Dequeue();

            if (d.row == grow && d.col == gcol) return d.step;

            if (memo[d.row, d.col] > 0 && memo[d.row, d.col] >= d.vitality) continue;
            memo[d.row, d.col] = d.vitality;

            foreach (int[] next in Next(d.row, d.col)) {
                int r = next[0];
                int c = next[1];
                if (0 <= r && r < n && 0 <= c && c < n) {
                    int v2 = d.vitality - g[r, c];
                    if (v2 > 0) {
                        q.Enqueue(new D { row=r, col=c, vitality=v2, step=d.step+1 });
                    }
                }
            }
        }
        return -1;
    }

    static void Main() {
        var xs = ReadInts();
        int n = xs[0];
        int vitality = xs[1];
        int sx = xs[2], sy = xs[3];
        int gx = xs[4], gy = xs[5];

        var g = new int[n, n];
        for (int i = 0; i < n; i++) {
            var s = ReadInts();
            for (int j = 0; j < n; j++) {
                g[i, j] = s[j];
            }
        }

        Console.WriteLine(BFS(n, vitality, sy-1, sx-1, gy-1, gx-1, g));
    }
}
0