📌  相关文章
📜  矩阵中距给定单元格最远的单元格

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

给定整数NMRC ,其中NM表示矩阵中的行数和列数, (R, C)表示该矩阵中的一个单元格,任务是找到离矩阵最远的单元格的距离细胞(R,C)
注意:矩阵一次只能水平或垂直遍历。

例子:

朴素的方法:最简单的方法是遍历矩阵并计算矩阵的每个单元格与给定单元格(R,C)的距离并保持所有距离的最大值。

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

有效的方法:为了优化上述方法,观察到距矩阵中任何单元最远的单元将是四个最角落单元之一,即(1, 1), (1, M), (N, 1), (N, M)

请按照以下步骤解决问题:

  • 初始化d1 , d2 , d3d4分别等于N + M – R – C , R + C – 2 , N – R + C – 1M – C + R – 1
  • 打印d1d2d3d4 中的最大值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the farthest
// cell distance from the given cell
void farthestCellDistance(int N, int M,
                          int R, int C)
{
    // Distance from all the
    // cornermost cells
 
    // From cell(N, M)
    int d1 = N + M - R - C;
 
    // From Cell(1, 1)
    int d2 = R + C - 2;
 
    // From cell(N, 1)
    int d3 = N - R + C - 1;
 
    // From cell(1, M)
    int d4 = M - C + R - 1;
 
    // Finding out maximum
    int maxDistance = max(d1,
                          max(d2,
                              max(d3, d4)));
 
    // Print the answer
    cout << maxDistance;
}
 
// Driver Code
int main()
{
    int N = 15, M = 12, R = 1, C = 6;
    farthestCellDistance(N, M, R, C);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
 
// Function to find the farthest
// cell distance from the given cell
static void farthestCellDistance(int N, int M,
                                 int R, int C)
{
     
    // Distance from all the
    // cornermost cells
 
    // From cell(N, M)
    int d1 = N + M - R - C;
 
    // From Cell(1, 1)
    int d2 = R + C - 2;
 
    // From cell(N, 1)
    int d3 = N - R + C - 1;
 
    // From cell(1, M)
    int d4 = M - C + R - 1;
 
    // Finding out maximum
    int maxDistance = Math.max(d1, Math.max(
                  d2, Math.max(d3, d4)));
 
    // Print the answer
    System.out.println(maxDistance);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 15, M = 12, R = 1, C = 6;
     
    farthestCellDistance(N, M, R, C);
}
}
 
// This code is contributed by Dharanendra L V


Python3
# Python program for the above approach
 
# Function to find the farthest
# cell distance from the given cell
def farthestCellDistance(N, M, R, C):
   
    # Distance from all the
    # cornermost cells
 
    # From cell(N, M)
    d1 = N + M - R - C;
 
    # From Cell(1, 1)
    d2 = R + C - 2;
 
    # From cell(N, 1)
    d3 = N - R + C - 1;
 
    # From cell(1, M)
    d4 = M - C + R - 1;
 
    # Finding out maximum
    maxDistance = max(d1, max(d2, max(d3, d4)));
 
    # Prthe answer
    print(maxDistance);
 
# Driver Code
if __name__ == '__main__':
    N = 15;
    M = 12;
    R = 1;
    C = 6;
 
    farthestCellDistance(N, M, R, C);
 
# This code is contributed by shikhasingrajput


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the farthest
// cell distance from the given cell
static void farthestCellDistance(int N, int M,
                                 int R, int C)
{
     
    // Distance from all the
    // cornermost cells
 
    // From cell(N, M)
    int d1 = N + M - R - C;
 
    // From Cell(1, 1)
    int d2 = R + C - 2;
 
    // From cell(N, 1)
    int d3 = N - R + C - 1;
 
    // From cell(1, M)
    int d4 = M - C + R - 1;
 
    // Finding out maximum
    int maxDistance = Math.Max(d1, Math.Max(
                  d2, Math.Max(d3, d4)));
 
    // Print the answer
    Console.WriteLine(maxDistance);
}
 
// Driver Code
static public void Main()
{
    int N = 15, M = 12, R = 1, C = 6;
     
    farthestCellDistance(N, M, R, C);
}
}
 
// This code is contributed by Dharanendra L V


Javascript


输出:
20

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程