Matrix 矩阵8x5问题可以';解决不了

Matrix 矩阵8x5问题可以';解决不了,matrix,implementation,calculated-columns,Matrix,Implementation,Calculated Columns,我只是在学习矩阵和算法。我在一个我还不能解决的问题上反复思考。问题是, 由整数值组成的矩阵 #[0 0 0 2 2] #|1 1 7 2 2| #|2 2 7 2 1| #|2 1 7 4 4| #|2 7 7 4 4| #|4 6 6 0 4| #|4 4 6 4 4| #[4 4 6 4 4] 当几个相同的值连接在一起(水平或垂直)时,定义了一个区域。例如,通过上述矩阵,我们可以找到以下区域 #[0 0 0 * *] #|* * * * *| #|* * * * *| #|* * *

我只是在学习矩阵和算法。我在一个我还不能解决的问题上反复思考。问题是,

由整数值组成的矩阵

#[0 0 0 2 2]
#|1 1 7 2 2|
#|2 2 7 2 1|
#|2 1 7 4 4|
#|2 7 7 4 4|
#|4 6 6 0 4|
#|4 4 6 4 4|
#[4 4 6 4 4]  
当几个相同的值连接在一起(水平或垂直)时,定义了一个区域。例如,通过上述矩阵,我们可以找到以下区域

#[0 0 0 * *]
#|* * * * *|
#|* * * * *|
#|* * * * *|
#|* * * * *|
#|* * * * *|
#|* * * * *|
#[* * * * *]

#[* * * 2 2]
#|* * * 2 2|
#|* * * 2 *|
#|* * * * *|
#|* * * * *|
#|* * * * *|
#|* * * * *|
#[* * * * *]

#[* * * * *]
#|1 1 * * *|
#|* * * * *|
#|* * * * *|
#|* * * * *|
#|* * * * *|
#|* * * * *|
#[* * * * *]
问题在于实现“Find Count Element Of maxist Area”(查找最大面积的元素计数)功能,该功能查找最大面积(由最大元素数组成的区域),并返回该区域的元素数。例如,对于上述矩阵,最大面积为:

#[* * * * *]
#|* * * * *|
#|* * * * *|
#|* * * 4 4|
#|* * * 4 4|
#|* * * * 4|
#|* * * 4 4|
#[* * * 4 4]
因此函数将返回9(该区域由9个元素组成)。

关于这个问题有什么帮助吗?我该怎么解决?最好的办法


谢谢

好的!我忍不住要做你的家庭作业

下面的C#代码使用递归函数计算给定矩阵单元的所有未访问邻居。主程序循环遍历所有行和列,以查找具有相同内容的最多邻居的单元格

namespace akArrayCluster
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] a = new int[,] 
            {
                {0, 0, 0, 2, 2},
                {1, 1, 7, 2, 2},
                {2, 2, 7, 2, 1},
                {2, 1, 7, 4, 4},
                {2, 7, 7, 4, 4},
                {4, 6, 6, 0, 4},
                {4, 4, 6, 4, 4},
                {4, 4, 6, 4, 4}
            };
            int row;
            int col;
            int res = getBiggestArea(a, out row, out col);

            o("");
            o("Biggest area has " + res + " cells");
            o("Top-left cell is [" + row + ";" + col + "]");
        }

        static int getBiggestArea(int[,] a, out int row, out int col)
        {
            int opt = 0;
            int cnt;
            int rows = a.GetLength(0);
            int cols = a.GetLength(1);
            bool[,] visited = new bool[rows, cols];

            row = col = -1;

            for (int r = 0; r < rows; r++)
                for (int c = 0; c < cols; c++)
                {
                    setAllToFalse(visited, rows, cols);

                    cnt = getNeighbourCount(a[r, c], a, visited, r, c);
                    if (cnt > opt)
                    {
                        opt = cnt;
                        row = r;
                        col = c;
                    }
                }

            setAllToFalse(visited, rows, cols);   //  edited col --> cols
            getNeighbourCount(a[row, col], a, visited, row, col);
            showSolution(a, visited, rows, cols);

            return opt;
        }

        static void setAllToFalse(bool[,] v, int rows, int cols)
        {            
            for (int row = 0; row < rows; row++)
                for (int col = 0; col < cols; col++)
                    v[row, col] = false;
        }

        static void showSolution(int[,] a, bool[,] visited, int rows, int cols)
        {
            string s;

            o("");
            for (int row = 0; row < rows; row++)
            {
                s = "";
                for (int col = 0; col < cols; col++)
                {
                    s += " " + (visited[row, col] ? a[row, col].ToString() : ".");
                }

                o(s);
            }
        }

        static int getNeighbourCount(int ca, int[,] a, bool[,] visited, 
                                     int row, int col)
        {
            int nc = 0;
            int rows = a.GetLength(0);
            int cols = a.GetLength(1);

            if ((row >= 0) && (row < rows) && 
                (col >= 0) && (col < cols) && 
                (ca == a[row, col]) && !visited[row, col])
            {
                visited[row, col] = true;

                nc = 1
                    + getNeighbourCount(ca, a, visited, row, col - 1)
                    + getNeighbourCount(ca, a, visited, row - 1, col)
                    + getNeighbourCount(ca, a, visited, row, col + 1)
                    + getNeighbourCount(ca, a, visited, row + 1, col);
            }

            return nc;
        }

        static void o(string s)
        {
            System.Console.WriteLine(s);
        }
    }
}
第二个示例(参见下面的注释):


实际上,我必须实现该功能,并将返回9(该区域由9个元素组成),请举例说明以下问题!非常感谢你,阿克塞尔先生,这对我来说是一个很好的例子,所以我将学习这个。不客气!请点击“接受”按钮来奖励我的努力。对不起,我没有看到:)那么最大矩形区域的元素数量呢,阿克塞尔先生?我必须在功能上更改哪个区域?再次感谢。例如,这个矩阵,
[0222]| 222 | 45622 | 09872 |
 . . . . .
 . . . . .
 . . . . .
 . . . 4 4
 . . . 4 4
 . . . . 4
 . . . 4 4
 . . . 4 4

Biggest area has 9 cells
Top-left cell is [3;3]
 . 2 2 2 2
 2 2 2 2 2
 . . . 2 2
 . . . . 2

Biggest area has 12 cells
Top-left cell is [0;1]