给定大小为NxM的二进制矩阵mat [] []以及两个整数X和Y ,任务是找到将给定矩阵中的所有1移动到单元格(X,Y)所需的最小步数。涉及向左,向右,向上或向下移动单元格。
例子:
Input: mat[][] = { {1, 0, 1}, {0, 1, 0}, {1, 0, 1} }, X = 1, Y = 1
Output: 8
Explanation:
Cells (0, 0), (0, 2), (1, 1), (2, 0) and (2, 2) consists of 1.
Moving 1 at index (0, 0) to (1, 1) requires 2 steps
(0, 0) -> (0, 1) ->(1, 0)
Moving 1 at index (0, 2) to (1, 1) requires 2 steps
Moving 1 at index (2, 0) to (1, 1) requires 2 steps
Moving 1 at index (2, 2) to (1, 1) requires 2 steps
Therefore, 8 steps are required.
Input: mat[][] = { {1, 0, 0, 0}, {0, 1, 0, 1}, {1, 0, 1, 1} }, X = 0, Y = 2
Output: 15
方法:
这个想法是遍历给定的矩阵并找到由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
15
时间复杂度: O(N * M)
辅助空间: O(1)