📜  D 天后矩阵中覆盖的细胞总数

📅  最后修改于: 2021-10-26 06:24:12             🧑  作者: Mango

给定N * M矩阵和病毒的起始位置(X, Y) ,如果病毒每天从当前细胞传播到相邻细胞,则任务是找出D天后覆盖细胞的数量。

例子:

方法:
为了解决上面提到的问题,我们必须清楚地观察到,从一个起始单元格开始,我们只需要找出D天后单元格向顶部、右侧、底部左侧延伸。然后计算形成的每个四边形单元格内的总单元并将它们全部相加。
因此,总答案将是D 天后四边形所有单元格的总和 + 沿顶部、右侧、底部、左侧和 1(用于起始单元格)的总单元格,同时考虑四边形的边界。
以下是所有四个方向的扩展条件:

请看下图,以便清楚了解:

现在乘以Top * Left , Top * Right , Down * Left , Down * Right并添加所有这些,并沿 4 个方向的线添加总单元格。我们还添加1 (用于起始单元格)以获得结果单元格。
下面是上述方法的实现:

C++
// C++ implementation to find the
// Total number of cells covered
// in a matrix after D days
 
#include 
using namespace std;
 
// Function to return the total
// infected cells after d days
int solve(int n, int m, int x,
        int y, int d)
{
    // Top extension
    int top = min(d, x - 1);
 
    // Bottom extension
    int down = min(d, n - x);
 
    // Left extension
    int left = min(d, y - 1);
 
    // Right extension
    int right = min(d, m - y);
 
    // Calculating the cells
    // in each quadrilateral
    int quad1 = top * left;
    int quad2 = left * down;
    int quad3 = down * right;
    int quad4 = right * top;
 
    // Sum all of them to get
    // total cells in each
    // quadrilateral
    int totalsq = quad1 + quad2
                + quad3 + quad4;
 
    // Add the singleblocks
    // along the lines of top,
    // down, left, right
    int singleBlocks = top + down
                    + left + right + 1;
 
    // Return the ans
    return totalsq + singleBlocks;
}
 
// Driver code
int main()
{
    int n, m, x, y, d;
 
    // Dimensions of cell
    n = 10, m = 10;
 
    // Starting Coordinates
    x = 7, y = 8;
 
    // Number of Days
    d = 4;
    d--;
 
    // Function Call
    cout << solve(n, m, x, y, d);
}


Java
// Java implementation to find the
// total number of cells covered
// in a matrix after D days
import java.util.*;
 
class GFG{
 
// Function to return the total
// infected cells after d days
static int solve(int n, int m, int x,
                 int y, int d)
{
     
    // Top extension
    int top = Math.min(d, x - 1);
 
    // Bottom extension
    int down = Math.min(d, n - x);
 
    // Left extension
    int left = Math.min(d, y - 1);
 
    // Right extension
    int right = Math.min(d, m - y);
 
    // Calculating the cells
    // in each quadrilateral
    int quad1 = top * left;
    int quad2 = left * down;
    int quad3 = down * right;
    int quad4 = right * top;
 
    // Sum all of them to get
    // total cells in each
    // quadrilateral
    int totalsq = quad1 + quad2 +
                  quad3 + quad4;
 
    // Add the singleblocks
    // along the lines of top,
    // down, left, right
    int singleBlocks = top + down +
                      left + right + 1;
 
    // Return the ans
    return totalsq + singleBlocks;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Dimensions of cell
    int n = 10, m = 10;
 
    // Starting coordinates
    int x = 7, y = 8;
 
    // Number of days
    int d = 4;
    d--;
 
    // Function call
    System.out.println(solve(n, m, x, y, d));
}
}
 
// This code is contributed by Pratima Pandey


Python3
# Python3 implementation to find the
# total number of cells covered in
# a matrix after D days
 
# Function to return the total
# infected cells after d days
def solve(n, m, x, y, d):
 
    # Top extension
    top = min(d, x - 1)
 
    # Bottom extension
    down = min(d, n - x)
 
    # Left extension
    left = min(d, y - 1)
 
    # Right extension
    right = min(d, m - y)
 
    # Calculating the cells
    # in each quadrilateral
    quad1 = top * left
    quad2 = left * down
    quad3 = down * right
    quad4 = right * top
 
    # Sum all of them to get
    # total cells in each
    # quadrilateral
    totalsq = (quad1 + quad2 +
               quad3 + quad4)
 
    # Add the singleblocks
    # along the lines of top,
    # down, left, right
    singleBlocks = (top + down +
                   left + right + 1)
 
    # Return the ans
    return totalsq + singleBlocks
 
# Driver Code
if __name__ == '__main__':
 
    # Dimensions of cell
    n = 10
    m = 10
 
    # Starting Coordinates
    x = 7
    y = 8
 
    # Number of Days
    d = 4
    d -= 1
 
    # Function Call
    print(solve(n, m, x, y, d))
 
# This code is contributed by Shivam Singh


C#
// C# implementation to find the
// total number of cells covered
// in a matrix after D days
using System;
class GFG{
 
// Function to return the total
// infected cells after d days
static int solve(int n, int m, int x,
                 int y, int d)
{
     
    // Top extension
    int top = Math.Min(d, x - 1);
 
    // Bottom extension
    int down = Math.Min(d, n - x);
 
    // Left extension
    int left = Math.Min(d, y - 1);
 
    // Right extension
    int right = Math.Min(d, m - y);
 
    // Calculating the cells
    // in each quadrilateral
    int quad1 = top * left;
    int quad2 = left * down;
    int quad3 = down * right;
    int quad4 = right * top;
 
    // Sum all of them to get
    // total cells in each
    // quadrilateral
    int totalsq = quad1 + quad2 +
                  quad3 + quad4;
 
    // Add the singleblocks
    // along the lines of top,
    // down, left, right
    int singleBlocks = top + down +
                      left + right + 1;
 
    // Return the ans
    return totalsq + singleBlocks;
}
 
// Driver code
public static void Main(String[] args)
{
     
    // Dimensions of cell
    int n = 10, m = 10;
 
    // Starting coordinates
    int x = 7, y = 8;
 
    // Number of days
    int d = 4;
    d--;
 
    // Function call
    Console.WriteLine(solve(n, m, x, y, d));
}
}
 
// This code is contributed by Rohit_ranjan


Javascript


输出:
42

时间复杂度: O(1)

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