Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 计算扇区中的单元数_Java_Arrays_Algorithm_Recursion - Fatal编程技术网

Java 计算扇区中的单元数

Java 计算扇区中的单元数,java,arrays,algorithm,recursion,Java,Arrays,Algorithm,Recursion,问题陈述-计算扇区中的单元格 考虑一个二维单元格网格,每个单元格可能是空的,也可能是满的。连接的填充单元格形成一个扇区。如果两个电池在水平、垂直或对角方向上彼此相邻,则称为连接。网格上可能有几个扇区。您的工作是在网格上找到最大的扇区(以单元数计) 网格是由0和1组成的二维数组,每个数组由0和1组成。1表示单元格已填充,0表示空单元格 下面的二维阵列显示了一个具有3个扇区的栅格(最大的包含5个单元格) 110000 011000 001000 000001 100001 010011 确定给定

问题陈述-计算扇区中的单元格

考虑一个二维单元格网格,每个单元格可能是空的,也可能是满的。连接的填充单元格形成一个扇区。如果两个电池在水平、垂直或对角方向上彼此相邻,则称为连接。网格上可能有几个扇区。您的工作是在网格上找到最大的扇区(以单元数计)

网格是由0和1组成的二维数组,每个数组由0和1组成。1表示单元格已填充,0表示空单元格

下面的二维阵列显示了一个具有3个扇区的栅格(最大的包含5个单元格)

  • 110000
  • 011000
  • 001000
  • 000001
  • 100001
  • 010011
确定给定栅格的最大扇区的大小

class Cell {
    int value;
    boolean checked;
    Cell(int value,boolean checked) {
        this.value = value;
        this.checked = checked;
    }
}

public class CountSectorSize {
    int traverseGrid(Cell[][] a) {
        int tempSectorSize, sectorSize = 0;
        // Cell[][] a = null;
        for (int i = 0; i < a.length; ++i)
            for (int j = 0; j < a[i].length; ++j) {
                if ((a[i][j].value == 0) || (a[i][j].checked == true))
                    continue;
                else {
                    tempSectorSize = findSectorSize(a, i, j);
                    System.out.println (tempSectorSize);
                    if (tempSectorSize > sectorSize)
                        sectorSize = tempSectorSize;
                    ++j;
                }
            }
        return sectorSize;
    }
int findSectorSize(Cell[][] a, int i, int j) {
    if ((i >= 0) && (i < a.length) && (j >= 0) && (j < a[i].length)) { // Check
        // Boundaries check
        if ((a[i][j].value == 0) || (a[i][j].checked == true))
            return 0;
        else {
            a[i][j].checked = true;
            int thisCellScore = 1;
            //Calling adjacent cells on the top, bottom and right of the cell as other cell would have been covered in previous calls.
            int top = findSectorSize(a, i - 1, j);
            int topRight = findSectorSize(a, i - 1, j + 1);
            int after = findSectorSize(a, i, j + 1);
            int bottomRight = findSectorSize(a, i + 1, j + 1);
            int bottom = findSectorSize(a, i + 1, j);
            return thisCellScore+ top + topRight + after + bottomRight
                    + bottom;
        }
    } else {
        return 0;
    }
}

/**
 * @param args
 */
public static void main(String[] args) {
    CountSectorSize countSectorSize = new CountSectorSize();
    // Sample Grid
    Cell[][] sample = {{new Cell(1,false),new Cell(0,false),new Cell(0,false),new Cell(1,false),new Cell(1,false)},
            {new Cell(1,false),new Cell(1,false),new Cell(0,false),new Cell(0,false),new Cell(1,false)},
            {new Cell(0,false),new Cell(1,false),new Cell(0,false),new Cell(0,false),new Cell(1,false)},
            {new Cell(1,false),new Cell(1,false),new Cell(1,false),new Cell(0,false),new Cell(1,false)},
            {new Cell(1,false),new Cell(0,false),new Cell(0,false),new Cell(0,false),new Cell(1,false)},
            {new Cell(1,false),new Cell(1,false),new Cell(0,false),new Cell(0,false),new Cell(1,false)},
            {new Cell(0,false),new Cell(1,false),new Cell(0,false),new Cell(0,false),new Cell(1,false)},
            {new Cell(1,false),new Cell(1,false),new Cell(0,false),new Cell(0,false),new Cell(0,false)}};
    System.out.println("Final Result \t "+countSectorSize.traverseGrid(sample));
    }
}
类单元{
int值;
布尔检查;
单元格(int值,布尔值已选中){
这个值=值;
this.checked=checked;
}
}
公共类CountSectorSize{
int traverseGrid(单元格[][]a){
int tempSectorSize,sectorSize=0;
//单元格[][]a=null;
对于(int i=0;i扇区大小)
扇区大小=临时扇区大小;
++j;
}
}
回归部门化;
}
int FindSector大小(单元格[][]a、int i、int j){
如果((i>=0)&&(i=0)&&(j
使用main方法中的这个示例输入,我应该得到13作为输出。但实际输出是8。
非常感谢您的帮助。

需要递归调用所有相邻的单元格,因为您无法确定您所在的单元格的位置 当前调用已从发生。尝试添加

            int bottomLeft = findSectorSize(a, i + 1, j - 1);
            int before = findSectorSize(a, i, j - 1);
            int topLeft = findSectorSize(a, i - 1, j - 1);
            return thisCellScore + top + topRight + after + bottomRight
                    + bottom + bottomLeft + before + topLeft;

如果你看左上部分的话,你应该得到5作为输出,因为最大的连接扇区有5个1。感谢@zenwraight I引用main方法中的示例输入。