给定一个大小为N * N 的方阵mat[][] ,任务是在增加矩阵元素后打印矩阵,使得矩阵的两个相邻元素不相等。
例子:
Input: mat[][] = { { 1, 2, 2 }, { 3, 2, 2 }, { 2, 2, 2 } }
Output: { { 2, 3, 2 }, { 3, 2, 3 }, { 2, 3, 2 } }
Explanation:
Incrementing the value of { mat[0][0], mat[0][1], mat[1][2], mat[2][1] } by 1 modifies the matrix to { { 2, 3, 2 }, { 3, 2, 3 }, { 2, 3, 2 } }, where no two adjacent elements are equal.
Therefore, the required output is { { 2, 3, 2 }, { 3, 2, 3 }, { 2, 3, 2 } }.
Input: mat[][] = { { 1, 2 }, { 1, 2 } }
Output: { { 2, 3 }, {1, 2 } }
处理方法:按照以下步骤解决问题:
- 使用变量(i, j)遍历矩阵并检查以下条件。
- 如果i % 2 == 0 和 j % 2 == 0 和 mat[i][j] % 2 == 1 ,则将mat[i][j]的值增加1 。
- 如果i % 2 == 0 和 j % 2 == 1 和 mat[i][j] % 2 == 0 ,则将mat[i][j]的值增加1 。
- 如果i % 2 == 1 和 j % 2 == 0 和 mat[i][j] % 2 == 0 ,则将mat[i][j]的值增加1 。
- 如果i % 2 == 1 和 j % 2 == 1 和 mat[i][j] % 2 == 1 ,则将mat[i][j]的值增加1 。
- 最后,打印矩阵mat[][] 。
下面是上述方法的实现:
C++14
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to print the matrix
void printMatrix(vector >& mat, int N)
{
// Print the leftmost parenthesis
// of the matrix
cout << "{ ";
// Traverse the matrix
for (int i = 0; i < N; i++) {
cout << "{ ";
// Traverse each column
// of the matrix
for (int j = 0; j < N; j++) {
// If current column is last
// column of the matrix
if (j == N - 1) {
// Print current element
// of the matrix
cout << mat[i][j];
}
// If current column is not
// last column of the matrix
else {
cout << mat[i][j] << ", ";
}
}
cout << " } ";
}
// Print the leftmost parenthesis
// of the matrix
cout << "}";
}
// Function to find the matrix with no two
// adjacent elements equal by increment operation
void MatNoAdjacentElemEq(vector >& mat,
int N)
{
// Traverse the matrix
for (int i = 0; i < N; i++) {
// Traverse coloum of the matrix
for (int j = 0; j < N; j++) {
// If i is an even number
if (i % 2 == 0) {
// If j is an even number and current
// matrix element is an odd number
if (j % 2 == 0 && mat[i][j] % 2 != 0) {
// Increment the value of
// matrix element
mat[i][j]++;
}
// If j is an odd number and current
// matrix element is an even number
else if (j % 2 != 0 && mat[i][j] % 2 == 0) {
// Increment the value of
// matrix element
mat[i][j]++;
}
}
// If i is an odd number
else {
// If j is an even number and current
// matrix element is an even number
if (j % 2 == 0 && mat[i][j] % 2 == 0) {
// Increment the value of
// matrix element
mat[i][j]++;
}
// If j is an odd number and current
// matrix element is an odd number
else if (j % 2 != 0 && mat[i][j] % 2 != 0) {
// Increment the value of
// matrix element
mat[i][j]++;
}
}
}
}
// Print the matrix
printMatrix(mat, N);
}
// Driver Code
int main()
{
vector > mat = { { 1, 2, 2 },
{ 3, 2, 2 },
{ 2, 2, 2 } };
int N = mat.size();
MatNoAdjacentElemEq(mat, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to print the matrix
static void printMatrix(int[][] mat, int N)
{
// Print the leftmost parenthesis
// of the matrix
System.out.print("{ ");
// Traverse the matrix
for(int i = 0; i < N; i++)
{
System.out.print("{ ");
// Traverse each column
// of the matrix
for(int j = 0; j < N; j++)
{
// If current column is last
// column of the matrix
if (j == N - 1)
{
// Print current element
// of the matrix
System.out.print(mat[i][j]);
}
// If current column is not
// last column of the matrix
else
{
System.out.print(mat[i][j] + ", ");
}
}
System.out.print(" } ");
}
// Print the leftmost parenthesis
// of the matrix
System.out.print("}");
}
// Function to find the matrix with no two
// adjacent elements equal by increment operation
static void MatNoAdjacentElemEq(int[][] mat,
int N)
{
// Traverse the matrix
for(int i = 0; i < N; i++)
{
// Traverse coloum of the matrix
for(int j = 0; j < N; j++)
{
// If i is an even number
if (i % 2 == 0)
{
// If j is an even number and current
// matrix element is an odd number
if (j % 2 == 0 &&
mat[i][j] % 2 != 0)
{
// Increment the value of
// matrix element
mat[i][j]++;
}
// If j is an odd number and current
// matrix element is an even number
else if (j % 2 != 0 &&
mat[i][j] % 2 == 0)
{
// Increment the value of
// matrix element
mat[i][j]++;
}
}
// If i is an odd number
else
{
// If j is an even number and current
// matrix element is an even number
if (j % 2 == 0 &&
mat[i][j] % 2 == 0)
{
// Increment the value of
// matrix element
mat[i][j]++;
}
// If j is an odd number and current
// matrix element is an odd number
else if (j % 2 != 0 &&
mat[i][j] % 2 != 0)
{
// Increment the value of
// matrix element
mat[i][j]++;
}
}
}
}
// Print the matrix
printMatrix(mat, N);
}
// Driver Code
public static void main(String[] args)
{
int[][] mat = { { 1, 2, 2 },
{ 3, 2, 2 },
{ 2, 2, 2 } };
int N = mat.length;
MatNoAdjacentElemEq(mat, N);
}
}
// This code is contributed by aashish1995
Python3
# Python3 program to implement
# the above approach
# Function to print the matrix
def printMatrix(mat, N):
# Print the leftmost parenthesis
# of the matrix
print("{ ",end = "")
# Traverse the matrix
for i in range(N):
print("{ ",end = "")
# Traverse each column
# of the matrix
for j in range(N):
# If current column is last
# column of the matrix
if (j == N - 1):
# Prcurrent element
# of the matrix
print(mat[i][j], end = "")
# If current column is not
# last column of the matrix
else:
print(mat[i][j], end = ", ")
print(" } ",end = "")
# Print the leftmost parenthesis
# of the matrix
print("}",end="")
# Function to find the matrix with no two
# adjacent elements equal by increment operation
def MatNoAdjacentElemEq(mat,N):
# Traverse the matrix
for i in range(N):
# Traverse coloum of the matrix
for j in range(N):
# If i is an even number
if (i % 2 == 0):
# If j is an even number and current
# matrix element is an odd number
if (j % 2 == 0 and mat[i][j] % 2 != 0):
# Increment the value of
# matrix element
mat[i][j] += 1
# If j is an odd number and current
# matrix element is an even number
elif (j % 2 != 0 and mat[i][j] % 2 == 0):
# Increment the value of
# matrix element
mat[i][j] += 1
# If i is an odd number
else:
# If j is an even number and current
# matrix element is an even number
if (j % 2 == 0 and mat[i][j] % 2 == 0):
#Increment the value of
#matrix element
mat[i][j] += 1
# If j is an odd number and current
# matrix element is an odd number
elif (j % 2 != 0 and mat[i][j] % 2 != 0):
# Increment the value of
# matrix element
mat[i][j] += 1
# Print the matrix
printMatrix(mat, N)
# Driver Code
if __name__ == '__main__':
mat =[[1, 2, 2],
[ 3, 2, 2],
[ 2, 2, 2]]
N = len(mat)
MatNoAdjacentElemEq(mat, N)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG{
// Function to print the matrix
static void printMatrix(int[,] mat, int N)
{
// Print the leftmost parenthesis
// of the matrix
Console.Write("{ ");
// Traverse the matrix
for(int i = 0; i < N; i++)
{
Console.Write("{ ");
// Traverse each column
// of the matrix
for(int j = 0; j < N; j++)
{
// If current column is last
// column of the matrix
if (j == N - 1)
{
// Print current element
// of the matrix
Console.Write(mat[i, j]);
}
// If current column is not
// last column of the matrix
else
{
Console.Write(mat[i, j] + ", ");
}
}
Console.Write(" } ");
}
// Print the leftmost parenthesis
// of the matrix
Console.Write("}");
}
// Function to find the matrix with no two
// adjacent elements equal by increment operation
static void MatNoAdjacentElemEq(int[,] mat,
int N)
{
// Traverse the matrix
for(int i = 0; i < N; i++)
{
// Traverse coloum of the matrix
for(int j = 0; j < N; j++)
{
// If i is an even number
if (i % 2 == 0)
{
// If j is an even number and current
// matrix element is an odd number
if (j % 2 == 0 &&
mat[i, j] % 2 != 0)
{
// Increment the value of
// matrix element
mat[i, j]++;
}
// If j is an odd number and current
// matrix element is an even number
else if (j % 2 != 0 &&
mat[i, j] % 2 == 0)
{
// Increment the value of
// matrix element
mat[i, j]++;
}
}
// If i is an odd number
else
{
// If j is an even number and current
// matrix element is an even number
if (j % 2 == 0 &&
mat[i, j] % 2 == 0)
{
// Increment the value of
// matrix element
mat[i, j]++;
}
// If j is an odd number and current
// matrix element is an odd number
else if (j % 2 != 0 &&
mat[i, j] % 2 != 0)
{
// Increment the value of
// matrix element
mat[i, j]++;
}
}
}
}
// Print the matrix
printMatrix(mat, N);
}
// Driver Code
public static void Main(String[] args)
{
int[,] mat = { { 1, 2, 2 },
{ 3, 2, 2 },
{ 2, 2, 2 } };
int N = mat.GetLength(0);
MatNoAdjacentElemEq(mat, N);
}
}
// This code is contributed by aashish1995
输出:
{ { 2, 3, 2 } { 3, 2, 3 } { 2, 3, 2 } }
时间复杂度: O(N 2 )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live