結果

問題 No.34 砂漠の行商人
ユーザー AreTrashAreTrash
提出日時 2016-09-02 12:06:48
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 3,323 bytes
コンパイル時間 956 ms
コンパイル使用メモリ 114,764 KB
実行使用メモリ 25,428 KB
最終ジャッジ日時 2024-04-27 14:58:44
合計ジャッジ時間 2,527 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

namespace No34_2{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var N = sr.Next<int>();
            var V = sr.Next<int>();
            var Sx = sr.Next<int>() - 1;
            var Sy = sr.Next<int>() - 1;
            var Gx = sr.Next<int>() - 1;
            var Gy = sr.Next<int>() - 1;
            var L = sr.Next<int>(N, N);

            Func<int, int, bool> isInside = (x, y) => 0 <= x && x < N && 0 <= y && y < N;
            var dx = new[]{0, 1, 0, -1};
            var dy = new[]{1, 0, -1, 0};

            var dist = new int[V + 1, N, N];
            var que = new Queue<int>();

            dist[V, Sx, Sy] = 1;
            que.Enqueue(V);
            que.Enqueue(Sx);
            que.Enqueue(Sy);

            var res = 0;
            while(que.Count > 0){
                var v = que.Dequeue();
                var x = que.Dequeue();
                var y = que.Dequeue();

                var man = Math.Abs(Gx - x) + Math.Abs(Gy - y);
                if(man * 9 <= v){//イコールつける!!!
                    res = dist[v, x, y] + man;
                    break;
                }

                for(var i = 0; i < 4; i++){
                    var nx = x + dx[i];
                    var ny = y + dy[i];
                    if(isInside(nx, ny)){
                        var nv = v - L[ny][nx];
                        if(nv <= 0) continue;
                        if(dist[nv, nx, ny] != 0 && dist[nv, nx, ny] <= dist[v, x, y] + 1) continue;

                        que.Enqueue(nv);
                        que.Enqueue(nx);
                        que.Enqueue(ny);
                        dist[nv, nx, ny] = dist[v, x, y] + 1;
                    }
                }
            }

            Console.WriteLine(res - 1);
            //---------------------------------
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];
        private readonly System.IO.StreamReader _sr = new System.IO.StreamReader(Console.OpenStandardOutput());

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = _sr.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

        public T[] Next<T>(int x){
            var ret = new T[x];
            for(var i = 0; i < x; ++i) ret[i] = Next<T>();
            return ret;
        }

        public T[][] Next<T>(int y, int x){
            var ret = new T[y][];
            for(var i = 0; i < y; ++i) ret[i] = Next<T>(x);
            return ret;
        }
    }
}
0