酷代码 AI
菜单
全部AI  /  热门  /  AI问答
服务商

用代码 解决以下问题并保证时间限制为1000以内 问题名称 SBACT - Slow Growing Bacteria 问题描述 Given an n×n grid of cells, a bacteria colony can colonize these cells. Their growth after every second is governed by the following rules: One new bacteria is born in cell (i, j) if and only if either one of its four neighboring cells or the cell (i, j) itself has a bacteria population more than or equal to the threshold value, k. Already living bacteria do not die. Given, the initial state of the n×n cell grid, you nee

```python def bacteria_growth(grid, n, k, m): time = 0 total_population = sum(sum(row) for row in grid) while total_population < m: new_grid = [[0 for _ in range(n)] for _ in range(n)] for i in range(n): for j in range(n): if grid[i][j] >= k or (i > 0 and grid[i - 1][j] >= k) or (i < n - 1 and grid[i + 1][j] >= k) or (j > 0 and grid[i][j - 1] >= k) or (j < n - 1 and grid[i][j + 1] >= k): new_grid[i][j] = grid[i][j] + 1 else: new_grid[i][j] = grid[i][j] grid = new_grid total_population = sum(sum(row) for row in grid) time += 1 if time > 1000: return "Not possible" return time t = int(input()) for _ in range(t): n, k, m = map(int, input().split()) grid = [] for _ in range(n): grid.append(list(map(int, input().split()))) print(bacteria_growth(grid, n, k, m)) ``` [2025-04-11 13:12:04 | AI问答 | 321点数解答]

相关提问