Total number of cells covered in a matrix after D days
Last Updated :
02 Nov, 2023
Given an N * M matrix and a starting position (X, Y) of a virus, the task is to find out the number of covered cells after D days, if the virus spreads from its current cell to its adjacent cells every day.
Examples:
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.
Approach:
To solve the problem mentioned above we have to observe clearly that from a starting cell, we just need to find out the extension of cells towards top, right, bottom and left after D days. Then calculate the total cells inside every quadrilateral of cells formed and add them all.
Therefore, the total answer will be the sum of all cells of quadrilaterals after D days + the total cells that are along the top, right, down, left, and 1 (for Starting cell) keeping in consideration the boundaries of the quadrilateral.
Below is the condition for extension in all four directions:
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)
Look at the image below for clear understanding:
Now multiply Top * Left, Top * Right, Down * Left, Down * Right and add all of them and also add the total cells along the line of 4 directions. We also add 1(for starting cell) to get the resultant cells.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int n, int m, int x, int y, int d)
{
int top = min(d, x - 1);
int down = min(d, n - x);
int left = min(d, y - 1);
int right = min(d, m - y);
int quad1 = top * left;
int quad2 = left * down;
int quad3 = down * right;
int quad4 = right * top;
int totalsq = quad1 + quad2 + quad3 + quad4;
int singleBlocks = top + down + left + right + 1;
return totalsq + singleBlocks;
}
int main()
{
int n, m, x, y, d;
n = 10, m = 10;
x = 7, y = 8;
d = 4;
d--;
cout << solve(n, m, x, y, d);
}
|
Java
import java.util.*;
class GFG {
static int solve( int n, int m, int x, int y, int d)
{
int top = Math.min(d, x - 1 );
int down = Math.min(d, n - x);
int left = Math.min(d, y - 1 );
int right = Math.min(d, m - y);
int quad1 = top * left;
int quad2 = left * down;
int quad3 = down * right;
int quad4 = right * top;
int totalsq = quad1 + quad2 + quad3 + quad4;
int singleBlocks = top + down + left + right + 1 ;
return totalsq + singleBlocks;
}
public static void main(String[] args)
{
int n = 10 , m = 10 ;
int x = 7 , y = 8 ;
int d = 4 ;
d--;
System.out.println(solve(n, m, x, y, d));
}
}
|
Python3
def solve(n, m, x, y, d):
top = min (d, x - 1 )
down = min (d, n - x)
left = min (d, y - 1 )
right = min (d, m - y)
quad1 = top * left
quad2 = left * down
quad3 = down * right
quad4 = right * top
totalsq = (quad1 + quad2 +
quad3 + quad4)
singleBlocks = (top + down +
left + right + 1 )
return totalsq + singleBlocks
if __name__ = = '__main__' :
n = 10
m = 10
x = 7
y = 8
d = 4
d - = 1
print (solve(n, m, x, y, d))
|
C#
using System;
class GFG {
static int solve( int n, int m, int x, int y, int d)
{
int top = Math.Min(d, x - 1);
int down = Math.Min(d, n - x);
int left = Math.Min(d, y - 1);
int right = Math.Min(d, m - y);
int quad1 = top * left;
int quad2 = left * down;
int quad3 = down * right;
int quad4 = right * top;
int totalsq = quad1 + quad2 + quad3 + quad4;
int singleBlocks = top + down + left + right + 1;
return totalsq + singleBlocks;
}
public static void Main(String[] args)
{
int n = 10, m = 10;
int x = 7, y = 8;
int d = 4;
d--;
Console.WriteLine(solve(n, m, x, y, d));
}
}
|
Javascript
<script>
function solve(n, m, x, y, d)
{
let top = Math.min(d, x - 1);
let down = Math.min(d, n - x);
let left = Math.min(d, y - 1);
let right = Math.min(d, m - y);
let quad1 = top * left;
let quad2 = left * down;
let quad3 = down * right;
let quad4 = right * top;
let totalsq = quad1 + quad2 +
quad3 + quad4;
let singleBlocks = top + down +
left + right + 1;
return totalsq + singleBlocks;
}
let n = 10, m = 10;
let x = 7, y = 8;
let d = 4;
d--;
document.write(solve(n, m, x, y, d));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...