← 返回 openai 的题目列表Infection Spread Problem
类型:online_judge
Implement a function to simulate the spread of an infectious disease through a city. Assume the city is a grid of size m x n where each cell can be an infected person (represented by 1), a healthy person (represented by 0), or an empty spot (represented by -1). In a single day, an infected person can spread the infection to their adjacent (up, down, left, right) healthy person. Write an algorithm to calculate the number of days for the infection to spread throughout the city. If impossible to infect everyone, return -1.
Input: A 2D integer array grid[m][n], representing the city layout. Output: An integer representing the number of days needed for complete spread. If impossible, return -1.
Example:
Input:
[[0,1,0],
[0,0,0],
[0,0,1]]
Output: 1
Note:
The city size is within the range [1, 100][1, 100].
There is at least one infected person at the start.
Example
Input
[[0,1,0],[0,0,0],[0,0,1]]
Output
1