Java 计算具有变化的二维矩阵中的孤岛数
给定一个布尔2D矩阵,求孤岛数。一组相连的1构成一个孤岛 在这个岛屿数量计算问题的变体中,我们要计算完全被水包围的岛屿的数量。也就是说,不应计算边缘的1,也不应计算边缘的孤岛。我们只希望1的四周只有0 我尝试过使用流行的dfs技术,但做了一些修改。我不会遍历矩阵的所有边。这显然只满足少数情况。以下示例失败,因为它返回2而不是1: 每次我回溯时,我也尝试减少计数,但这显然是徒劳的,因为计数最终小于零。最后,我尝试减少计数,同时将下限设置为0。这只是始终返回0。我肯定错过了什么。有什么帮助吗 下面是代码:Java 计算具有变化的二维矩阵中的孤岛数,java,algorithm,depth-first-search,gaps-and-islands,Java,Algorithm,Depth First Search,Gaps And Islands,给定一个布尔2D矩阵,求孤岛数。一组相连的1构成一个孤岛 在这个岛屿数量计算问题的变体中,我们要计算完全被水包围的岛屿的数量。也就是说,不应计算边缘的1,也不应计算边缘的孤岛。我们只希望1的四周只有0 我尝试过使用流行的dfs技术,但做了一些修改。我不会遍历矩阵的所有边。这显然只满足少数情况。以下示例失败,因为它返回2而不是1: 每次我回溯时,我也尝试减少计数,但这显然是徒劳的,因为计数最终小于零。最后,我尝试减少计数,同时将下限设置为0。这只是始终返回0。我肯定错过了什么。有什么帮助吗 下面
class Islands {
// No of rows and columns
static final int ROW = 5, COL = 5;
int gcount;
// A function to check if a given cell (row, col) can
// be included in DFS
boolean isSafe(int M[][], int row, int col,
boolean visited[][])
{
// row number is in range, column number is in range
// and value is 1 and not yet visited
return (row >= 1) && (row < ROW-1) && (col >= 1) && // used to be row >= 0 && row < ROW && col >=0
(col < COL-1) && (M[row][col] == 1 && !visited[row][col]); //col < COL
}
// A utility function to do DFS for a 2D boolean matrix.
// It only considers the 8 neighbors as adjacent vertices
void DFS(int M[][], int row, int col, boolean visited[][])
{
// These arrays are used to get row and column numbers
// of 8 neighbors of a given cell
int rowNbr[] = new int[] { -1, -1, -1, 0, 0, 1, 1, 1 };
int colNbr[] = new int[] { -1, 0, 1, -1, 1, -1, 0, 1 };
// Mark this cell as visited
visited[row][col] = true;
// Recur for all connected neighbors
for (int k = 0; k < 8; ++k)
if (isSafe(M, row + rowNbr[k], col + colNbr[k], visited)) {
System.out.println("here");
++gcount;
DFS(M, row + rowNbr[k], col + colNbr[k], visited);
}
/*else {
gcount--;
if(gcount < 1) {
gcount = 0;
}
}*/
}
// The main function that returns count of islands in a given
// boolean 2D matrix
int countIslands(int M[][])
{
// Make a bool array to mark visited cells.
// Initially all cells are unvisited
boolean visited[][] = new boolean[ROW][COL];
// Initialize count as 0 and traverse through the all cells
// of given matrix
int count = 0;
for (int i = 1; i < ROW-1; ++i) //I changed this from ROW
for (int j = 1; j < COL-1; ++j) //I changed this from COL
if (M[i][j] == 1 && !visited[i][j]) // If a cell with
{ // value 1 is not
// visited yet, then new island found, Visit all
// cells in this island and increment island count
DFS(M, i, j, visited);
//++gcount;
}
return gcount;
}
类孤岛{
//行数和列数
静态最终整数行=5,列=5;
国际计算;
//用于检查给定单元格(行、列)是否可以
//包括在DFS中
布尔isSafe(int M[],int行,int列,
布尔值[][])
{
//行数在范围内,列数在范围内
//值为1且尚未访问
返回(行>=1)&&&(行=1)&&//用于行>=0&&row=0
(列
在countIslands
中,首先访问所有边缘单元格(顶行和底行、左列和右列)。这将标记所有可从边缘单元格访问的岛屿
for (int j = 0; j < COL; ++j)
{
if (M[0][j] == 1 && !visited[0][j])
DFS(M, 0, j, visited);
if (M[ROW - 1][j] == 1 && !visited[ROW - 1][j])
DFS(M, ROW - 1, j, visited);
}
for (int i = 1; i < ROW - 1; ++i) // I changed this from ROW
{
if (M[i][0] == 1 && !visited[i][0])
DFS(M, i, 0, visited);
if (M[i][COL - 1] == 1 && !visited[i][COL - 1])
DFS(M, i, COL - 1, visited);
}
我肯定错过了一些东西-是的,你的代码。发布,然后我们可能可以帮助你检查周围的8个元素吗?你的代码在哪里。很抱歉,我刚刚发布了代码@foobarah,我明白了。我们首先从边缘运行DFS。这有助于消除从边缘突出的每个岛。整洁!
int count = 0;
for (int i = 1; i < ROW - 1; ++i)
{
for (int j = 1; j < COL - 1; ++j)
{
if (M[i][j] == 1 && !visited[i][j])
{
DFS(M, i, j, visited);
count++;
}
}
}
int rowNbr[] = new int[] { -1, -1, -1, 0, 0, 1, 1, 1 };
int colNbr[] = new int[] { -1, 0, 1, -1, 1, -1, 0, 1 };