結果

問題 No.124 門松列(3)
ユーザー 14番14番
提出日時 2016-04-17 13:18:42
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 4,373 bytes
コンパイル時間 1,074 ms
コンパイル使用メモリ 109,568 KB
実行使用メモリ 27,824 KB
最終ジャッジ日時 2024-04-15 02:14:42
合計ジャッジ時間 3,028 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
24,928 KB
testcase_01 AC 32 ms
25,224 KB
testcase_02 AC 32 ms
23,092 KB
testcase_03 AC 42 ms
27,824 KB
testcase_04 AC 35 ms
27,216 KB
testcase_05 AC 34 ms
24,928 KB
testcase_06 AC 32 ms
25,220 KB
testcase_07 AC 32 ms
25,092 KB
testcase_08 AC 30 ms
22,964 KB
testcase_09 AC 33 ms
27,424 KB
testcase_10 AC 31 ms
24,868 KB
testcase_11 AC 32 ms
27,052 KB
testcase_12 AC 32 ms
25,000 KB
testcase_13 AC 32 ms
25,096 KB
testcase_14 AC 33 ms
27,308 KB
testcase_15 AC 33 ms
22,964 KB
testcase_16 AC 34 ms
25,088 KB
testcase_17 AC 33 ms
24,968 KB
testcase_18 AC 32 ms
25,092 KB
testcase_19 AC 33 ms
27,136 KB
testcase_20 AC 33 ms
25,004 KB
testcase_21 AC 33 ms
27,052 KB
testcase_22 AC 34 ms
24,960 KB
testcase_23 AC 32 ms
24,668 KB
testcase_24 AC 34 ms
25,072 KB
testcase_25 AC 32 ms
24,964 KB
testcase_26 AC 32 ms
25,024 KB
testcase_27 AC 30 ms
25,088 KB
testcase_28 AC 34 ms
25,260 KB
testcase_29 AC 34 ms
25,172 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;
using System.Linq;
 
class Program
{
    public void Proc()
    {
        Reader.IsDebug = false;
        int[] inpt = Reader.GetInt();
        int width = inpt[0];
        int height = inpt[1];
        
        int[,] map = new int[height, width];
        for(int i=0; i<height; i++) {
            inpt = Reader.GetInt();
            for(int j=0; j<width; j++) {
                map[i,j] = inpt[j];
            }
        }
        
        Queue<TaskItem> task = new Queue<TaskItem>();
        task.Enqueue(new TaskItem(0,0,-1,0));
        task.Enqueue(new TaskItem(0,0,100,0));
        
        Dictionary<int, int>[,] step = new Dictionary<int, int>[height, width];
        while (task.Count > 0)
        {
           TaskItem t = task.Dequeue();
           if(step[t.Y, t.X] == null) {
               step[t.Y, t.X] = new Dictionary<int, int>();
           }
           if(step[t.Y, t.X].ContainsKey(t.Prev)) {
               if(step[t.Y, t.X][t.Prev] <= t.NextStep) {
                   continue;
               } else {
                   step[t.Y, t.X][t.Prev] = t.NextStep;
               }
           } else {
               step[t.Y, t.X].Add(t.Prev, t.NextStep);
           }
           if(t.X == width - 1 && t.Y == height - 1) {
               continue;
           }
           List<int[]> nextPosList = new List<int[]>();
           if(t.Y > 0) {
               nextPosList.Add(new int[]{t.Y-1, t.X});
           }
           if(t.Y < height - 1) {
               nextPosList.Add(new int[]{t.Y+1, t.X});
           }
           if(t.X > 0) {
               nextPosList.Add(new int[]{t.Y, t.X - 1});
           }
           if(t.X < width - 1) {
               nextPosList.Add(new int[]{t.Y, t.X + 1});
           }
           int nowLen = map[t.Y, t.X];
           nextPosList.ForEach((a)=>{
               int nextY = a[0];
               int nextX = a[1];
               int nextLen = map[nextY, nextX];
               if(nowLen == nextLen || t.Prev == nextLen) {
                   return;
               }
               if(t.Prev > nowLen && nowLen > nextLen) {
                   return;
               } 
               if(t.Prev < nowLen && nowLen < nextLen) {
                   return;
               }
               TaskItem newTask = new TaskItem(nextX, nextY, nowLen, t.NextStep + 1);
               task.Enqueue(newTask);
           });
        }
        int ans = 0;
        if(step[height-1, width-1] == null) {
            ans = -1;
        } else {
            ans = step[height-1, width-1].Values.Min();
        }
        Console.WriteLine(ans);

    }
    
    public class TaskItem {
        public int X;
        public int Y;
        
        public int Prev;
        public int NextStep;
        
        public TaskItem(int x, int y, int prev, int nextstep) {
            this.X = x;
            this.Y = y;
            this.Prev = prev;
            this.NextStep = nextstep;
        }
    }
    
    public class CellState {
        public int Prev;
        public int Step;
        public CellState(int prev, int step) {
            this.Prev = prev;
            this.Step = step;
        }
    }
 
    public class Reader
    {
        public static bool IsDebug = true;
        private static String PlainInput = @"




3 3
5 4 1
6 6 9
2 8 8




 
";
        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