forked from prateekshyap/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CovidSpread.java
96 lines (87 loc) · 2.64 KB
/
CovidSpread.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*https://practice.geeksforgeeks.org/problems/269f61832b146dd5e6d89b4ca18cbd2a2654ebbe/1/*/
//BFS approach
class Cell implements Comparable<Cell>
{
int r, c;
Cell(int r, int c)
{
this.r = r;
this.c = c;
}
public int compareTo(Cell c)
{
return 0;
}
}
class Solution{
public int helpaterp(int[][] hospital) {
int time = 0;
int R = hospital.length;
int C = hospital[0].length;
Queue<Cell> queue = new LinkedList<Cell>();
//add infected wards to the queue
for (int i = 0; i < R; ++i)
{
for (int j = 0; j < C; ++j)
{
if (hospital[i][j] == 2)
{
queue.add(new Cell(i,j));
}
}
}
//till the queue becomes empty
while (!queue.isEmpty())
{
//initialize a boolean variable to indicate new infections in this iteration
boolean isInfected = false;
//for the currently infected wards
int len = queue.size();
for (int i = 0; i < len; ++i)
{
//get the infected cell
Cell c = queue.poll();
//check if any adjacent cells can be infected and mark it
if (c.r+1 < R && hospital[c.r+1][c.c] == 1)
{
isInfected = true;
hospital[c.r+1][c.c] = 2;
queue.add(new Cell(c.r+1,c.c));
}
if (c.c+1 < C && hospital[c.r][c.c+1] == 1)
{
isInfected = true;
hospital[c.r][c.c+1] = 2;
queue.add(new Cell(c.r,c.c+1));
}
if (c.r-1 >= 0 && hospital[c.r-1][c.c] == 1)
{
isInfected = true;
hospital[c.r-1][c.c] = 2;
queue.add(new Cell(c.r-1,c.c));
}
if (c.c-1 >= 0 && hospital[c.r][c.c-1] == 1)
{
isInfected = true;
hospital[c.r][c.c-1] = 2;
queue.add(new Cell(c.r,c.c-1));
}
}
//if new infections occured, increase time
if (isInfected) ++time;
}
//if any uninfected patient present, store -1 in time
for (int i = 0; i < R; ++i)
{
for (int j = 0; j < C; ++j)
{
if (hospital[i][j] == 1)
{
time = -1;
break;
}
}
}
return time;
}
}