Oracle Interview Question
Company: Oracle
Role: unknown
Interview Stage: unknown
Location: unknown
Year: unknown
Original Post Content:
Virus Transmission
You’re given a rectangular petri dish divided into m rows, n columns, each subdivision containing cells or left empty. Cells can be infected (0), weak (1), strong (2) or empty (_). An infected cell can infect adjacent healthy (both weak and strong) cells. Once adjacent to the infected, it takes a day for a weak cell to get infected and be able to transmit the virus, and two days for a strong one. The objective is to find the number of days required to infect all weak and strong cells.
A subdivision can have a maximum of 4 adjacent subdivisions as show below. Infected cell is shown in red and it’s adjacent 4 cells are shown by black dots.
virus.png
Input Format
The first line contains and integer t denoting the number of test cases. The second line consists of two space separated integers m and n. The next m rows each contain n characters.
Output Format
For each test case output the number of days required. If it’s not possible to turn all healthy cells to infected, output -1. If there are no healthy cells output 0.
Sample Input
4
4 5
02_20
21212
_121_
__2__
3 3
_1_
101
_1_
2 3
1_2
_0_
1 2
0_
Sample Output
8
1
-1
0
It takes a total of 8 days.
Infected cell is shown in red, weak in blue and strong in green. Intermediate state of a strong cell after first day of infection is shown in orange.
Constraints
1 <= t <= 1000
1 <= m,n <= 100
Environment
Read from STDIN and write to STDOUT.
Please check the sample programs below which print the sum of two numbers received as input
C++ bit.ly/2Io1VND
Java bit.ly/3dc9uDT (Remove package declarations and keep the main class (class containing the main method) name as “solution” (small case))