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[,] step = new List[mapLength, mapLength]; step[startY, startX] = new List(); step[startY, startX].Add(new CellState(tairyoku, 0)); Queue task = new Queue(); task.Enqueue(new TaskItem(startX, startY, 0, tairyoku)); while (task.Count > 0) { TaskItem tsk = task.Dequeue(); List nextPosList = new List(); 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(); } 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 { 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(); } }