結果

問題 No.34 砂漠の行商人
ユーザー noriocnorioc
提出日時 2015-10-06 20:11:37
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 765 ms / 5,000 ms
コード長 2,120 bytes
コンパイル時間 3,896 ms
コンパイル使用メモリ 105,180 KB
実行使用メモリ 57,524 KB
最終ジャッジ日時 2023-09-10 19:16:55
合計ジャッジ時間 8,211 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
21,836 KB
testcase_01 AC 62 ms
21,812 KB
testcase_02 AC 62 ms
21,792 KB
testcase_03 AC 62 ms
21,816 KB
testcase_04 AC 85 ms
26,760 KB
testcase_05 AC 87 ms
26,832 KB
testcase_06 AC 65 ms
22,020 KB
testcase_07 AC 116 ms
27,796 KB
testcase_08 AC 126 ms
30,784 KB
testcase_09 AC 246 ms
45,832 KB
testcase_10 AC 88 ms
26,808 KB
testcase_11 AC 101 ms
26,412 KB
testcase_12 AC 69 ms
23,900 KB
testcase_13 AC 765 ms
57,524 KB
testcase_14 AC 514 ms
51,168 KB
testcase_15 AC 64 ms
21,952 KB
testcase_16 AC 89 ms
29,080 KB
testcase_17 AC 63 ms
22,060 KB
testcase_18 AC 64 ms
21,952 KB
testcase_19 AC 205 ms
45,604 KB
testcase_20 AC 290 ms
45,564 KB
testcase_21 AC 66 ms
23,948 KB
testcase_22 AC 66 ms
21,932 KB
testcase_23 AC 64 ms
21,912 KB
testcase_24 AC 338 ms
47,404 KB
testcase_25 AC 78 ms
28,592 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;

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<Tuple<int, int, int, int>>(); // (row, col, vitality, step)
        q.Enqueue(Tuple.Create(srow, scol, vitality, 0));
        while (q.Count > 0) {
            var t = q.Dequeue();
            int row = t.Item1;
            int col = t.Item2;
            int v = t.Item3;
            int step = t.Item4;

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

            if (memo[row, col] > 0 && memo[row, col] >= v) continue;
            memo[row, col] = v;

            for (int i = -1; i <= 1; i++) {
                for (int j = -1; j <= 1; j++) {
                    if (Math.Abs(i) + Math.Abs(j) == 1) {
                        int r = row + i;
                        int c = col + j;
                        if (0 <= r && r < n && 0 <= c && c < n) {
                            int v2 = v - g[r, c];
                            if (v2 > 0) {
                                q.Enqueue(Tuple.Create(r, c, v2, 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