给定一个维度为NxM的矩阵mat[][] ,任务是检查给定的矩阵是否平衡。如果是平衡矩阵则打印“ Balanced” ,否则打印“ Unbalanced” 。
A matrix is balanced if all cells in the matrix are balanced and a cell of the matrix is balanced if the number of cells in that matrix that are adjacent to that cell is strictly greater than the value written in this cell.
Adjacent cell means cells in the top, down, left, and right cell of each cell if it exists.
例子:
Input: N = 3, M = 3
mat[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output: Unbalanced
Explanation: Each cell of the given grid is not stable, so the overall grid is unbalanced.
Input: N = 3, M = 3
mat[][] = {{1, 2, 1},
{2, 3, 2},
{1, 2, 1}}
Output: Balanced
Explanation: Each cell of the given grid is stable, so the overall grid is Balanced.
方法:
- 遍历给定的矩阵mat[][] 。
- 对于矩阵的每个单元格检查是否所有相邻单元格,即mat[i+1][j], mat[i][j+1], mat[i-1][j], mat[i][j] -1]严格小于当前单元格。
- 对于矩阵的角单元,只有两个相邻单元,即mat[i+1][j] 和 mat[i][j+1]检查所有这些相邻单元是否严格小于角单元。
- 对于矩阵的边界单元格,有 3 个相邻单元格,即mat[i-1][j]、mat[i+1][j] 和 mat[i][j+1]检查是否所有这些相邻单元格严格小于边界单元格。
- 如果矩阵的所有单元格的所有上述条件都为真,则打印“ Balanced”,否则打印“ Unbalanced” 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Define size of matrix
#define N 4
#define M 4
// Function to check given matrix
// balanced or unbalanced
string balancedMatrix(int mat[][M])
{
// Flag for check matrix is balanced
// or unbalanced
bool is_balanced = true;
// Iterate row until condition is true
for (int i = 0; i < N && is_balanced; i++) {
// Iterate cols until condition is true
for (int j = 0; j < M && is_balanced; j++) {
// Check for corner edge elements
if ((i == 0 || i == N - 1)
&& (j == 0 || j == M - 1)) {
if (mat[i][j] >= 2)
is_balanced = false;
}
// Check for border elements
else if (i == 0 || i == N - 1
|| j == 0 || j == M - 1) {
if (mat[i][j] >= 3)
is_balanced = false;
}
else {
// Check for the middle ones
if (mat[i][j] >= 4)
is_balanced = false;
}
}
}
// Return balanced or not
if (is_balanced)
return "Balanced";
else
return "Unbalanced";
}
// Driver Code
int main()
{
// Given Matrix mat[][]
int mat[N][M] = { { 1, 2, 3, 4 },
{ 3, 5, 2, 6 },
{ 5, 3, 6, 1 },
{ 9, 5, 6, 0 } };
// Function Call
cout << balancedMatrix(mat);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Define size of matrix
static final int N = 4;
static final int M = 4;
// Function to check given matrix
// balanced or unbalanced
static String balancedMatrix(int mat[][])
{
// Flag for check matrix is balanced
// or unbalanced
boolean is_balanced = true;
// Iterate row until condition is true
for (int i = 0; i < N && is_balanced; i++)
{
// Iterate cols until condition is true
for (int j = 0; j < M && is_balanced; j++)
{
// Check for corner edge elements
if ((i == 0 || i == N - 1) &&
(j == 0 || j == M - 1))
{
if (mat[i][j] >= 2)
is_balanced = false;
}
// Check for border elements
else if (i == 0 || i == N - 1 ||
j == 0 || j == M - 1)
{
if (mat[i][j] >= 3)
is_balanced = false;
}
else
{
// Check for the middle ones
if (mat[i][j] >= 4)
is_balanced = false;
}
}
}
// Return balanced or not
if (is_balanced)
return "Balanced";
else
return "Unbalanced";
}
// Driver Code
public static void main(String[] args)
{
// Given Matrix mat[][]
int mat[][] = {{1, 2, 3, 4},
{3, 5, 2, 6},
{5, 3, 6, 1},
{9, 5, 6, 0}};
// Function Call
System.out.print(balancedMatrix(mat));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Define the size of the matrix
N = 4
M = 4
# Function to check given matrix
# balanced or unbalanced
def balancedMatrix(mat):
# Flag for check matrix is balanced
# or unbalanced
is_balanced = True
# Iterate row until condition is true
i = 0
while i < N and is_balanced:
# Iterate cols until condition is true
j = 0
while j < N and is_balanced:
# Check for corner edge elements
if ((i == 0 or i == N - 1) and
(j == 0 or j == M - 1)):
if mat[i][j] >= 2:
isbalanced = False
# Check for border elements
elif (i == 0 or i == N - 1 or
j == 0 or j == M - 1):
if mat[i][j] >= 3:
is_balanced = False
# Check for the middle ones
else:
if mat[i][j] >= 4:
is_balanced = False
j += 1
i += 1
# Return balanced or not
if is_balanced:
return "Balanced"
else:
return "Unbalanced"
# Driver code
# Given matrix mat[][]
mat = [ [ 1, 2, 3, 4 ],
[ 3, 5, 2, 6 ],
[ 5, 3, 6, 1 ],
[ 9, 5, 6, 0 ] ]
# Function call
print(balancedMatrix(mat))
# This code is contributed by Stuti Pathak
C#
// C# program for the above approach
using System;
class GFG{
// Define size of matrix
static readonly int N = 4;
static readonly int M = 4;
// Function to check given matrix
// balanced or unbalanced
static String balancedMatrix(int [, ]mat)
{
// Flag for check matrix is balanced
// or unbalanced
bool is_balanced = true;
// Iterate row until condition is true
for (int i = 0; i < N && is_balanced; i++)
{
// Iterate cols until condition is true
for (int j = 0; j < M && is_balanced; j++)
{
// Check for corner edge elements
if ((i == 0 || i == N - 1) &&
(j == 0 || j == M - 1))
{
if (mat[i, j] >= 2)
is_balanced = false;
}
// Check for border elements
else if (i == 0 || i == N - 1 ||
j == 0 || j == M - 1)
{
if (mat[i, j] >= 3)
is_balanced = false;
}
else
{
// Check for the middle ones
if (mat[i, j] >= 4)
is_balanced = false;
}
}
}
// Return balanced or not
if (is_balanced)
return "Balanced";
else
return "Unbalanced";
}
// Driver Code
public static void Main(String[] args)
{
// Given Matrix [,]mat
int [, ]mat = {{1, 2, 3, 4},
{3, 5, 2, 6},
{5, 3, 6, 1},
{9, 5, 6, 0}};
// Function Call
Console.Write(balancedMatrix(mat));
}
}
// This code is contributed by 29AjayKumar
Javascript
Unbalanced
时间复杂度: O(N*M)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。