给定尺寸为N * M的矩阵A [] [] ,任务是通过在每一步以相等的值遍历连接的像元,找到从(0,0)开始遍历整个矩阵所需的最小移动数。
From a cell (i, j), cells (i + 1, j), (i – 1, j), (i, j – 1) and (i, j + 1) can be connected.
例子:
Input: arr[][] = {{1, 1, 2, 2, 1}, {1, 1, 2, 2, 1}, {1, 1, 1, 3, 2}}
Output: 5
Explanation:
Minimum 5 moves are required to traverse the matrix.
First move: Starting from [0, 0], traverse cells [0, 1], [1, 1], [1, 0], [2, 0], [2, 1], [2, 2] as all these cells have same value 1.
Second move: Traverse cells [0, 2], [0, 3], [1, 3], [1, 2] as all these cells have value 2.
Third move: Traverse cells [0, 4], [1, 4] containing 1.
Fourth move: Traverse [2, 3] containing 3.
Fifth move: Traverse [2, 4] containing 4.
Input: arr[][] = {{2, 1, 3}, {1, 1, 2}}
Output: 4
Explanation:
Minimum 4 moves are required to cover this 2-D array
First move: Traverse only [0, 0] as no other connected cell has value 2.
Second move: Traverse cells [0, 1], [1, 1], [1, 0] as these cells contain value 1.
Third move: Traverse cell [0, 2] containing 3.
Fourth move: Traverse cell [1, 2] containing 2.
方法:
请按照以下步骤解决问题:
- 创建另一个矩阵,用不同的值填充每个单元格。
- 从(0,0)开始遍历矩阵A [] [] 。对于每个像元(i,j) ,检查其相邻像元是否具有与A [i] [j]相同的值。
- 如果任何相邻的单元格具有相同的值,则用B [i] [j]的值替换B [] []中的该单元格。
- 完成A [] []的遍历后,对B [] []矩阵中剩余的不同元素进行计数,得出所需的答案。
下面是上述方法的实现:
C++
// C++ program to find the
// minimum number of moves
// to traverse a given matrix
#include
using namespace std;
// Function to find the minimum
// number of moves to traverse
// the given matrix
int solve(int A[][10], int N, int M)
{
int B[N][M];
int c = 1;
set s;
// Constructing another matrix
// consisting of distinct values
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
B[i][j] = c++;
}
// Updating the array B by checking
// the values of A that if there are
// same values connected
// through an edge or not
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
// Check for boundary
// condition of the matrix
if (i != 0) {
// If adjacent cells have
// same value
if (A[i - 1][j] == A[i][j])
B[i - 1][j] = B[i][j];
}
// Check for boundary
// condition of the matrix
if (i != N - 1) {
// If adjacent cells have
// same value
if (A[i + 1][j] == A[i][j])
B[i + 1][j] = B[i][j];
}
// Check for boundary
// condition of the matrix
if (j != 0) {
// If adjacent cells have
// same value
if (A[i][j - 1] == A[i][j])
B[i][j - 1] = B[i][j];
}
// Check for boundary
// condition of the matrix
if (j != M - 1) {
// If adjacent cells have
// same value
if (A[i][j + 1] == A[i][j])
B[i][j + 1] = B[i][j];
}
}
}
// Store all distinct elements
// in a set
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++)
s.insert(B[i][j]);
}
// Return answer
return s.size();
}
// Driver Code
int main()
{
int N = 2, M = 3;
int A[][10] = { { 2, 1, 3 },
{ 1, 1, 2 } };
// Function Call
cout << solve(A, N, M);
}
Java
// Java program to find the
// minimum number of moves
// to traverse a given matrix
import java.util.*;
class GFG{
// Function to find the minimum
// number of moves to traverse
// the given matrix
static int solve(int A[][], int N,
int M)
{
int [][]B = new int[N][M];
int c = 1;
HashSet s = new HashSet();
// Constructing another matrix
// consisting of distinct values
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
B[i][j] = c++;
}
// Updating the array B by checking
// the values of A that if there are
// same values connected
// through an edge or not
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
// Check for boundary
// condition of the matrix
if (i != 0)
{
// If adjacent cells have
// same value
if (A[i - 1][j] == A[i][j])
B[i - 1][j] = B[i][j];
}
// Check for boundary
// condition of the matrix
if (i != N - 1)
{
// If adjacent cells have
// same value
if (A[i + 1][j] == A[i][j])
B[i + 1][j] = B[i][j];
}
// Check for boundary
// condition of the matrix
if (j != 0)
{
// If adjacent cells have
// same value
if (A[i][j - 1] == A[i][j])
B[i][j - 1] = B[i][j];
}
// Check for boundary
// condition of the matrix
if (j != M - 1)
{
// If adjacent cells have
// same value
if (A[i][j + 1] == A[i][j])
B[i][j + 1] = B[i][j];
}
}
}
// Store all distinct elements
// in a set
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
s.add(B[i][j]);
}
// Return answer
return s.size();
}
// Driver Code
public static void main(String[] args)
{
int N = 2, M = 3;
int A[][] = { { 2, 1, 3 },
{ 1, 1, 2 } };
// Function Call
System.out.print(solve(A, N, M));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find the
# minimum number of moves
# to traverse a given matrix
# Function to find the minimum
# number of moves to traverse
# the given matrix
def solve(A, N, M):
B = []
c = 1
s = set()
# Constructing another matrix
# consisting of distinct values
for i in range(N):
new = []
for j in range(M):
new.append(c)
c = c + 1
B.append(new)
# Updating the array B by checking
# the values of A that if there are
# same values connected
# through an edge or not
for i in range(N):
for j in range(M):
# Check for boundary
# condition of the matrix
if i != 0:
# If adjacent cells have
# same value
if A[i - 1][j] == A[i][j]:
B[i - 1][j] = B[i][j]
# Check for boundary
# condition of the matrix
if (i != N - 1):
# If adjacent cells have
# same value
if A[i + 1][j] == A[i][j]:
B[i + 1][j] = B[i][j]
# Check for boundary
# condition of the matrix
if (j != 0):
# If adjacent cells have
# same value
if A[i][j - 1] == A[i][j]:
B[i][j - 1] = B[i][j]
# Check for boundary
# condition of the matrix
if (j != M - 1):
# If adjacent cells have
# same value
if (A[i][j + 1] == A[i][j]):
B[i][j + 1] = B[i][j]
# Store all distinct elements
# in a set
for i in range(N):
for j in range(M):
s.add(B[i][j])
# Return answer
return len(s)
# Driver code
N = 2
M = 3
A = [ [ 2, 1, 3 ], [ 1, 1, 2 ] ]
# Function call
print(solve(A, N, M))
# This code is contributed by divyeshrabadiya07
C#
// C# program to find the
// minimum number of moves
// to traverse a given matrix
using System;
using System.Collections.Generic;
class GFG{
// Function to find the minimum
// number of moves to traverse
// the given matrix
static int solve(int [,]A, int N,
int M)
{
int [,]B = new int[N, M];
int c = 1;
HashSet s = new HashSet();
// Constructing another matrix
// consisting of distinct values
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
B[i, j] = c++;
}
// Updating the array B by checking
// the values of A that if there are
// same values connected
// through an edge or not
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
// Check for boundary
// condition of the matrix
if (i != 0)
{
// If adjacent cells have
// same value
if (A[i - 1, j] == A[i, j])
B[i - 1, j] = B[i, j];
}
// Check for boundary
// condition of the matrix
if (i != N - 1)
{
// If adjacent cells have
// same value
if (A[i + 1, j] == A[i, j])
B[i + 1, j] = B[i, j];
}
// Check for boundary
// condition of the matrix
if (j != 0)
{
// If adjacent cells have
// same value
if (A[i, j - 1] == A[i, j])
B[i, j - 1] = B[i, j];
}
// Check for boundary
// condition of the matrix
if (j != M - 1)
{
// If adjacent cells have
// same value
if (A[i, j + 1] == A[i, j])
B[i, j + 1] = B[i, j];
}
}
}
// Store all distinct elements
// in a set
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
s.Add(B[i, j]);
}
// Return answer
return s.Count;
}
// Driver Code
public static void Main(String[] args)
{
int N = 2, M = 3;
int [,]A = { { 2, 1, 3 },
{ 1, 1, 2 } };
// Function Call
Console.Write(solve(A, N, M));
}
}
// This code is contributed by 29AjayKumar
4
时间复杂度: O(N 2 )
辅助空间: O(N 2 )