結果

問題 No.34 砂漠の行商人
ユーザー 14番14番
提出日時 2016-04-03 17:53:58
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 5,173 bytes
コンパイル時間 913 ms
コンパイル使用メモリ 116,312 KB
実行使用メモリ 115,200 KB
最終ジャッジ日時 2024-04-10 10:47:21
合計ジャッジ時間 12,156 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
115,200 KB
testcase_01 AC 27 ms
24,104 KB
testcase_02 AC 29 ms
24,112 KB
testcase_03 AC 29 ms
23,856 KB
testcase_04 AC 70 ms
26,932 KB
testcase_05 AC 116 ms
31,320 KB
testcase_06 AC 38 ms
26,248 KB
testcase_07 AC 205 ms
36,320 KB
testcase_08 AC 349 ms
40,692 KB
testcase_09 AC 2,440 ms
59,348 KB
testcase_10 AC 653 ms
47,116 KB
testcase_11 WA -
testcase_12 AC 102 ms
32,464 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

        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[startX, startY] = new List<CellState>();
        step[startX, startY].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 =0; i<step[nextY, nextX].Count; 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);
                        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((a, b) =>
            {
                if (a.Step < b.Step)
                {
                    return -1;
                }
                if (a.Step > b.Step)
                {
                    return 1;
                }
                return 0;
            });

            ans = step[goalY, goalX][0].Step;
        }
        Console.WriteLine(ans);

    }

    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
    {
        public int Tairyoku = 0;
        public int Step = 0;
        public CellState(int tairyoku, int step)
        {
            this.Tairyoku = tairyoku;
            this.Step = step;
        }
    }

    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