c# - 遍历 0's and 1' 的 int[,] 数组

标签 c# multidimensional-array int 2d-games

草书文本:较早的条目

普通文本:最新条目

好的,所以我有一个二维整数数组,更具体地说,是 0 和 1。 最初这是一个 BMP 文件(黑白),我将其转换为一个整数数组。 现在,矩阵代表一张 map ,其中 0 是我可以站立的地方(如果你愿意的话,地板),而 1 是深渊或墙(你不能站在 1 中)。 所以我需要遍历这个随机的整数数组,但我需要能够遍历 map 的所有 0。 我是否多次访问“像素”并不重要 这有什么意义呢?我有一种方法可以在 map 中“隐藏”4 个键,我的角色必须在 map 中找到它们。 我的角色只能上下左右移动。不允许沿对角线移动,显然也不允许传送。 到目前为止我有这段代码:

public void goThrough(int[,] map, int[] pos)
{
    int i = pos[0], j = pos[1]  ;
    while( i < map.GetLength(0) && j < map.GetLength(1) )
    {
        int cont = 0;
        if (map[i, j] == 0) 
        { 
            if (map[i, j + 1] == 0 && cont == 0 ) { j++; cont++; }
            if (map[i + 1, j] == 0 && cont == 0) { i++; cont++; }
            if (map[i, j - 1] == 0 && cont == 0 ) { j--; cont++; }
            if (map[i - 1, j] == 0 && cont == 0 ) { i--; cont++; }
        }
        if (map[i, j] == 1)
        {
        }
    }
}

public int[] Position(int[,] map)
{
    int[] pos = { 0, 0 };

    for (int i = 0; i < map.GetLength(0) && map[pos[0], pos[1]] == 1; i++)
    {
        for (int j = 0; j < map.GetLength(1) && map[pos[0], pos[1]] == 1; j++)
        {
            pos[0] = i;
            pos[1] = j;
        }
    }
    return pos;
}

它显然有一些错误。请发送一些反馈!也许对这种代码有一定经验的人可以帮我一点忙 :D。

Edit1:很抱歉我没有指定语言。这是 C#。 也要进行快速更新: 我尝试创建另一个 int[,] (与第一个完全相同),每次我的“探索者”经过任何(x,y)时,我都会将一个(1)添加到(第二个,复制的那个)的这个位置) 大批。这样我就可以制作一种方法来识别我何时完全“探索” map 。 此外,使用这个“复制”数组,如果我处于探索者之前的位置,我可以选择另一个方向(该点在第二个数组中不会是 0,实际上,每次探索者经过那个点,它会将一 (1) 添加到数组中的该位置)。这个想法是让探险家在经过同一地点一次、两次或更多次时表现不同……但这一切都不起作用。我仍然在无休止地循环......

EDIT2:现在我有了这段代码:

    public void goThrough(int[,] map, int[] pos, bool[,] visited)
    {
        int x = pos[0];
        int y = pos[1];
        visited[x, y] = true;
        Console.WriteLine("Position -> Column: {0} || Row: {1}", x, y);
        // ShowAtPosition(x, y)
        if (y > 0 && map[x, y - 1] == 0 && !visited[x, y - 1])
        {
            goThrough(map, new[] { x, y + 1 },visited); // north
            // ShowAtPosition(x, y)
        }
        if (x < map.GetLength(0) - 1 && map[x + 1, y] == 0 && !visited[x + 1, y])
        {
            goThrough(map, new[] { x + 1, y }, visited); // east
            // ShowAtPosition(x, y)
        }
        if (y < map.GetLength(1) - 1 && map[x, y + 1] == 0 && !visited[x, y + 1])
        {
            goThrough(map, new[] { x, y - 1 }, visited); // south
            // ShowAtPosition(x, y)
        }
        if (x > 0 && map[x - 1, y] == 0 && !visited[x - 1, y])
        {
            goThrough(map, new[] { x - 1, y }, visited); // west
            // ShowAtPosition(x, y)
        }
        if (NoZeros(visited)) { Console.WriteLine("I went through everything!"); Console.ReadLine(); }


    }

请注意我放置的 WriteLine,因此我可以跟踪此递归方法的每次迭代。 这是输出:

Position -> Column: 1 || Row: 31
Position -> Column: 2 || Row: 31
Position -> Column: 3 || Row: 31
Position -> Column: 4 || Row: 31
Position -> Column: 5 || Row: 31
Position -> Column: 6 || Row: 31
Position -> Column: 7 || Row: 31
Position -> Column: 8 || Row: 31
Position -> Column: 9 || Row: 31
Position -> Column: 10 || Row: 31
Position -> Column: 10 || Row: 32
Position -> Column: 11 || Row: 31
Position -> Column: 12 || Row: 31
Position -> Column: 13 || Row: 31
Position -> Column: 13 || Row: 32
Position -> Column: 14 || Row: 31
Position -> Column: 15 || Row: 31
Position -> Column: 16 || Row: 31
Position -> Column: 16 || Row: 32
Position -> Column: 17 || Row: 31
Position -> Column: 18 || Row: 31
Position -> Column: 19 || Row: 31
Position -> Column: 20 || Row: 31
Position -> Column: 21 || Row: 31
Position -> Column: 22 || Row: 31
Position -> Column: 23 || Row: 31
Position -> Column: 24 || Row: 31
Position -> Column: 25 || Row: 31
Position -> Column: 25 || Row: 30
Position -> Column: 1 || Row: 30

所以,首先,这个方法没有走完所有的迷宫,甚至没有走近(是的,0 都是相连的)。

其次,在最后一次迭代中(输出的最后两行),探险家从 (25,30)“传送”到 (1,30)。

顺便说一句,这是图片:

White =0's & Black =1's

最佳答案

起初听起来您只是在寻找 maze solving algorithm ;类似于左手壁随动器。

但是,您并不是在寻找走出迷宫的路径,而是在寻找访问具有相同值的所有连续位置。所以基本上你可以使用 flood fill algorithm .

唯一的问题是您可能有多个未连接的 0 池(除非您的原始位图被构造为所有 0 都将被连接,在这种情况下您可以确保只有将它们放在 0 个单元格上才能访问所有键)。因此,您可能需要从多个起点进行洪水填充,以确保覆盖所有内容。

关于c# - 遍历 0's and 1' 的 int[,] 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134818/

相关文章:

c# - MassTransit 和 .NET Core DI - 如何使用无参数构造函数解决依赖关系?

时间:2019-03-07 标签:c#奇怪DictionaryContainsKey或StringComaprer

c++ - 'int' 的问题

Java:如何从字符串和整数计算答案

ios - 写入plist文件而不写入

c# - 试图了解如何在 UI 线程上调用它

C# 使用反射访问窗口属性

c - 传递一个二维结构数组

c - 如何在 C 程序中声明、定义和调用函数,该函数必须具有 1 )"fill"3D 数组和 2 )"read"3D 字符数组

Java 字节数组并将 int 复制到其中