📌  相关文章
📜  通过连接单元以相等的值遍历整个矩阵的最小移动次数

📅  最后修改于: 2021-05-04 17:02:35             🧑  作者: Mango

给定尺寸为N * M的矩阵A [] [] ,任务是通过在每一步以相等的值遍历连接的像元,找到从(0,0)开始遍历整个矩阵所需的最小移动数。

例子:

方法:
请按照以下步骤解决问题:

  • 创建另一个矩阵,用不同的值填充每个单元格。
  • (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 )