c#检查部分是否相交
我有一个连续的垂直和水平部分的列表,固定尺寸的形式是一系列方向的形式(右→;左←; down←; down↓;向上↑)。 如果部分相交,则该程序应返回true。 例如: n = 6:{向上,左,向下,向下,右,向上} - 返回true。 我得到了每个部分的坐标,但是我真的不知道如何完成。 有没有理智?
static (int, int) GetCoordinates(string[] sectionDirection, int numberOfSections)
{
(int X, int Y) pos = (0, 0);
foreach (string move in sectionDirection)
{
switch (move)
{
case "left":
pos = (0, 0);
pos.X--;
break;
case "right":
pos = (0, 0);
pos.X++;
break;
case "down":
pos = (0, 0);
pos.Y--;
break;
case "up":
pos = (0, 0);
pos.Y++;
break;
}
}
return (pos.X, pos.Y);
}
static bool CheckSectionsIntersect(string[] sectionDirection, int numberOfSections)
{
// I need help here.
}
I have a list of N consecutive vertical and horizontal sections of fixed dimensions in a form of a succession of directions (right → ; left ←; down ↓; up ↑).
The program should return true if the sections intersect.
For example:
N = 6: { up, left, down, down, right, up} - return True.
I got the coordinates for every section, but I don't really know how to finish this.
Any ideeas?
static (int, int) GetCoordinates(string[] sectionDirection, int numberOfSections)
{
(int X, int Y) pos = (0, 0);
foreach (string move in sectionDirection)
{
switch (move)
{
case "left":
pos = (0, 0);
pos.X--;
break;
case "right":
pos = (0, 0);
pos.X++;
break;
case "down":
pos = (0, 0);
pos.Y--;
break;
case "up":
pos = (0, 0);
pos.Y++;
break;
}
}
return (pos.X, pos.Y);
}
static bool CheckSectionsIntersect(string[] sectionDirection, int numberOfSections)
{
// I need help here.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我能想到的最简单的代码更改是使用会记住所有访问的地点的东西,并可以告诉您您是否曾访问过一个位置。这样的对象需要一种方法来将位置添加到其内存中,并且需要一种方法来 test 查看是否记住了位置。
信不信由你,您只需使用
字符串
来执行此操作。因为元组将用括号和conmas和逗号划定字段的(0,0)
将字符串转换为字符串,所以只要将它们全部串联成字符串,然后使用string.contains 查看是否存在位置。 (这有点愚蠢,我更喜欢标签,而不是效率的字符串,但是您在评论中说。)
它会构建一个看起来像:
(0,0)(0,1)(-1,1)(-1,0)
...要记住一个位置,请用
将其附加到字符串的末端+=
操作员。当您进行内存 += pos
时,pos
将使用toString()
自动将其转换为字符串。要检查一个位置是否存在,请使用
memory.contains(pos.tostring())
。在这种情况下,您必须将元组显式转换为带有.toString()
的字符串,才能将其作为参数传递给.contains
。The simplest code change I can think of would be to use something that will remember all the visited locations and can tell you whether or not you have visited a location before. Such an object needs a way to add a location to its memory, and needs a way to test to see if a location is remembered.
Believe it or not, you can just use
string
to do this. Because a tuple will convert to a string as(0, 0)
with parentheses and commas delimiting the fields, it's fine to just concatenate them all into a string and then usestring.Contains
to see if a location exists. (This is kind of silly, and I'd prefer a HashSet instead of a string for efficiency, but you said in your comment you cannot.)It will build up a string that looks like:
(0, 0)(0, 1)(-1, 1)(-1, 0)
...To remember a location, append it to the end of the string with the
+=
operator. When you domemory += pos
thenpos
will automatically get converted to a string usingToString()
.To check if a location exists, use
memory.Contains(pos.ToString())
. In this case you have to explicitly convert the tuple to a string with.ToString()
to pass it as an argument to.Contains
.这就是我本质上将其在一行中进行的方式。
通过查看一组不同值的元素是否比完整的坐标集更小的元素来检查任何坐标是否是重复的。
PS。我更喜欢使用
enum
作为有限值(例如方向)的事物This is how I would do it in one line essentially.
Check if any of the coordinates are duplicates by seeing if the set of distinct values has less elements that the full set of coordinates.
PS. I prefer to use
enum
for things with finite values like directions试想一下自己在一个无限大的国际象棋板上,有人告诉您要朝他告诉您的任何方向迈出一步。知道您是否已经越过点的唯一方法是将一些东西放在您经过的每个点上,以防万一您要命令去这样的观点,您会喊“相交!” :-)
如果这只是回到您的起点,您只需计算您收到的命令的数量:如果“ UP”的数量等于“ down” s的数量,而“ down” s的数量和“ left” s的数量等于“右”的数量,您知道您已经达到了起点。
如果不允许您保留四个计数器,则可以使用两个变量“ HOR”(用于水平)和“ ver”(用于垂直)进行操作:
如果最后,“ HOR”和“ VER”都回来了为零,您再次达到了起点。
玩得开心 :-)
Just imagine yourself being on an infinitely large chess board and somebody tells you to put a step in any direction he tells you. The only way to know if you have already crossed a point is by putting some stuff on every point you have passed and in case you get the command to go to such a point, you shout "Intersect!" :-)
If it's just about getting back to your starting point, you just count the amount of commands you've received: if the number of "up"s equals the number of "down"s and the number of "left"s equals the number of "right"s, you know that you have reached your starting point.
If you are not allowed to keep four counters, you can do it with two variables, "hor" (for horizontal) and "ver" (for vertical):
If, at the end, both "hor" and "ver" are back to zero, you have reached again your starting point.
Have fun :-)