a 0 离二维矩阵中心的最远距离
给定一个奇数阶矩阵mat ,任务是找到 a 0离矩阵中心最远的距离。矩阵的位置(i1, j1) 和 (i2, j2)处的两个元素之间的距离计算为 |i1- i2| + |j1-j2|。如果发生在基质中无0然后打印0作为结果。
例子:
Input: mat[][] = {{2, 3, 0}, {0, 2, 0}, {0, 1, 1}}
Output: 2
Input: mat[][] = {{2, 3, 4, {0, 2, 0}, {6, 1, 1}}
Output: 1
方法:任何奇数阶矩阵的中心位于索引i = j = floor(n/2) 处。现在为了找到任何0到中心的最远距离,计算每个0到矩阵中心的距离为|in/2| + |jn/2|并更新最大距离作为结果。最后打印结果,或者如果矩阵不包含任何0则打印0 。
下面是上述方法的实现:
C++
// C++ program to find the farthest distance
// of a 0 from the center of the matrix
#include
#define n 3
using namespace std;
// function to return farthest distance
// of zero from center of the matrix
int farthestDistance(int matrix[][n])
{
int result = 0;
// traverse the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0)
result = max(result
, abs(i - n/2) + abs(j - n/2));
}
}
// return result
return result;
}
// driver program
int main()
{
int matrix[n][n] = { { 1, 2, 3 }
, { 0, 1, 1 }
, { 0, 0, 0 } };
cout << farthestDistance(matrix);
return 0;
}
Java
// Java program to find the farthest distance
// of a 0 from the center of the matrix
import java.io.*;
class GFG {
static int n = 3;
// function to return farthest distance
// of zero from center of the matrix
static int farthestDistance(int matrix[][])
{
int result = 0;
// traverse the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0)
result = Math.max(result
, Math.abs(i - n/2) + Math.abs(j - n/2));
}
}
// return result
return result;
}
// driver program
public static void main (String[] args) {
int matrix[][] = { { 1, 2, 3 }
, { 0, 1, 1 }
, { 0, 0, 0 } };
System.out.print(farthestDistance(matrix));
}
}
// This code is contributed by anuj_67..
Python3
# Python3 program to find the farthest distance
# of a 0 from the center of the matrix
n = 3
# function to return farthest distance
# of zero from center of the matrix
def farthestDistance(matrix):
result = 0
# traverse the matrix
for i in range (0, n):
for j in range (0, n):
if (matrix[i][j] == 0):
result = max(result, abs(i - n // 2) +
abs(j - n//2))
# return result
return result
# Driver Code
matrix = [[1, 2, 3],
[0, 1, 1],
[0, 0, 0]]
print(farthestDistance(matrix))
# This code is contributed by
# Archana_kumari
C#
// C# program to find the farthest distance
// of a 0 from the center of the matrix
using System;
class GFG
{
static int n = 3;
// function to return farthest distance
// of zero from center of the matrix
static int farthestDistance(int [,]matrix)
{
int result = 0;
// traverse the matrix
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (matrix[i,j] == 0)
result = Math.Max(result ,
Math.Abs(i - n / 2) +
Math.Abs(j - n / 2));
}
}
// return result
return result;
}
// Driver Code
static public void Main ()
{
int [,]matrix = {{ 1, 2, 3 },
{ 0, 1, 1 },
{ 0, 0, 0 }};
Console.WriteLine(farthestDistance(matrix));
}
}
// This code is contributed by Sachin
PHP
0)
$result = max($result,
abs($i - $n / 2) +
abs($j - $n / 2));
}
}
// return result
return $result;
}
// Driver Code
$matrix = array(array( 1, 2, 3 ),
array( 0, 1, 1 ),
array( 0, 0, 0 ));
echo farthestDistance($matrix);
// This code is contributed by Sach_code
?>
Javascript
输出:
2