0% found this document useful (0 votes)
34 views2 pages

Input Format: M N H I X

The document describes a problem to find the minimum number of days required to infect all healthy plants in a field divided into rows and columns, where each cell is marked with h, i, or x to indicate a healthy, infected, or empty plant. In each day, an infected plant can infect its neighboring healthy plants. The output is the number of days or -1 if not possible to infect all plants or 0 if there are no healthy plants. Sample input and output is provided along with an example explanation.

Uploaded by

THULASI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

Input Format: M N H I X

The document describes a problem to find the minimum number of days required to infect all healthy plants in a field divided into rows and columns, where each cell is marked with h, i, or x to indicate a healthy, infected, or empty plant. In each day, an infected plant can infect its neighboring healthy plants. The output is the number of days or -1 if not possible to infect all plants or 0 if there are no healthy plants. Sample input and output is provided along with an example explanation.

Uploaded by

THULASI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

The Infected Plant

Given a field divided into  m  rows,  n  columns, each cell containing  h  (denoting a healthy plant),  i  (denoting a
infected plant) or  x  (denoting empty spot), find the number of days required to turn all healthy plants to infected. In
one day, a infected plant can make infect neighboring healthy plants. A cell can have at most 4 neighbors (left, right,
front and back).
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 plants to infected,
output  -1 . If there are no healthy plants output  0 .
Sample Input

9
2 3
ixh
hhh
1 2
ix
3 3
hhh
hih
hhh
1 3
hxi
2 1
h
x
3 3
ihh
hhh
hhh
1 9
hixhihhhx
3 1
h
i
h
1 1
x

Sample Output

4
0
2
-1
-1
4
3
1
0

Explanation
For the case

ixh
hhh

After Day 1

ixh
ihh

After Day 2

ixh
iih

After Day 3

ixh
iii

After Day 4

ixi
iii

Constraints
1 <= t <= 1000
1 <= m,n <= 100

You might also like