给定矩阵mat[][] ,索引对X和Y ,任务是找到将矩阵的所有非零元素带到(X, Y)处的给定单元格的移动次数。
A move consists of moving an element at any cell to its four directional adjacent cells i.e., left, right, top, bottom.
例子:
Input: mat[][] = {{1, 0}, {1, 0}}, X = 1, Y = 1
Output: 3
Explanation:
Moves required =>
For Index (0, 0) => 2
For Index (1, 0) => 1
Total moves required = 3
Input: mat[][] = {{1, 0, 1, 0}, {1, 1, 0, 1}, {0, 0, 1, 0}}, X = 1, Y = 3
Output: 13
方法:想法是遍历矩阵,并为矩阵的每个非零元素找到当前单元格(例如(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
Javascript
输出:
27
时间复杂度: O(N 2 )
辅助空间: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live