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

📅  最后修改于: 2021-04-29 17:17:40             🧑  作者: Mango

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

例子:

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

请看下面的图片,以清楚地了解:

现在,将“顶部*左” ,“顶部*右” ,“向下” *“左” ,“向下” *“右”相乘,并将它们全部相加,并沿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


输出:
42


时间复杂度: O(1)