-3

我有一个函数来检查一个数组的邻居,如果该元素等于 1。X 是找到的每个邻居,v[l] 是每个 0 的位置。我有这个代码的问题,每次给我“索引超出了数组的范围”,我不知道还能做什么。

 public int modificari(int i,int j,int n,int m)
    {
        int x = 0;
        v = new int[n];
        l=0;
        if (mat[i, j] == 1)
        {
            if (j++ < m)
            {
                if (mat[i, j++] == 1)
                    x++;
                else
                {
                    v[l] = i * n + j + 2;
                    l++;
                }
            }
            if (j++ < m && i++ < n)
            {
                if (mat[i++, j++] == 1)
                    x++;
                else
                {
                    v[l] = (i + 1) * n + j + 2;
                    l++;
                }
            }
            if (i++ < n)
            {
                if (mat[i++, j] == 1)
                    x++;
                else
                {
                    v[l] = (i + 1) * n + j + 1;
                    l++;
                }
            }
            if (j-- >= 0 && i++ < n)
            {
                if (mat[i++, j--] == 1)
                    x++;
                else
                {
                    v[l] = (i + 1) * n + j;
                    l++;
                }
            }
            if (j-- >= 0)
            {
                if (mat[i, j--] == 1)
                    x++;
                else
                {
                    v[l] = i * n + j;
                    l++;
                }
            }
            if (j-- >= 0 && i-- >= 0)
            {
                if (mat[i--, j--] == 1)
                    x++;
                else
                {
                    v[l] = (i - 1) * n + j;
                    l++;
                }
            }
            if (i-- >= 0)
            {
                if (mat[i--, j] == 1)
                    x++;
                else
                {
                    v[l] = (i - 1) * n + j + 1;
                    l++;
                }
            }
            if (j < n && i-- >= 0)
            {
                if (mat[i--, j++] == 1)
                    x++;
                else
                {
                    v[l] = (i - 1) * n + j + 2;
                    l++;
                }
            }
            if (x < 2 && x > 3)
                return 1;
            else
                return random();
        }
        return x;
    }
4

1 回答 1

4

那是一团糟。即使对于经验丰富的编码人员来说,也很难理解。为了可读性,通常不鼓励使用单字母变量名和内联 ++ 运算符。

我很快尝试根据我对您要达到的目标的最佳猜测来重写您的功能。我希望你能看到一种不同的方法来解决更适合你的问题。

注意:我根本没有测试这段代码,它可能有编译错误。

public struct Point
{
    public int X;
    public int Y;
    public Point( int x, int y )
    {
        X = x;
        Y = y;
    }
}

public class Whatever
{
    // ...

    // Here is a list of the positions of all the neighbours whose values are
    // zero.
    List<Point> zeroPositions = new List<Point>();

    // ...

    public int Modificari(int pointX, int pointY)
    {
        // Determine dimensions of array.
        int height = mat.GetLength(0);
        int width = mat.GetLength(1);

        // Find the minimum and maximum positions bounded by array size. (So we
        // don't try to look at cell (-1, -1) when considering the neighbours of
        // cell (0, 0) for instance.
        int left = Math.Max( pointX - 1, 0 );
        int right = Math.Min( pointX + 1, width );

        int top = Math.Max( pointY - 1, 0 );
        int bottom = Math.Min( pointY + 1, height );

        // This is the number of neighbours whose value is 1.
        int oneCount = 0;

        zeroPositions.Clear();

        for( int y = top; y <= bottom; y++ )
        {
            for( int x = left; x <= right; x++ )
            {
                if( mat[x, y] == 1 )
                {
                    oneCount++;
                }
                else if( mat[x, y] == 0 )
                {
                    zeroPositions.Add( new Point( x, y ) );
                }
            }
        }

        return oneCount;
    }

    //...
}

另外,我真的建议您不要在函数中做太多事情。尝试使用不同的函数来获取 1 的位置并返回 0 的数量。

于 2014-01-20T13:29:19.527 回答