给定N * M矩阵和病毒的起始位置(X,Y) ,如果病毒每天从其当前细胞扩散到其相邻细胞,则任务是找出D天后被覆盖细胞的数量。
例子:
Input: N = 5, M = 5, X = 1, Y = 3, D = 3
Output: 15
Explanation:
We can clearly see from the picture that 15 cells are covered after 3 days.
Input: N = 10, M = 10, X = 7, Y = 8, D = 4
Output: 42
Explanation:
On making an N * M matrix and filling the adjacent cells for 4 days we will get 42 covered cells.
方法:
为了解决上述问题,我们必须清楚地观察到,从起始单元格开始,我们只需要找出D天后单元格向顶部,右侧,底部和左侧的扩展。然后计算形成的单元格的每个四边形内的总单元格,并将它们全部相加。
因此,总答案将是D天后四边形的所有像元的总和+沿上,右,下,左和1(对于起始像元)的总像元,并考虑四边形的边界。
以下是在所有四个方向上扩展的条件:
Extension upto Top -> min(D, X-1)
Extension upto Down -> min(D, N-X)
Extension upto Left -> min(D, Y-1)
Extension upto Right -> min(D, M-Y)
请看下面的图片,以清楚地了解:
现在,将“顶部*左” ,“顶部*右” ,“向下” *“左” ,“向下” *“右”相乘,并将它们全部相加,并沿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)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。