C# 检查N个截面是否相交

C# 检查N个截面是否相交,c#,function,intersection,directions,C#,Function,Intersection,Directions,伙计们!我需要实现函数static bool checksections sintersect,该函数检查各部分是否相交(右→ ; 左边←; 向下↓; 向上的↑). 给定一个由N个连续的垂直和水平部分组成的列表,这些部分以连续的方向的形式固定尺寸。我不得不认为我将有一个由N个部分组成的路线。 如果我到达我以前去过的点,函数应该返回True。 例如: N=6:{up,left,down,down,right,up}-返回True ⬇⬅ ⬇⬆ <- Start ➡⬆ ⬇⬅ ⬇⬆ 如果考

伙计们!我需要实现函数static bool checksections sintersect,该函数检查各部分是否相交(右→ ; 左边←; 向下↓; 向上的↑). 给定一个由N个连续的垂直和水平部分组成的列表,这些部分以连续的方向的形式固定尺寸。我不得不认为我将有一个由N个部分组成的路线。 如果我到达我以前去过的点,函数应该返回True。 例如:

N=6:{up,left,down,down,right,up}-返回True

⬇⬅
⬇⬆  <- Start
➡⬆
⬇⬅

⬇⬆ 如果考虑笛卡尔网格,从0,0开始,x坐标左/右递增/递减,y坐标上/下递增,那么答案是在任何点返回0,0

static Dictionary<string,(int X,int Y)> transforms = new Dictionary<string, (int X, int Y)>{
    ["U"] = (0,1),
    ["D"] = (0,-1),
    ["L"] = (-1,0),
    ["R"] = (1,0)
};

