【发布时间】:2020-05-02 21:17:45
【问题描述】:
在给定一个 0 和 1 的多维数组的情况下,我需要找到簇的数量。簇由在上、下、左或右方向非对角线相邻的 1 定义。表示以下多维数组有一个簇:
01000
01000
下一个有两个:
00100
00010
我已经编写了代码(请参阅下面的“我的代码”),但是在使用几个用例进行编译和测试时,它未能提供一致的准确答案:
当运行以下命令时,它应该产生以下结果:
运行:
package org.example;
public class App
{
public static void main( String[] args )
{
FindCluster findCluster = new FindCluster();
FindCluster findCluster0 = new FindCluster();
FindCluster findCluster1 = new FindCluster();
FindCluster findCluster2 = new FindCluster();
// Attempt 1
int myArr[][] = {
{1, 1, 0, 0, 1},
{0, 1, 0, 0, 1},
{1, 0, 0, 1, 1},
{0, 0, 0, 0, 0},
{1, 0, 1, 1, 0}
};
int numOfClusters = findCluster.findNumberOfClusters(5, 5, myArr);
System.out.println("The number of clusters found in your Array is: " + numOfClusters);
// Attempt 2
int myArr0[][] = {
{1, 0, 1, 0, 1},
{0, 1, 1, 0, 1},
{1, 1, 0, 1, 0},
{0, 0, 1, 0, 1},
{1, 0, 1, 1, 0}
};
int numOfClusters0 = findCluster0.findNumberOfClusters(5, 5, myArr0);
System.out.println("The number of clusters found in your Array is: " + numOfClusters0);
// Attempt 3
int myArr1[][] = {
{1, 1, 0, 1, 1},
{0, 1, 0, 0, 1},
{1, 0, 1, 0, 1},
{0, 0, 1, 0, 1},
{0, 0, 1, 1, 0}
};
int numOfClusters1 = findCluster1.findNumberOfClusters(5, 5, myArr1);
System.out.println("The number of clusters found in your Array is: " + numOfClusters1);
// Attempt 4
int myArr2[][] = {
{1, 1, 0, 1, 1},
{0, 1, 1, 0, 1},
{1, 0, 1, 0, 1},
{0, 0, 1, 0, 1},
{1, 0, 1, 1, 1}
};
int numOfClusters2 = findCluster2.findNumberOfClusters(5, 5, myArr2);
System.out.println("The number of clusters found in your Array is: " + numOfClusters2);
}
}
预期结果:
The number of clusters found in your Array is: 5
The number of clusters found in your Array is: 7
The number of clusters found in your Array is: 4
The number of clusters found in your Array is: 3
实际结果:
The number of clusters found in your Array is: 5
The number of clusters found in your Array is: 6
The number of clusters found in your Array is: 3
The number of clusters found in your Array is: 4
我的代码:
package org.example;
public class FindCluster {
private int clusters = 0;
private boolean gridCheckArr[][] = null;
private int gridClusterArr[][] = null;
public void setGridCheckArr(int rows, int columns) {
this.gridCheckArr = new boolean[rows][columns];
}
public void setGridClusterArr(int gridClusterArr[][]) { this.gridClusterArr = gridClusterArr; }
public void setGridCheckInitialValues(){
for(int r = 0;r < gridCheckArr.length; r++){
for(int c = 0; c < gridCheckArr[r].length; c++){
gridCheckArr[r][c] = false;
}
}
}
public Integer findNumberOfClusters(int rows, int columns, int grid[][]) {
// Using a method to set a grid to check if location has been visited
setGridCheckArr(rows, columns);
// Using a method to set the initial values to false for the grid being checked
setGridCheckInitialValues();
// Using a method to set the the classes grid to the grid passed to the method called
setGridClusterArr(grid);
// Using a method that performs the checks and computation of the clusters
gridCheck();
return this.clusters;
}
public void gridCheck() {
for(int r = 0; r < gridCheckArr.length; r++) {
for(int c = 0; c < gridCheckArr[r].length; c++) {
if(r == 0 && c == 0){
checkFirstRowBeginning(r, c);
} else if (r == 0 && c == gridCheckArr[r].length - 1) {
checkFirstRowEnd(r, c);
} else if ( r == 0 && c > 0) {
checkFirstRow(r, c);
} else if (r == gridCheckArr.length - 1 && c == 0) {
checkLastRowBeginning(r, c);
} else if (r == gridCheckArr.length -1 && c == gridCheckArr[r].length -1) {
checkLastRowEnd(r, c);
} else if (r == gridCheckArr.length -1 && c > 0) {
checkLastRow(r, c);
} else if (r > 0 && c == 0) {
checkRowBeginning(r, c);
} else if (r > 0 && c == gridCheckArr[r].length -1) {
checkRowEnd(r, c);
} else if (r > 0 && c > 0) {
checkRow(r, c);
} else {
System.out.println("Oops something went wrong trying to find a grid check!!!!");
}
}
}
}
public void checkFirstRow(int row, int column) { // checks Left, Right, Down
int arrIndexValue = this.gridClusterArr[row][column];
switch(arrIndexValue) {
case 0:
break;
case 1:
this.gridCheckArr[row][column] = true;
if(this.gridCheckArr[row][column - 1] != true && this.gridCheckArr[row][column + 1] != true && this.gridCheckArr[row + 1][column] != true) {
this.clusters++;
}
break;
default:
System.out.println("Oops something went wrong when trying to check grid at First Row case not able to be handled array index value is: " + arrIndexValue);
}
}
public void checkFirstRowBeginning(int row, int column) { // checks Right, Down
int arrIndexValue = this.gridClusterArr[row][column];
switch(arrIndexValue) {
case 0:
break;
case 1:
this.gridCheckArr[row][column] = true;
if(this.gridCheckArr[row][column + 1] != true && this.gridCheckArr[row + 1][column] != true) {
this.clusters++;
}
break;
default:
System.out.println("Oops something went wrong when trying to check grid at First Row Beginning case not able to be handled array index value is: " + arrIndexValue);
}
}
public void checkFirstRowEnd(int row, int column) { // checks Left, Down
int arrIndexValue = this.gridClusterArr[row][column];
switch(arrIndexValue) {
case 0:
break;
case 1:
this.gridCheckArr[row][column] = true;
if(this.gridCheckArr[row][column - 1] != true && this.gridCheckArr[row + 1][column] != true) {
this.clusters++;
}
break;
default:
System.out.println("Oops something went wrong when trying to check grid at First Row End case not able to be handled array index value is: " + arrIndexValue);
}
}
public void checkRow(int row, int column) { // checks Left, Right, Up, Down
int arrIndexValue = this.gridClusterArr[row][column];
switch(arrIndexValue) {
case 0:
break;
case 1:
this.gridCheckArr[row][column] = true;
if(this.gridCheckArr[row][column - 1] != true && this.gridCheckArr[row][column + 1] != true && this.gridCheckArr[row - 1][column] != true && this.gridCheckArr[row + 1][column] != true) {
if((this.gridCheckArr[row - 1][column - 1] != true && this.gridClusterArr[row][column - 1] != 1) && (this.gridCheckArr[row - 1][column + 1] != true && this.gridClusterArr[row][column + 1] != 1) && (this.gridCheckArr[row + 1][column - 1] != true && this.gridClusterArr[row][column - 1] != 1) && (this.gridCheckArr[row + 1][column + 1] != true && this.gridClusterArr[row][column + 1] != 1)) {
this.clusters++;
}
}
break;
default:
System.out.println("Oops something went wrong when trying to check grid at a Row case not able to be handled array index value is: " + arrIndexValue);
}
}
public void checkRowBeginning(int row, int column) { // checks Right, Up, Down
int arrIndexValue = this.gridClusterArr[row][column];
switch(arrIndexValue) {
case 0:
break;
case 1:
this.gridCheckArr[row][column] = true;
if(this.gridCheckArr[row][column + 1] != true && this.gridCheckArr[row - 1][column] != true && this.gridCheckArr[row + 1][column] != true) {
this.clusters++;
}
break;
default:
System.out.println("Oops something went wrong when trying to check grid at a Row Beginning case not able to be handled array index value is: " + arrIndexValue);
}
}
public void checkRowEnd(int row, int column) { // checks Left, Up, Down
int arrIndexValue = this.gridClusterArr[row][column];
switch(arrIndexValue) {
case 0:
break;
case 1:
this.gridCheckArr[row][column] = true;
if(this.gridCheckArr[row][column - 1] != true && this.gridCheckArr[row - 1][column] != true && this.gridCheckArr[row + 1][column] != true) {
this.clusters++;
}
break;
default:
System.out.println("Oops something went wrong when trying to check grid at a Row End case not able to be handled array index value is: " + arrIndexValue);
}
}
public void checkLastRow(int row, int column) { // checks Left, Right, Up
int arrIndexValue = this.gridClusterArr[row][column];
switch(arrIndexValue) {
case 0:
break;
case 1:
this.gridCheckArr[row][column] = true;
if(this.gridCheckArr[row][column - 1] != true && this.gridCheckArr[row][column + 1] != true && this.gridCheckArr[row - 1][column] != true) {
this.clusters++;
}
break;
default:
System.out.println("Oops something went wrong when trying to check grid at Last Row case not able to be handled array index value is: " + arrIndexValue);
}
}
public void checkLastRowBeginning(int row, int column) { // checks Right, Up
int arrIndexValue = this.gridClusterArr[row][column];
switch(arrIndexValue) {
case 0:
break;
case 1:
this.gridCheckArr[row][column] = true;
if(this.gridCheckArr[row][column + 1] != true && this.gridCheckArr[row - 1][column] != true) {
this.clusters++;
}
break;
default:
System.out.println("Oops something went wrong when trying to check grid at Last Row Beginning case not able to be handled array index value is: " + arrIndexValue);
}
}
public void checkLastRowEnd(int row, int column) { // checks Left, Up
int arrIndexValue = this.gridClusterArr[row][column];
switch(arrIndexValue) {
case 0:
break;
case 1:
this.gridCheckArr[row][column] = true;
if(this.gridCheckArr[row][column - 1] != true && this.gridCheckArr[row - 1][column] != true) {
this.clusters++;
}
break;
default:
System.out.println("Oops something went wrong when trying to check grid at Last Row End case not able to be handled array index value is: " + arrIndexValue);
}
}
}
当我在某些情况下手动跟踪它时,我明白为什么它会失败。但是,我一直在寻找一种可以解决该问题的算法。例如,在 myArr2 的尝试 4 中
int myArr2[][] = {
{1, 1, 0, 1, 1},
{0, 1, 1, 0, 1},
{1, 0, 1, 0, 1},
{0, 0, 1, 0, 1},
{1, 0, 1, 1, 1}
};
在第一列标识的第一个 1 上(索引 [0][ 0])。然后,当它到达第一列([0][3] 的索引)上标识的第三个 1 时,我的逻辑检查再次将集群计数增加 1。这是不正确的,因为它实际上在集群中连接到之前在第一行中标识的前两个 1(索引 [0][0] 和 [0][1]),如果您遵循整个二维数组中相邻的1。如何创建一个逻辑算法来实现这一点?
谁能改进我的代码以修复错误或提供更好/更有效的解决方案并解释它如何/为什么更好地工作?
【问题讨论】:
标签: java arrays algorithm sorting multidimensional-array