C#比较二维数组中的相邻值
路径从行索引80、列索引0开始。我必须创建到地图另一个边缘的路径(列索引199)。我不能返回到我访问过的前一个单元格,我必须对仅与当前单元格相邻的单元格进行比较,但我不能向后移动列,我必须继续向前移动,或者我可以在当前列中“向上”或“向下”移动。我发现细胞之间的差异最小 为了表示路径,我必须存储一个(/)来表示您向上和向右移动,()来表示我向下和向右移动,()来表示我向右移动,或者(|)来表示我向上或向下移动 以下是我到目前为止的情况: 我不知道如何比较相邻的单元格。我想我需要一些if语句,但我不太明白。 至于表示路径,我真的不确定该使用什么。我试图用一个单独的方法将这些字符存储到另一个2d数组中,但无法使其工作C#比较二维数组中的相邻值,c#,arrays,algorithm,project,C#,Arrays,Algorithm,Project,路径从行索引80、列索引0开始。我必须创建到地图另一个边缘的路径(列索引199)。我不能返回到我访问过的前一个单元格,我必须对仅与当前单元格相邻的单元格进行比较,但我不能向后移动列,我必须继续向前移动,或者我可以在当前列中“向上”或“向下”移动。我发现细胞之间的差异最小 为了表示路径,我必须存储一个(/)来表示您向上和向右移动,()来表示我向下和向右移动,()来表示我向右移动,或者(|)来表示我向上或向下移动 以下是我到目前为止的情况: 我不知道如何比较相邻的单元格。我想我需要一些if语句,但我
static void Main(string[] args)
{
string[,] path = new string[116, 200];
short[,] map = arrayMethod();
int rowIndex = 80;
int colIndex = 0;
int positionOfX = 0;
int positionOfY = 0;
short minValue = short.MaxValue;
while (colIndex != 199)
{
for (short l = -1; l < 2; l++)
{
for (short k = 0; k < 2; k++)
{
if (map[l+ rowIndex, k+colIndex] < minValue)
{
positionOfX = l;
positionOfY = k;
minValue = map[l, k];
}
}
}
}
}
// Method to store data into 2d array
public static short[,] arrayMethod()
{
short[,] map = new short[116, 200];
string fileName = "land.csv";
StreamReader reader = new StreamReader(File.Open(fileName,
FileMode.Open));
string nextLine;
int partsCounter = 0;
while ((nextLine = reader.ReadLine()) != null)
{
string[] parts = nextLine.Split(',');
for (int cols = 0; cols < 200; cols++)
{
map[partsCounter, cols] = Convert.ToInt16(parts[cols]);
}
partsCounter++;
}
return map;
}
static void Main(字符串[]args)
{
字符串[,]路径=新字符串[116200];
short[,]map=arrayMethod();
指数=80;
int-colIndex=0;
int-positionOfX=0;
y=0;
short minValue=short.MaxValue;
while(colIndex!=199)
{
对于(简称l=-1;l<2;l++)
{
用于(短k=0;k<2;k++)
{
if(映射[l+行索引,k+共索引]
关于选择路径,您可以说:
我发现细胞之间的差异最小
我会再谈这个问题
关于比较相邻单元格,假设每个单元格都由数组中的
short
表示,这应该是简单的数字比较
我相信问题的关键在于找到相邻的细胞
你有一个二维网格,你有一个单元格的坐标
+-----+-----+-----+-----+
| | | | |
+-----+-----+-----+-----+
| | | | |
+-----+-----+-----+-----+
| | | x,y | |
+-----+-----+-----+-----+
| | | | |
+-----+-----+-----+-----+
通过将坐标更改1,可以找到相邻单元:
+-----+-----+-----+-----+
| | | | |
+-----+-----+-----+-----+
| | |x,y-1| |
+-----+-----+-----+-----+
| |x-1,y| x,y |x+1,y|
+-----+-----+-----+-----+
| | |x,y+1| |
+-----+-----+-----+-----+
注意:我表示的坐标假定原点在左上角。这是计算机图形学的标准惯例。它可能不适用于您的情况。不管怎样,这些细胞仍然是你的相邻细胞
如果我们不应该回到前一列(列的数量必须不断增加),我认为忽略相邻的一列是安全的:
+-----+-----+-----+-----+
| | | | |
+-----+-----+-----+-----+
| | |x,y-1| |
+-----+-----+-----+-----+
| | | x,y |x+1,y|
+-----+-----+-----+-----+
| | |x,y+1| |
+-----+-----+-----+-----+
我们可以这样做:
// the map
short[,] map = arrayMethod();
// map dimensions
int numRows = map.GetLength(0);
int numCols = map.GetLength(1);
// current position
int rowIndex = 80;
int colIndex = 0;
while (colIndex < numCols - 1)
{
var current = map[rowIndex, colIndex];
if (rowIndex > 0)
{
// We are not at the low row edge
var adjacent = map[rowIndex - 1, colIndex];
Compare(current, adjacent);
}
if (rowIndex < numRows - 1)
{
// We are not at the high row edge
var adjacent = map[rowIndex + 1, colIndex];
Compare(current, adjacent);
}
/*if (colIndex > 0)
{
// We are not at the low column edge
var adjacent = map[rowIndex, colIndex - 1];
Compare(current, adjacent);
}*/
if (/*colIndex < numCols - 1*/ true)
{
// We are not at the high column edge
var adjacent = map[rowIndex, colIndex + 1];
Compare(current, adjacent);
}
// ... update rowIndex, colIndex
}
现在
我发现细胞之间的差异最小
我将其理解为差值的绝对值,我推断您希望将其最小化(这方面的要求不是很清楚)
在这种理解下,我们可以进行比较:
foreach (var adjacent in adjacents)
{
var diff = Math.Abs(map[adjacent.Item1, adjacent.Item2] - current);
}
选择我们的目的地:
Tuple<short, short> best;
short bestDiff;
foreach (var adjacent in adjacents)
{
var diff = Math.Abs(map[adjacent.Item1, adjacent.Item2] - current);
if (best == null || bestDiff > diff)
{
best = adjacent;
bestDiff = diff;
}
}
rowIndex = best.Item1;
colIndex = best.Item2;
我相信你可以为它编出代码,只是更多的是相同的。这些将更接近于检查和添加到列表中
我希望你比我更了解这些要求
一些建议:
-
<> >你可以考虑使用一种类型来表示地图中的位置。由于您经常使用行变量对和列变量对,因此有一个用于该变量的类型是有意义的。。。我只是在使用元组,但你可以做得更好
- 您可能希望封装检查相邻位置是否不是旧位置并将其添加到列表中的逻辑。这样做的好处是使代码更具可读性,并且不容易出错(例如,您不会意外地检查一个相邻的代码,而是添加一个不同的代码,这可能会在复制和粘贴过程中忽略)
附录:我猜输出将是一个字符串,您可以使用
StringBuilder
构建它。添加的字符取决于拾取的相邻字符。如果一个结构同时包含相邻坐标和要使用的角色(如果您选择它),那么它可能会使事情变得更容易。还是需要在控制台中绘制路径?因为这是完全不同的。堆栈溢出并不意味着是一个“为我做”的编码站点。请阅读,不要让任何人为我做这件事。我正在寻求帮助。谢谢。你能告诉我们你的代码有什么问题吗?它也不是一个雇佣人的网站。提出一个问题。我猜你的代码不起作用了。。。在这种情况下,我们可以告诉您它在做什么,它与您期望的有什么不同,然后我们可以告诉您如何修复它。你可以。你的问题是你基本上列出了一个规格表。这会让遇到你问题的人认为你没有做任何工作。是的,您确实添加了代码,但是,您的问题的格式使我们相信您没有特定错误,您不想做任何工作。正如@Theraot所提到的,它将帮助您根据编辑问题,以便我们能够帮助您解决您的确切问题:)非常感谢您的回复!对不起,如果我不清楚一些要求。路径
Tuple<short, short> best;
short bestDiff;
foreach (var adjacent in adjacents)
{
var diff = Math.Abs(map[adjacent.Item1, adjacent.Item2] - current);
if (best == null || bestDiff > diff)
{
best = adjacent;
bestDiff = diff;
}
}
rowIndex = best.Item1;
colIndex = best.Item2;
// the map
short[,] map = arrayMethod();
// map dimensions
int numRows = map.GetLength(0);
int numCols = map.GetLength(1);
// current position
int rowIndex = 80;
int colIndex = 0;
// old position
int rowIndexOld = 80;
int colIndexOld = -1; // <-- placing it outside the map
while (colIndex < numCols - 1)
{
var current = map[rowIndex, colIndex];
var adjacents = new List<Tuple<short, short>>();
if (rowIndex > 0 && rowIndexOld != rowIndex - 1 && colIndexOld != colIndex)
{
adjacents.Add(Tuple.Create(rowIndex - 1, colIndex));
}
if (rowIndex < numRows - 1 && rowIndexOld != rowIndex + 1 && colIndexOld != colIndex)
{
adjacents.Add(Tuple.Create(rowIndex + 1, colIndex));
}
if (rowIndexOld != rowIndex && colIndexOld != colIndex + 1)
{
adjacents.Add(Tuple.Create(rowIndex, colIndex + 1));
}
rowIndexOld = rowIndex;
colIndexOld = colIndex;
// etc
}
+-------+-------+-------+-------+
| | | | |
+-------+-------+-------+-------+
| |x-1,y-1| x,y-1 |x+1,y-1|
+-------+-------+-------+-------+
| | x-1,y | x,y | x+1,y |
+-------+-------+-------+-------+
| |x-1,y+1| x,y+1 |x+1,y+1|
+-------+-------+-------+-------+