寻路 - 方向和空间移动

时间:2017-11-28 09:04:20

标签: c# coordinates

我只是从C#开始(我是绿色的)并且正在使用x和y坐标的网格系统进行迷宫解决方案。我可以通过迷宫找到开放点,我有一堆从探路者返回的点。

public Stack Path {get; }

但是我想把它打印成人类可读的方向。 IE旅行剩下4个空间,2个向下类型。

例如:

点堆栈:

X Y格式

6,60
7,60
8,60
9,60
10,60
10,61
10,62
10,63
10,64
10,65
11,65

现在经历这个,在人类可读的情况下,意味着向下移动5个空间,向左移动5个空间,向下移动1个空间。

5空间下降(6,60 | 7,60 | 8,60 | 9,60 | 10,60)

剩下5个空格(10,61 | 10,62 | 10,63 | 10,64 | 10,65)

1 Space Down(11,65)

但是,我如何遍历堆栈以找到方向的变化以及朝这个方向前进的空间数量?我假设检查x或y有多长是恒定的,有多少......但我不知道怎么做!请帮助一个菜鸟:(

还可以写出行走方向改变了多少次?

2 个答案:

答案 0 :(得分:1)

如果您知道如何确定(6,60) - > (7,60)=向下,以及如何存储当前方向'并且增加一个“行进距离”,然后您需要做的就是弄清楚如果下两个点之间的方向与您当前的方向不同,该怎么办。

(顺便说一下,如果你想要加分,你可以使用points.Aggegrate( )相当容易地做到这一点,你的累加器负责建立每个新点的方向列表。但我不建议这样做除非你熟悉函数式编程概念)

答案 1 :(得分:0)

一种方法是简单地跟踪当前方向和我们在该方向上采取的步数,并且无论何时方向改变,输出我们采取的步数并重置我们的计数器和方向。 / p>

首先,快速记录方向:

enum Direction
{
    None, Up, Down, Left, Right
}

然后,一种方法,它接收点列表并回显给用户的指示。我在代码中使用了注释而不是在这里描述它。

需要注意的一点是,根据您的示例,第一个数字(X)的增加表示向下移动,第二个数字(Y)的增加表示移动剩下。

该代码还假设只有XY在点之间发生变化。要允许两者都改变​​,您需要跟踪水平和垂直移动变化。

请注意,您需要在文件顶部添加using System.Drawing;以引用Point类:

private static void ProcessPoints(List<Point> points)
{
    if (points == null || points.Count < 2) return;

    // Setup current direction and step count
    var direction = Direction.None;
    var steps = 0;

    // Process each point, starting with the second one
    for (var i = 1; i < points.Count; i++)
    {
        // Get the previous and current points
        var previousPoint = points[i - 1];
        var thisPoint = points[i];

        // If we haven't moved, continue
        if (previousPoint == thisPoint) continue;

        // Calculate the current direction
        var currentDirection = thisPoint.X == previousPoint.X
            ? thisPoint.Y == previousPoint.Y
                ? Direction.None
                : thisPoint.Y > previousPoint.Y ? Direction.Left : Direction.Right
            : thisPoint.X > previousPoint.X ? Direction.Down : Direction.Up;

        // If this is our first step, set the direction
        if (direction == Direction.None) direction = currentDirection;

        // If our current direction is the same as the last one, increment steps
        if (currentDirection == direction)
        {
            steps++;
        }
        // If we've changed direction, output how many steps we took in
        // the previous direction and reset our steps and direction
        else
        {
            Console.WriteLine($"Walk {steps} spaces {direction}");
            steps = 1;
            direction = currentDirection;
        }
    }

    // Output the last leg of our journey
    Console.WriteLine($"Walk {steps} spaces {direction}");
}

要使用您的示例数据对其进行测试,我们只需创建这些点的列表并将其传递给我们的方法:

private static void Main()
{
    var points = new List<Point>
    {
        new Point(6, 60),
        new Point(7, 60),
        new Point(8, 60),
        new Point(9, 60),
        new Point(10, 60),
        new Point(10, 61),
        new Point(10, 62),
        new Point(10, 63),
        new Point(10, 64),
        new Point(10, 65),
        new Point(11, 65),
    };

    ProcessPoints(points);

    Console.WriteLine("\nDone!\nPress any key to exit...");
    Console.ReadKey();
}

<强>输出

请注意,我的结果与您的结果略有不同,因为我从0步开始,没有方向。第一点就是 - 你开始时所站的点,而不是你从其他地方走过的点,所以最初的一步只有4步,而不是5步:

enter image description here