Nov-13-2019, 02:11 PM
Task is described as:
So subtasks can be defined as:
- find zombies
- infect neighbours adjacent to zombies
- repeat until all are zombies
You must separate finding zombies and infecting neighbours otherwise you will get incorrect result (how to separate existed zombies and newly created zombies).
Finding zombies is relatively easy. As we need to infect neighbours we should use indices to identify the location. For achieving that we can 'iterate over enumerated rows and for every enumerated item in row we return row-item number pair if item value is 1'
How to find neighbours?
If we have coordinate of zombie, then to get previous (to the left of zombie) we should change coordinates (0, -1), to get next (to the right of zombie) we shold change (0, 1), for row above (-1, 0) and row below (1, 0).
So we calculate coordinates of infected cells and change their values. What we should take into consideration: we shouldn't allow negative values (it will start counting from end); we will get IndexError for values in first and last row and column. Both of these are easily mitigated.
If we have 'infected' neighbours we should check whether humans are still present and if so repeat.
Quote:Given a 2D grid, each cell is either a zombie 1 or a human 0. Zombies can turn adjacent (up / down / left / right) human beings into zombies every hour. Find out how many hours does it take to infect all humans?
So subtasks can be defined as:
- find zombies
- infect neighbours adjacent to zombies
- repeat until all are zombies
You must separate finding zombies and infecting neighbours otherwise you will get incorrect result (how to separate existed zombies and newly created zombies).
Finding zombies is relatively easy. As we need to infect neighbours we should use indices to identify the location. For achieving that we can 'iterate over enumerated rows and for every enumerated item in row we return row-item number pair if item value is 1'
zombies = ((i, j) for i, row in enumerate(m) for j, item in enumerate(row) if item == 1)Now we have locations (row, col) of zombies.
How to find neighbours?
If we have coordinate of zombie, then to get previous (to the left of zombie) we should change coordinates (0, -1), to get next (to the right of zombie) we shold change (0, 1), for row above (-1, 0) and row below (1, 0).
So we calculate coordinates of infected cells and change their values. What we should take into consideration: we shouldn't allow negative values (it will start counting from end); we will get IndexError for values in first and last row and column. Both of these are easily mitigated.
If we have 'infected' neighbours we should check whether humans are still present and if so repeat.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.