結果

問題 No.20 砂漠のオアシス
ユーザー AreTrashAreTrash
提出日時 2016-08-31 05:34:02
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 79 ms / 5,000 ms
コード長 5,408 bytes
コンパイル時間 1,042 ms
コンパイル使用メモリ 115,116 KB
実行使用メモリ 28,608 KB
最終ジャッジ日時 2024-04-21 06:55:09
合計ジャッジ時間 2,697 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
24,260 KB
testcase_01 AC 31 ms
24,236 KB
testcase_02 AC 29 ms
24,376 KB
testcase_03 AC 32 ms
22,196 KB
testcase_04 AC 35 ms
24,136 KB
testcase_05 AC 78 ms
26,376 KB
testcase_06 AC 45 ms
26,680 KB
testcase_07 AC 79 ms
26,960 KB
testcase_08 AC 56 ms
28,608 KB
testcase_09 AC 75 ms
26,564 KB
testcase_10 AC 31 ms
24,256 KB
testcase_11 AC 30 ms
26,188 KB
testcase_12 AC 31 ms
24,368 KB
testcase_13 AC 31 ms
24,260 KB
testcase_14 AC 33 ms
23,880 KB
testcase_15 AC 32 ms
24,388 KB
testcase_16 AC 37 ms
24,004 KB
testcase_17 AC 35 ms
26,068 KB
testcase_18 AC 35 ms
24,388 KB
testcase_19 AC 36 ms
24,128 KB
testcase_20 AC 30 ms
23,876 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;
using System.Collections.Generic;
using System.Linq;

namespace No20{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            var N = sr.Next<int>();
            var V = sr.Next<int>();
            var Ox = sr.Next<int>() - 1;
            var Oy = 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 hp = new int[N, N].ToJaggedArray();
            var pq = new PriorityQueue<Status>();
            pq.Enqueue(new Status(V, 0, 0));

            while(pq.Count > 0){
                var stat = pq.Dequeue();
                for(var i = 0; i < 4; i++){
                    var nx = stat.X + dx[i];
                    var ny = stat.Y + dy[i];

                    if(isInside(nx, ny)){
                        var nv = stat.Hp - L[ny][nx];
                        if(nv <= hp[ny][nx]) continue;

                        if(nx == Ox && ny == Oy) nv *= 2;
                        pq.Enqueue(new Status(nv, nx, ny));
                        hp[ny][nx] = nv;
                    }
                }
            }

            //foreach(var b in hp){
            //    Console.WriteLine(string.Join(" ", b));
            //}

            Console.WriteLine(hp[N - 1][N - 1] > 0 ? "YES" : "NO");
            //---------------------------------
        }
    }

    public static class ExMethod{
        public static T[][] ToJaggedArray<T>(this T[,] src){
            var x = src.GetLength(1);
            var y = src.GetLength(0);
            var ret = new T[y][];

            for(var i = 0; i < y; i++){
                ret[i] = new T[x];
                for(var j = 0; j < x; j++){
                    ret[i][j] = src[i, j];
                }
            }

            return ret;
        } 
    }

    public class Status : IComparable<Status>{
        public int Hp;
        public int X;
        public int Y;

        public Status(int hp, int x, int y){
            Hp = hp;
            X = x;
            Y = y;
        }

        public int CompareTo(Status stat){
            return -(Hp - stat.Hp);
        }
    }

    public class PriorityQueue<T> : IEnumerable<T> where T : IComparable<T>{
        private readonly List<T> _list = new List<T>();
        public int Count => _list.Count;

        IEnumerator<T> IEnumerable<T>.GetEnumerator(){
            return _list.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator(){
            return _list.GetEnumerator();
        }

        public void Clear(){
            _list.Clear();
        }

        public bool Contains(T value){
            return _list.Contains(value);
        }

        public T Peek(){
            return _list[0];
        }

        public void Enqueue(T value){
            _list.Add(value);
            PushHeap();
        }

        public T Dequeue(){
            var ret = _list[0];
            _list[0] = _list[_list.Count - 1];
            _list.RemoveAt(_list.Count - 1);
            PopHeap();
            return ret;
        }

        private void PushHeap(){
            var i = _list.Count - 1;
            while(i != 0){
                var p = (i - 1) / 2;
                if(_list[i].CompareTo(_list[p]) > 0) return;

                SwapIndex(i, i = p);
            }
        }

        private void PopHeap(){
            var i = 0;
            while(true){
                var l = 2 * i + 1;
                var r = l + 1;

                var maxi = i;
                if(l < _list.Count && _list[maxi].CompareTo(_list[l]) > 0) maxi = l;
                if(r < _list.Count && _list[maxi].CompareTo(_list[r]) > 0) maxi = r;
                if(maxi == i) return;

                SwapIndex(i, i = maxi);
            }
        }

        private void SwapIndex(int left, int right){
            var tmp = _list[left];
            _list[left] = _list[right];
            _list[right] = tmp;
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = Console.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