Last active
September 26, 2019 04:17
-
-
Save shiracamus/15125218458c5bd411344e2fcab0b78e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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