Javascript-查找矩阵中的最大区域

Javascript-查找矩阵中的最大区域,javascript,matrix,multidimensional-array,area,Javascript,Matrix,Multidimensional Array,Area,任务是:找出矩阵中相同数字的最大面积 矩阵是硬编码的,到目前为止,我有以下代码 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script type="text/javascript"> /* ---- Function that prints a matrix and finds the largest area and

任务是:找出矩阵中相同数字的最大面积

矩阵是硬编码的,到目前为止,我有以下代码

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
    /* ---- Function that prints a matrix and finds the largest area and its value  ---- */
            function LargestAreaMatrix() {

                var matrix = [[1,3,2,2,2,4],
                              [4,3,2,2,4,4],
                              [4,4,1,2,3,3],
                              [4,3,1,2,3,1],
                              [4,3,3,2,2,1]];

                var arrSize = matrix.length;
                var itemSize = matrix[0].length;
                var counter = {};

                for (var i = 0; i < arrSize; i++ ){
                    for (var j = 0; j < itemSize; j++) {
                                //if the current element is equal to the next element   
                                if (matrix[i][j] == matrix[i][j+1]) {
                                    //to the object "key" is assigned the current value of the matrix and the "value" is incrementing till the condition is true  
                                    counter[matrix[i][j]] = 1 + (counter[matrix[i][j]] || 0);
                                    console.log("Right neighbor: "+ matrix[i][j] + " - ij: " + i + " " + j);
                                } 
                                if (typeof(matrix[i+1]) != "undefined" ) {
                                    //if the current element is equal to the bottom element
                                    if (matrix[i][j] == matrix[i+1][j]) {
                                        // the value of the specific key is incrementing
                                        counter[matrix[i][j]] = 1 + (counter[matrix[i][j]] || 0);
                                        console.log("Down neighbor:  "+ matrix[i][j] + " - ij: " + i + " " + j);
                                    }
                                } else {
                                    console.log("Not a neighbor: "+ matrix[i][j] + " - ij: " + i + " " + j);
                                } 
                    }//end of for j
                }//end of for i


                console.log("Neighbors count: ");
                console.log(counter);   


                //Printing the array with an html table
                var table = '<table border="0">';
                for (var i = 0; i < matrix.length; i++) {
                    table += '<tr>';
                    for (var j = 0; j < matrix[i].length; j++) {
                        table += '<td>' + matrix[i][j] + '</td>';
                    }
                    table += '</tr>';
                }
                table += '</table>';

                document.getElementById('matrix').innerHTML = table;
            }
</script>
</head>
<body>
<p></p>
<p><a href="#" onClick="LargestAreaMatrix();">Largest Area Matrix</a></p>
<label name="matrix" id="matrix"> </label>
</body>
</html>

