結果

問題 No.34 砂漠の行商人
ユーザー noriocnorioc
提出日時 2015-10-07 23:22:41
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 376 ms / 5,000 ms
コード長 1,914 bytes
コンパイル時間 4,422 ms
コンパイル使用メモリ 105,004 KB
実行使用メモリ 31,740 KB
最終ジャッジ日時 2023-09-10 19:17:04
合計ジャッジ時間 6,274 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
23,940 KB
testcase_01 AC 62 ms
21,896 KB
testcase_02 AC 63 ms
23,880 KB
testcase_03 AC 63 ms
23,740 KB
testcase_04 AC 70 ms
21,712 KB
testcase_05 AC 79 ms
23,976 KB
testcase_06 AC 65 ms
21,848 KB
testcase_07 AC 93 ms
26,144 KB
testcase_08 AC 106 ms
28,248 KB
testcase_09 AC 175 ms
29,648 KB
testcase_10 AC 74 ms
25,848 KB
testcase_11 AC 110 ms
28,108 KB
testcase_12 AC 68 ms
21,768 KB
testcase_13 AC 376 ms
31,740 KB
testcase_14 AC 316 ms
29,632 KB
testcase_15 AC 66 ms
23,856 KB
testcase_16 AC 78 ms
25,988 KB
testcase_17 AC 65 ms
21,976 KB
testcase_18 AC 64 ms
19,800 KB
testcase_19 AC 145 ms
29,724 KB
testcase_20 AC 197 ms
29,784 KB
testcase_21 AC 65 ms
21,756 KB
testcase_22 AC 67 ms
21,848 KB
testcase_23 AC 65 ms
21,844 KB
testcase_24 AC 223 ms
25,568 KB
testcase_25 AC 77 ms
23,952 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 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;

            var dxy = new[] { 0, 1, 0, -1 };
            for (int i = 0; i < dxy.Length; i++) {
                int r = d.row + dxy[i];
                int c = d.col + dxy[3-i];
                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