static bool CheckSectionsIntersect(string[] sectionDirection, int numberOfSections)
{
    (int X, int Y) pos = (0,0);
    for(var i = 0;i<numberOfSections;i++)
    {
        if(!transforms.TryGetValue(sectionDirection[i], out var transform))
           throw new ArgumentException("sectionDirections");
        pos.X += transform.X;
        pos.Y += transform.Y;
        if(pos.X == 0 && pos.Y == 0)
            return true;                
    }
    return false;
}
静态字典转换=新字典{
[“U”]=(0,1),
[“D”]=(0,-1),
[“L”]=(-1,0),
[“R”]=(1,0)
};
静态bool CheckSectionsIntersect(字符串[]sectionDirection,int numberOfSections)
{
(整数X,整数Y)pos=(0,0);

对于(var i=0;i我不确定我是否理解你在说什么。 如果你想看看你是否在你去过的地方结束,只需要做一个元组数组。在每次移动中保存你的坐标(从0,0开始)。在路径的末尾检查你的坐标是否在数组中。或者在每次移动的末尾检查你已经去过的地方

编辑:

static bool CheckSectionsIntersect(string[] sectionDirection, int numberOfSections)
    {
        (int X, int Y) pos = (0, 0); //Variable to track our actual position. Start position (0,0)
        List<(int X, int Y)> coordinates = new List<(int X, int Y)>(); //List where we are going to keep the coordinates
        foreach (string move in sectionDirection) //for each move
        {
            coordinates.Add(pos); //Add our current position to the list
            switch (move)         //Make a move
            {
                case "left":
                    pos.X -= 1;
                    break;
                case "right":
                    pos.X += 1;
                    break;
                case "down":
                    pos.Y -= 1;
                    break;
                case "up":
                    pos.Y += 1;
                    break;
            }
            if (coordinates.Contains(pos)) { return true; } //If our new position is already in the list, we have been there.
        }
        return false;
    }
static bool CheckSectionsIntersect(字符串[]sectionDirection,int numberOfSections)
{
(intx,inty)pos=(0,0);//用于跟踪实际位置的变量。起始位置(0,0)
List coordinates=new List();//列出要保留坐标的位置
foreach(字符串沿截面方向移动)//每次移动
{
coordinates.Add(pos);//将当前位置添加到列表中
开关(移动)//移动
{
案例“左”:
位置X-=1;
打破
案例“权利”:
位置X+=1;
打破
案例“向下”:
位置Y-=1;
打破
案例“up”:
位置Y+=1;
打破
}
如果(coordinates.Contains(pos)){return true;}//如果我们的新位置已经在列表中,那么我们已经在那里了。
}
返回false;
}
我已经测试过了,它是有效的。 为此,使用列表比使用数组更容易。这是由于使用了.Add()和.contains()方法


此代码不是最强的(例如,它不接受大写字母)。但既然您是初学者,这就足够了。我鼓励您改进它以继续学习。

对于非起点部分。如果新值已在哈希集中,则在哈希集中添加坐标返回true;这仅涵盖起点的交点。我可以用任何点的交点编辑您的答案吗。只添加
var existing=新哈希集{pos};
如果(!existing.Add(pos))返回true;
。因为这是100%的代码,只有一行新代码。@DragandDrop原则上你编辑我的答案没有问题。你甚至不需要我的许可-这是SE的工作方式!但我可能会争辩说,我们不知道这是否是一个实际需求,因此可能过于复杂。作为另一个答案(或此代码块中的第二个代码块)我认为这很好,如果你愿意,欢迎你从我的代码中获得灵感。@SimonaM。你能确认这个答案是否完整吗?你是想确定你是在开始时结束,还是在以前去过的任何地方结束?你的
true
example/testcase在开始时结束。@jamice,我需要确定是否我在一个我去过的地方结束。我是一个初学者,我还没有在我的课程中学习字典以及如何引发异常。我正在尝试使用数组找到另一个解决方案。欢迎任何建议。劳尔德,你说得对,我想看看我是否在一个我去过的地方结束。我已经尝试按照你的建议编写代码ion,但没有成功。我不懂如何写这些动作。你能帮我吗?谢谢!没问题,我已经用一个例子编辑了答案如果你只想检查端点。请输入“如果”非常感谢您的解决方案。这太有帮助了!对不起,我不明白您的问题。您能重新措辞吗?谢谢!
static Dictionary<string,(int X,int Y)> transforms = new Dictionary<string, (int X, int Y)>{
    ["U"] = (0,1),
    ["D"] = (0,-1),
    ["L"] = (-1,0),
    ["R"] = (1,0)
};

static bool CheckSectionsIntersect(string[] sectionDirection, int numberOfSections)
{
    (int X, int Y) pos = (0,0);
    for(var i = 0;i<numberOfSections;i++)
    {
        if(!transforms.TryGetValue(sectionDirection[i], out var transform))
           throw new ArgumentException("sectionDirections");
        pos.X += transform.X;
        pos.Y += transform.Y;
        if(pos.X == 0 && pos.Y == 0)
            return true;                
    }
    return false;
}
static bool CheckSectionsIntersect(string[] sectionDirection, int numberOfSections)
{
    (int X, int Y) pos = (0,0);
    var visited = new List<(int X, int Y)>{ pos };
    for(var i = 0;i<numberOfSections;i++)
    {
        if(!transforms.TryGetValue(sectionDirection[i], out var transform))
           throw new ArgumentException("sectionDirections");
        (int X, int Y) newPos = (pos.X + transform.X, pos.Y + transform.Y);
        
        if(visited.Contains(newPos))
            return true;    
                    
        pos = newPos;
        visited.Add(newPos);
    }
    return false;
}
static bool CheckSectionsIntersect(string[] sectionDirection, int numberOfSections)
    {
        (int X, int Y) pos = (0, 0); //Variable to track our actual position. Start position (0,0)
        List<(int X, int Y)> coordinates = new List<(int X, int Y)>(); //List where we are going to keep the coordinates
        foreach (string move in sectionDirection) //for each move
        {
            coordinates.Add(pos); //Add our current position to the list
            switch (move)         //Make a move
            {
                case "left":
                    pos.X -= 1;
                    break;
                case "right":
                    pos.X += 1;
                    break;
                case "down":
                    pos.Y -= 1;
                    break;
                case "up":
                    pos.Y += 1;
                    break;
            }
            if (coordinates.Contains(pos)) { return true; } //If our new position is already in the list, we have been there.
        }
        return false;
    }