📌  相关文章
📜  寻找将所有元素带入矩阵的一个单元格的最小移动

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

给定一个矩阵mat [] [] ,一对索引XY ,任务是找到将矩阵的所有非零元素带到给定单元格(X,Y)的移动次数

例子:

方法:想法是遍历矩阵,并为矩阵的每个非零元素找到当前单元格(例如(A,B) )到矩阵目标单元格(X,Y)的距离,如下所示:

moves = abs(x - i) + abs(y - j)


通过上述公式对所有非零元素的所有距离之和是必需的结果。
下面是上述方法的实现:

C++
// C++ implementation to find the
// minimum number of moves to
// bring all non-zero element
// in one cell of the matrix
  
#include 
using namespace std;
  
const int M = 4;
const int N = 5;
  
// Function to find the minimum
// number of moves to bring all
// elements in one cell of matrix
void no_of_moves(int Matrix[M][N],
                int x, int y)
{
  
    // Moves variable to store
    // the sum of number of moves
    int moves = 0;
  
    // Loop to count the number
    // of the moves
    for (int i = 0; i < M; i++) {
  
        for (int j = 0; j < N; j++) {
  
            // Condition to check that
            // the current cell is a
            // non-zero element
            if (Matrix[i][j] != 0) {
                moves += abs(x - i);
  
                moves += abs(y - j);
            }
        }
    }
  
    cout << moves << "\n";
}
  
// Driver Code
int main()
{
    // Coordinates of given cell
    int x = 3;
    int y = 2;
  
    // Given Matrix
    int Matrix[M][N] = { { 1, 0, 1, 1, 0 },
                        { 0, 1, 1, 0, 1 },
                        { 0, 0, 1, 1, 0 },
                        { 1, 1, 1, 0, 0 } };
  
    // Element to be moved
    int num = 1;
  
    // Function call
    no_of_moves(Matrix, x, y);
    return 0;
}


Java
// Java implementation to find the
// minimum number of moves to
// bring all non-zero element
// in one cell of the matrix
class GFG{
      
static int M = 4;
static int N = 5;
  
// Function to find the minimum
// number of moves to bring all
// elements in one cell of matrix
public static void no_of_moves(int[][] Matrix,
                               int x, int y)
{
      
    // Moves variable to store
    // the sum of number of moves
    int moves = 0;
  
    // Loop to count the number
    // of the moves
    for(int i = 0; i < M; i++)
    {
        for(int j = 0; j < N; j++) 
        {
              
            // Condition to check that
            // the current cell is a
            // non-zero element
            if (Matrix[i][j] != 0) 
            {
                moves += Math.abs(x - i);
                moves += Math.abs(y - j);
            }
        }
    }
    System.out.println(moves);
}
  
// Driver code
public static void main(String[] args)
{
      
    // Coordinates of given cell
    int x = 3;
    int y = 2;
  
    // Given Matrix
    int[][] Matrix = { { 1, 0, 1, 1, 0 },
                       { 0, 1, 1, 0, 1 },
                       { 0, 0, 1, 1, 0 },
                       { 1, 1, 1, 0, 0 } };
  
    // Element to be moved
    int num = 1;
  
    // Function call
    no_of_moves(Matrix, x, y);
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation to find the
# minimum number of moves to
# bring all non-zero element
# in one cell of the matrix
M = 4
N = 5
  
# Function to find the minimum
# number of moves to bring all
# elements in one cell of matrix
def no_of_moves(Matrix, x, y):
  
    # Moves variable to store
    # the sum of number of moves
    moves = 0
  
    # Loop to count the number
    # of the moves
    for i in range(M):
        for j in range(N):
  
            # Condition to check that
            # the current cell is a
            # non-zero element
            if (Matrix[i][j] != 0):
                moves += abs(x - i)
                moves += abs(y - j)
  
    print(moves)
  
# Driver Code
if __name__ == '__main__':
    
    # Coordinates of given cell
    x = 3
    y = 2
  
    # Given Matrix
    Matrix = [ [ 1, 0, 1, 1, 0 ],
               [ 0, 1, 1, 0, 1 ],
               [ 0, 0, 1, 1, 0 ],
               [ 1, 1, 1, 0, 0 ] ]
  
    # Element to be moved
    num = 1
  
    # Function call
    no_of_moves(Matrix, x, y)
  
# This code is contributed by mohit kumar 29


C#
// C# implementation to find the 
// minimum number of moves to 
// bring all non-zero element 
// in one cell of the matrix 
using System;
  
class GFG{ 
      
static int M = 4; 
static int N = 5; 
  
// Function to find the minimum 
// number of moves to bring all 
// elements in one cell of matrix 
public static void no_of_moves(int[,] Matrix, 
                               int x, int y) 
{ 
      
    // Moves variable to store 
    // the sum of number of moves 
    int moves = 0; 
  
    // Loop to count the number 
    // of the moves 
    for(int i = 0; i < M; i++) 
    { 
        for(int j = 0; j < N; j++) 
        { 
              
            // Condition to check that 
            // the current cell is a 
            // non-zero element 
            if (Matrix[i, j] != 0) 
            { 
                moves += Math.Abs(x - i); 
                moves += Math.Abs(y - j); 
            } 
        } 
    } 
    Console.WriteLine(moves); 
} 
  
// Driver code 
public static void Main(String[] args) 
{ 
      
    // Coordinates of given cell 
    int x = 3; 
    int y = 2; 
  
    // Given matrix 
    int[,] Matrix = { { 1, 0, 1, 1, 0 }, 
                      { 0, 1, 1, 0, 1 }, 
                      { 0, 0, 1, 1, 0 }, 
                      { 1, 1, 1, 0, 0 } }; 
  
    // Function call 
    no_of_moves(Matrix, x, y); 
} 
} 
  
// This code is contributed by 29AjayKumar


输出:
27

时间复杂度: O(N 2 )
辅助空间: O(1)