結果

問題 No.34 砂漠の行商人
ユーザー 14番14番
提出日時 2016-04-03 18:16:49
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 933 ms / 5,000 ms
コード長 5,900 bytes
コンパイル時間 1,228 ms
コンパイル使用メモリ 115,584 KB
実行使用メモリ 42,156 KB
最終ジャッジ日時 2024-06-28 10:31:47
合計ジャッジ時間 7,530 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
18,048 KB
testcase_01 AC 28 ms
17,792 KB
testcase_02 AC 31 ms
18,560 KB
testcase_03 AC 30 ms
18,304 KB
testcase_04 AC 56 ms
22,144 KB
testcase_05 AC 72 ms
22,272 KB
testcase_06 AC 35 ms
21,248 KB
testcase_07 AC 102 ms
23,296 KB
testcase_08 AC 146 ms
24,704 KB
testcase_09 AC 357 ms
34,176 KB
testcase_10 AC 256 ms
30,464 KB
testcase_11 AC 144 ms
24,064 KB
testcase_12 AC 62 ms
22,144 KB
testcase_13 AC 933 ms
42,156 KB
testcase_14 AC 840 ms
41,988 KB
testcase_15 AC 100 ms
23,040 KB
testcase_16 AC 102 ms
23,040 KB
testcase_17 AC 158 ms
25,472 KB
testcase_18 AC 35 ms
21,632 KB
testcase_19 AC 391 ms
37,760 KB
testcase_20 AC 516 ms
38,144 KB
testcase_21 AC 30 ms
18,688 KB
testcase_22 AC 304 ms
32,768 KB
testcase_23 AC 156 ms
24,832 KB
testcase_24 AC 533 ms
38,016 KB
testcase_25 AC 165 ms
25,344 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.Text;

class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        // Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff"));

        int[] inpt = Reader.GetInt(' ', true);
        int mapLength = inpt[0];
        int tairyoku = inpt[1];
        int startX = inpt[2] - 1;
        int startY = inpt[3] - 1;
        int goalX = inpt[4] - 1;
        int goalY = inpt[5] - 1;

        int[,] map = new int[mapLength,mapLength];

        for (int i = 0; i < mapLength; i++)
        {
            inpt = Reader.GetInt();
            for (int j = 0; j < mapLength; j++)
            {
                map[i, j] = inpt[j];
            }
        }

        List<CellState>[,] step = new List<CellState>[mapLength, mapLength];
        step[startY, startX] = new List<CellState>();
        step[startY, startX].Add(new CellState(tairyoku, 0));
        Queue<TaskItem> task = new Queue<TaskItem>();
        task.Enqueue(new TaskItem(startX, startY, 0, tairyoku));

        while (task.Count > 0)
        {
            TaskItem tsk = task.Dequeue();
            List<int[]> nextPosList = new List<int[]>();
            if (tsk.X > 0)
            {
                nextPosList.Add(new int[] { tsk.X - 1, tsk.Y });
            }
            if (tsk.X < mapLength - 1)
            {
                nextPosList.Add(new int[] { tsk.X + 1, tsk.Y });
            }
            if (tsk.Y > 0)
            {
                nextPosList.Add(new int[] { tsk.X, tsk.Y - 1 });
            }
            if (tsk.Y < mapLength - 1)
            {
                nextPosList.Add(new int[] { tsk.X, tsk.Y + 1 });
            }
            foreach (int[] nextPos in nextPosList)
            {
                int nextX = nextPos[0];
                int nextY = nextPos[1];

                int nextTairyoku = tsk.Tairyoku - map[nextY, nextX];
                if(nextTairyoku > 0) {
                    if (step[nextY, nextX] == null)
                    {
                        step[nextY, nextX] = new List<CellState>();
                    }

                    bool canAddTask = true;
                    int nextStep = tsk.Step + 1;
                    for(int i = step[nextY, nextX].Count - 1; i>=0; i--) {
                        CellState tmpCS = step[nextY, nextX][i];
                        if(tmpCS.Tairyoku >= nextTairyoku && tmpCS.Step <= nextStep) {
                            canAddTask = false;
                            break;
                        }
                    }
                    if(canAddTask) {
                        CellState newState = new CellState(nextTairyoku, nextStep);

                        for (int i = step[nextY, nextX].Count - 1; i >= 0; i--)
                        {
                            if (step[nextY, nextX][i].Step > nextStep)
                            {
                                break;
                            }
                            if (step[nextY, nextX][i].Step >= nextStep && step[nextY, nextX][i].Tairyoku <= nextTairyoku)
                            {
                                step[nextY, nextX].RemoveAt(i);
                            }
                        }

                        step[nextY, nextX].Add(newState);
                        if ((nextX != goalX || nextY != goalY))
                        {
                            TaskItem newTask = new TaskItem(nextX, nextY, nextStep, nextTairyoku);
                            task.Enqueue(newTask);
                        }
                    }
                }
            }
        }

        int ans = -1;

        if(step[goalY, goalX] != null) {
            step[goalY, goalX].Sort();

            ans = step[goalY, goalX][0].Step;
        }
        Console.WriteLine(ans);
        // Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff"));

    }

    public class TaskItem
    {
        public int Step;
        public int Tairyoku;
        public int X;
        public int Y;
        public TaskItem(int x, int y, int step, int tairyoku)
        {
            this.X = x;
            this.Y = y;
            this.Step = step;
            this.Tairyoku = tairyoku;
        }

    }
    public class CellState:IComparable<CellState>
    {
        public int Tairyoku = 0;
        public int Step = 0;
        public CellState(int tairyoku, int step)
        {
            this.Tairyoku = tairyoku;
            this.Step = step;
        }

        public int CompareTo(CellState other)
        {
            if (this.Step < other.Step)
            {
                return -1;
            }
            if (this.Step > other.Step)
            {
                return 1;
            }
            return 0;
        }
    }

    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"





 
";
        private static System.IO.StringReader Sr = null;
        public static string ReadLine()
        {
            if (IsDebug)
            {
                if (Sr == null)
                {
                    Sr = new System.IO.StringReader(PlainInput.Trim());
                }
                return Sr.ReadLine();
            }
            else
            {
                return Console.ReadLine();
            }
        }
        public static int[] GetInt(char delimiter = ' ', bool trim = false)
        {
            string inptStr = ReadLine();
            if (trim)
            {
                inptStr = inptStr.Trim();
            }
            string[] inpt = inptStr.Split(delimiter);
            int[] ret = new int[inpt.Length];
            for (int i = 0; i < inpt.Length; i++)
            {
                ret[i] = int.Parse(inpt[i]);
            }
            return ret;
        }
    }
    static void Main()
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0