示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="x">End X coordinate</param>
        /// <param name="y">End Y coordinate</param>
        private void LookingForStepsToPoint(int x, int y, FieldPoint start)
        {
            try
            {
                Path.Add(new FieldPoint(x, y));

                if (x == start.X && y == start.Y)
                {
                    return;
                }

                if (A[x, y] == temp)
                {
                    temp--;
                    if (y > 0 && A[x, y - 1] == temp)
                    {
                        LookingForStepsToPoint(x, y - 1, start);
                        return;
                    }
                    if (x > 0 && A[x - 1, y] == temp)
                    {
                        LookingForStepsToPoint(x - 1, y, start);
                        return;
                    }
                    if (y < n && A[x, y + 1] == temp)
                    {
                        LookingForStepsToPoint(x, y + 1, start);
                        return;
                    }
                    if (x < n && A[x + 1, y] == temp)
                    {
                        LookingForStepsToPoint(x + 1, y, start);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Add(new Log(this.GetType().Name, MethodBase.GetCurrentMethod().Name, $"Error", ex));
            }
        }
示例#2
0
        /// <summary>
        /// Checking the ways from point A to B
        /// </summary>
        /// <param name="start">Start point</param>
        /// <param name="end">End point</param>
        /// <returns>If the path exists, its points are returned</returns>
        public List <FieldPoint> GetWay(FieldPoint start, FieldPoint end)
        {
            try
            {
                var str = new string[A.GetUpperBound(0) + 1];
                A[start.X, start.Y] = 2;
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < n; j++)
                    {
                        str[i] += A[i, j] + " ";
                    }
                }

                if (HaveWay(end.X, end.Y, start))
                {
                    for (int i = 0; i < n; i++)
                    {
                        str[i] = "";
                        for (int j = 0; j < n; j++)
                        {
                            str[i] += A[i, j] + " ";
                        }
                    }
                    n--;
                    LookingForStepsToPoint(end.X, end.Y, start);
                    n++;
                    Path.Reverse();
                    return(Path);
                }
            }
            catch (Exception ex)
            {
                Logger.Add(new Log(this.GetType().Name, MethodBase.GetCurrentMethod().Name, $"Error", ex));
            }
            return(null);
        }