📌  相关文章
📜  最小化将矩阵中的所有 1 移动到给定索引所需的步骤

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

给定一个大小为NxM的二进制矩阵mat[][]和两个整数XY ,任务是找到将给定矩阵中的所有1移动到单元格(X, Y)所需的最小步数,其中,一步涉及向左、向右、向上向下移动单元格。
例子:

方法:
这个想法是遍历给定的矩阵并找到由1组成的单元格。对于由1组成的任何单元格(i, j) 基于给定方向到达(X, Y)所需的最小步长由下式给出:

Minimum steps = abs(X - i) + abs(Y - j)

使用上述公式计算给定矩阵mat[][]中包含1 的每个单元格所需的总步数。
下面是上述方法的实现:

C++
// C++ program to calculate
// the minimum steps
// required to reach
// a given cell from all
// cells consisting of 1's
#include 
using namespace std;
 
// Function to calculate and
// return the minimum
// number of steps required
// to move all 1s to (X, Y)
int findAns(vector > mat,
            int x, int y,
            int n, int m)
{
    int ans = 0;
 
    // Iterate the given matrix
    for (int i = 0; i < n; i++) {
 
        for (int j = 0; j < m; j++) {
 
            // Update the answer with
            // minimum moves required
            // for the given element
            // to reach the given index
            if (mat[i][j] == 1) {
 
                ans += abs(x - i)
                    + abs(y - j);
            }
        }
    }
 
    // Return the number
    // of steps
    return ans;
}
 
// Driver Code
int main()
{
    // Given matrix
    vector > mat
        = { { 1, 0, 0, 0 },
            { 0, 1, 0, 1 },
            { 1, 0, 1, 1 } };
 
    // Given position
    int x = 0, y = 2;
 
    // Function Call
    cout << findAns(mat, x, y,
                    mat.size(),
                    mat[0].size())
        << endl;
    return 0;
}


Java
// Java program to calculate the
// minimum steps required to reach
// a given cell from all cells
// consisting of 1's
import java.util.*;
 
class GFG{
 
// Function to calculate and
// return the minimum number
// of steps required to move
// all 1s to (X, Y)
static int findAns(int [][]mat,
                   int x, int y,
                   int n, int m)
{
    int ans = 0;
 
    // Iterate the given matrix
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
             
            // Update the answer with
            // minimum moves required
            // for the given element
            // to reach the given index
            if (mat[i][j] == 1)
            {
                ans += Math.abs(x - i) +
                       Math.abs(y - j);
            }
        }
    }
 
    // Return the number
    // of steps
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Given matrix
    int [][]mat = { { 1, 0, 0, 0 },
                    { 0, 1, 0, 1 },
                    { 1, 0, 1, 1 } };
 
    // Given position
    int x = 0, y = 2;
 
    // Function Call
    System.out.print(findAns(mat, x, y,
                             mat.length,
                             mat[0].length) + "\n");
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program to calculate
# the minimum steps required to
# reach a given cell from all
# cells consisting of 1's
 
# Function to calculate and
# return the minimum number
# of steps required to move
# all 1s to (X, Y)
def findAns(mat, x, y, n, m):
     
    ans = 0
     
    # Iterate the given matrix
    for i in range(n):
        for j in range(m):
             
            # Update the answer with
            # minimum moves required
            # for the given element
            # to reach the given index
            if (mat[i][j] == 1):
                ans += (abs(x - i) +
                        abs(y - j))
     
    # Return the number
    # of steps
    return ans
     
# Driver Code
 
# Given matrix
mat = [ [ 1, 0, 0, 0 ],
        [ 0, 1, 0, 1 ],
        [ 1, 0, 1, 1 ] ]
 
# Given position
x = 0
y = 2
 
# Function call
print(findAns(mat, x, y, len(mat),
                        len(mat[0])))
 
# This code is contributed by shubhamsingh10


C#
// C# program to calculate the
// minimum steps required to reach
// a given cell from all cells
// consisting of 1's
using System;
 
class GFG{
 
// Function to calculate and
// return the minimum number
// of steps required to move
// all 1s to (X, Y)
static int findAns(int [,]mat,
                   int x, int y,
                   int n, int m)
{
    int ans = 0;
 
    // Iterate the given matrix
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
             
            // Update the answer with
            // minimum moves required
            // for the given element
            // to reach the given index
            if (mat[i, j] == 1)
            {
                ans += Math.Abs(x - i) +
                       Math.Abs(y - j);
            }
        }
    }
 
    // Return the number
    // of steps
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Given matrix
    int [,]mat = { { 1, 0, 0, 0 },
                   { 0, 1, 0, 1 },
                   { 1, 0, 1, 1 } };
 
    // Given position
    int x = 0, y = 2;
 
    // Function call
    Console.Write(findAns(mat, x, y,
                          mat.GetLength(0),
                          mat.GetLength(1)) + "\n");
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
15

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