计算每个岛按行和按列分隔的岛数
给定一个只有两个可能值“X”和“O”的矩形矩阵。值“X”总是以矩形岛的形式出现,并且这些岛总是按行和按列由至少一行“O”隔开。请注意,岛屿只能对角相邻。计算给定矩阵中岛屿的数量。
例子:
mat[M][N] = {{'O', 'O', 'O'},
{'X', 'X', 'O'},
{'X', 'X', 'O'},
{'O', 'O', 'X'},
{'O', 'O', 'X'},
{'X', 'X', 'O'}
};
Output: Number of islands is 3
mat[M][N] = {{'X', 'O', 'O', 'O', 'O', 'O'},
{'X', 'O', 'X', 'X', 'X', 'X'},
{'O', 'O', 'O', 'O', 'O', 'O'},
{'X', 'X', 'X', 'O', 'X', 'X'},
{'X', 'X', 'X', 'O', 'X', 'X'},
{'O', 'O', 'O', 'O', 'X', 'X'},
};
Output: Number of islands is 4
我们强烈建议您最小化您的浏览器并首先自己尝试。
这个想法是计算给定矩阵的所有左上角。我们可以通过检查以下条件来检查“X”是否在左上角。
1) 如果正上方的单元格是“O”,则“X”是矩形的顶部
2)如果左边的单元格是“O”,则“X”是矩形的最左边
请注意,我们必须检查这两种情况,因为矩形岛中可能有多个顶部单元格和多个最左侧单元格。下面是上述想法的实现。
C++
// A C++ program to count the number of rectangular
// islands where every island is separated by a line
#include
using namespace std;
// Size of given matrix is M X N
#define M 6
#define N 3
// This function takes a matrix of 'X' and 'O'
// and returns the number of rectangular islands
// of 'X' where no two islands are row-wise or
// column-wise adjacent, the islands may be diagonally
// adjacent
int countIslands(int mat[][N])
{
int count = 0; // Initialize result
// Traverse the input matrix
for (int i=0; i
Java
// A Java program to count the number of rectangular
// islands where every island is separated by a line
import java.io.*;
class islands
{
// This function takes a matrix of 'X' and 'O'
// and returns the number of rectangular islands
// of 'X' where no two islands are row-wise or
// column-wise adjacent, the islands may be diagonally
// adjacent
static int countIslands(int mat[][], int m, int n)
{
// Initialize result
int count = 0;
// Traverse the input matrix
for (int i=0; i
Python3
# A Python3 program to count the number
# of rectangular islands where every
# island is separated by a line
# Size of given matrix is M X N
M = 6
N = 3
# This function takes a matrix of 'X' and 'O'
# and returns the number of rectangular
# islands of 'X' where no two islands are
# row-wise or column-wise adjacent, the islands
# may be diagonally adjacent
def countIslands(mat):
count = 0; # Initialize result
# Traverse the input matrix
for i in range (0, M):
for j in range(0, N):
# If current cell is 'X', then check
# whether this is top-leftmost of a
# rectangle. If yes, then increment count
if (mat[i][j] == 'X'):
if ((i == 0 or mat[i - 1][j] == 'O') and
(j == 0 or mat[i][j - 1] == 'O')):
count = count + 1
return count
# Driver Code
mat = [['O', 'O', 'O'],
['X', 'X', 'O'],
['X', 'X', 'O'],
['O', 'O', 'X'],
['O', 'O', 'X'],
['X', 'X', 'O']]
print("Number of rectangular islands is",
countIslands(mat))
# This code is contributed by iAyushRaj
C#
// A C# program to count the number of rectangular
// islands where every island is separated by
// a line
using System;
class GFG {
// This function takes a matrix of 'X' and 'O'
// and returns the number of rectangular
// islands of 'X' where no two islands are
// row-wise or column-wise adjacent, the
// islands may be diagonally adjacent
static int countIslands(int [,]mat, int m, int n)
{
// Initialize result
int count = 0;
// Traverse the input matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
// If current cell is 'X', then check
// whether this is top-leftmost of a
// rectangle. If yes, then increment
// count
if (mat[i,j] == 'X')
{
if ((i == 0 || mat[i-1,j] == 'O') &&
(j == 0 || mat[i,j-1] == 'O'))
count++;
}
}
}
return count;
}
// Driver program
public static void Main ()
{
// Size of given matrix is m X n
int m = 6;
int n = 3;
int [,]mat = { {'O', 'O', 'O'},
{'X', 'X', 'O'},
{'X', 'X', 'O'},
{'O', 'O', 'X'},
{'O', 'O', 'X'},
{'X', 'X', 'O'}
};
Console.WriteLine("Number of rectangular "
+ "islands is: " + countIslands(mat, m, n));
}
}
// This code is contributed by Sam007.
PHP
Javascript
输出:
Number of rectangular islands is 3