java - 对扇区中的细胞进行计数
问题描述
问题陈述 - 计算扇区中的单元格
考虑一个二维网格的单元格,每个单元格可能是空的或填充的。连接的填充单元形成一个扇区。如果两个单元在水平、垂直或对角线上彼此相邻,则称它们是连接的。网格上可能有多个扇区。你的工作是在网格上找到最大的扇区(就单元数而言)。
网格以 0 和 1 的二维数组形式给出,每个数组由 0 和 1 组成。1 表示单元格已填充,0 表示单元格为空。
下面的 2D 数组说明了一个具有 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));
}
}
在 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;
推荐阅读
- c# - 使用控制器检索列表中包含的数据会导致错误
- python - 如何将python数据框中一行中的所有元素移动一列?
- arrays - 使用 Powershell 从 json 文件中创建具有适当列和行的 CSV 文件
- postgresql - PostgreSQL 12.5 - 存储过程看不到用户定义的类型
- google-apps-script - 使用复选框清除表格中的一系列单元格
- powerbi - 如何从 powerbi 中的 RANKX 函数中删除过滤器?
- javascript - 如何在jquery中修改div内的html文档?
- python - 给出了一个字符串。字符串被分成三个连续符号的片段
- r - 按组汇总值并以其他两列为条件
- r - R 中的 npcdens 和 npudense 具有使用“np”包的生产函数