Rezultati

Up. imeNalogaJezikRezultatČas oddaje
ocetje-interneta-2019 Gozdovi Python 3 100/100OK 24. apr '19 @ 17:49

Test Točke Porabljen spomin Porabljen čas Status
#1 [več] 10/10 9,359 MiB 0,000 s OK
#2 [več] 10/10 10,328 MiB 0,754 s OK
#3 [več] 10/10 21,633 MiB 1,563 s OK
#4 [več] 10/10 10,355 MiB 1,038 s OK
#5 [več] 10/10 9,449 MiB 2,383 s OK
#6 [več] 10/10 9,742 MiB 0,947 s OK
#7 [več] 10/10 9,480 MiB 1,174 s OK
#8 [več] 10/10 9,570 MiB 1,195 s OK
#9 [več] 10/10 9,563 MiB 0,987 s OK
#10 [več] 10/10 9,488 MiB 0,996 s OK

Ocenjevani program (gozd.py):
from collections import deque

def bfs(init_x, init_y, forest_type):

    q = deque()  
    q.append([init_x, init_y])
    
    while len(q) != 0:
        x, y = q.popleft()

        if (x < 0 or y < 0 or x >= n or y >= m):
            continue        
        
        if visited[x][y] == True:
            continue

        if forest[x][y] != forest_type:
            continue
    
        visited[x][y] = True
        
        q.append([x+1,y+1])
        q.append([x+1,y]) 
        q.append([x+1,y-1]) 
        q.append([x,y+1])
        q.append([x,y-1])
        q.append([x-1,y+1])
        q.append([x-1,y])
        q.append([x-1,y-1])



while True:

    n, m = list(map(int, input().split(' ')))
    
    if n == 0 or m == 0:
        break

    visited = []
    forest = []

    for i in range(n):
        visited.append([False for i in range(m)])

        forest.append(input())
        
    counter = 0

    for i in range(n):
        for j in range(m):
            if not visited[i][j]:
                bfs(i, j, forest[i][j])
                counter += 1

    print(counter)