Skip to content

Instantly share code, notes, and snippets.

@shiracamus
Last active September 26, 2019 04:17
Show Gist options
  • Save shiracamus/15125218458c5bd411344e2fcab0b78e to your computer and use it in GitHub Desktop.
Save shiracamus/15125218458c5bd411344e2fcab0b78e to your computer and use it in GitHub Desktop.
from queue import Queue
def solve():
width, height = map(int, input().split())
world = [[*map(int, input().split()), 0] for _ in range(height)] + [[0] * width]
iland = 0
q = Queue()
for y in range(height):
for x in range(width):
if world[y][x]:
iland += 1
q.put((y, x))
while not q.empty():
y, x = q.get()
world[y][x] = 0
if world[y-1][x]: q.put((y-1, x))
if world[y+1][x]: q.put((y+1, x))
if world[y][x-1]: q.put((y, x-1))
if world[y][x+1]: q.put((y, x+1))
print(iland)
if __name__ == "__main__":
solve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment