結果
問題 |
No.34 砂漠の行商人
|
ユーザー |
![]() |
提出日時 | 2015-10-06 20:11:37 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 776 ms / 5,000 ms |
コード長 | 2,120 bytes |
コンパイル時間 | 3,144 ms |
コンパイル使用メモリ | 105,600 KB |
実行使用メモリ | 52,040 KB |
最終ジャッジ日時 | 2024-06-28 10:25:00 |
合計ジャッジ時間 | 5,360 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
コンパイルメッセージ
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.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)); } }