/*----打印矩阵并查找最大面积及其值的函数--*/
函数largestreamatrix(){
var矩阵=[[1,3,2,2,2,4],
[4,3,2,2,4,4],
[4,4,1,2,3,3],
[4,3,1,2,3,1],
[4,3,3,2,2,1]];
var arrSize=矩阵长度;
var itemSize=矩阵[0]。长度;
var计数器={};
对于(变量i=0;i

我在矩阵中做了两个循环,检查右下邻域。如果有-我使用一个对象来放置一个键的矩阵值,而对象值随着键的计数而递增。 最后,我得到了每个值的邻居数

我的问题:出于某种原因,在外部循环中,“i”命中4(矩阵大小),第二个if和else都被执行。为什么会这样? 此外,我仍在试图找出如何使其仅计算最大面积,而不是所有邻居的特定值

我将感谢任何帮助。谢谢

更新:

所以结果比我想象的要简单:) 下面是我用来计算每个区域大小的递归函数:

                function findNeighbors(row, col, item){
                if(row < 0 || col < 0 || row > (arrSize - 1) || col > (itemSize - 1)) {
                    return 0;
                }

                if(matrixZero[row][col] == 1) {
                    return 0;
                }

                if(item == matrix[row][col]){
                    matrixZero[row][col] = 1;
                    tempCount = 1 + (findNeighbors(row, col+1, matrix[row][col]) || 0) + (findNeighbors(row+1, col, matrix[row][col]) || 0) + (findNeighbors(row, col-1, matrix[row][col]) || 0) + (findNeighbors(row-1, col, matrix[row][col]) || 0);
                    return tempCount;
                }
            }
函数findNeighbors(行、列、项){
如果(行<0 | |列<0 | |列>(arrSize-1)| |列>(itemSize-1)){
返回0;
}
如果(矩阵零[行][列]==1){
返回0;
}
如果(项目==矩阵[行][列]){
矩阵零[行][列]=1;
tempCount=1+(findNeighbors(行,列+1,矩阵[行][col])| | 0)+(findNeighbors(行+1,列,矩阵[行][col])| | 0)+(findNeighbors(行,列-1,矩阵[行][col])| | 0);
返回tempCount;
}
}
首先,我检查当前项是否在矩阵范围内,如果不在,则将-0添加到tempCount值。然后我检查是否已经访问了该项目,如果是,则将-0添加到temp中。然后,如果矩阵中的项目既没有访问过,也没有访问过,我将其检查为已访问,并将1添加到临时值中,以此类推

然后在一个简单的例子中,我比较tempCount值和当前的max值,如果temp高于max,我就切换它们


谢谢大家的帮助

我运行了您的代码,执行的不是第二个if和else,而是第二个if语句的第一个if和else部分


因此,在第一个if语句中,如果发现矩阵(i,j)是右邻域,然后在第二个if语句中继续测试下邻域。但是,当i=4时,此时您试图访问行i+1时,必须生成未定义的行,因为矩阵中没有第6行。这就是为什么第一个if和第二个if的else部分都被命中的原因。我猜逻辑有问题。

我不认为您的代码做了什么奇怪的事情。但也许你没有用最好的逻辑来解释。这是一个工作函数。如果您是初学者,可以阅读此代码

function LargestAreaMatrix() {

  var matrix = [[1,3,2,2,2,4],
                [4,3,2,2,4,4],
                [4,4,1,2,3,3],
                [4,3,1,2,3,1],
                [4,3,3,2,2,1]];

  var cache = {};

  function pulse(x, y) {
    var queue = [],
        visited = {},
        size = 0;

    // Current cell is the first element
    queue.push({
      'x' : x,
      'y' : y
    });
    visited[x + ' ' + y] = true;
    size = 1;

    function test(x, y, value) {
      if (!visited[x + ' ' + y] && y >= 0 && y < matrix.length && x >= 0 && x < matrix[y].length && matrix[y][x] == value) {
        queue.push({
          'x' : x,
          'y' : y
        });
        visited[x + ' ' + y] = true;
        size += 1;
      }
    }

    while (queue.length) {
      var cell = queue.pop(),
          value = matrix[cell.y][cell.x];

      // Add neighbors of the same value to the queue
      test(cell.x - 1, cell.y, value);
      test(cell.x + 1, cell.y, value);
      test(cell.x, cell.y - 1, value);
      test(cell.x, cell.y + 1, value);
    }

    // Cache the size for all visited cells for performances
    for (var key in visited) {
      cache[key] = size;
    }

    return size;
  }

  var max = 0;

  for (var y = 0; y < matrix.length; ++y) {
    for (var x = 0; x < matrix[y].length; ++x) {
      if (!cache[x + ' ' + y]) {
        var size = pulse(x, y);

        if (size > max) {
          max = size;
        }
      }
    }
  }

  console.log('Largest area size', max);
}
函数largestreamatrix(){
var矩阵=[[1,3,2,2,2,4],
[4,3,2,2,4,4],
[4,4,1,2,3,3],
[4,3,1,2,3,1],
[4,3,3,2,2,1]];
var cache={};
功能脉冲(x,y){
变量队列=[],
访问={},
尺寸=0;
//当前单元格是第一个元素
queue.push({
“x”:x,
“y”:y
});
访问[x+“”+y]=真;
尺寸=1;
功能测试(x、y、值){
如果(!已访问[x+“”+y]&&y>=0&&y=0