Rezultati

Up. imeNalogaJezikRezultatČas oddaje
boku-yes-pico-2019 Gozdovi Python 3 100/100OK 24. apr '19 @ 17:17

Test Točke Porabljen spomin Porabljen čas Status
#1 [več] 10/10 8,598 MiB 0,000 s OK
#2 [več] 10/10 9,395 MiB 0,669 s OK
#3 [več] 10/10 20,160 MiB 1,525 s OK
#4 [več] 10/10 9,438 MiB 0,705 s OK
#5 [več] 10/10 8,695 MiB 1,963 s OK
#6 [več] 10/10 8,750 MiB 0,822 s OK
#7 [več] 10/10 8,656 MiB 0,831 s OK
#8 [več] 10/10 8,703 MiB 0,841 s OK
#9 [več] 10/10 8,660 MiB 0,857 s OK
#10 [več] 10/10 8,652 MiB 0,632 s OK

Ocenjevani program (gozdovi.py):
n = 0
m = 0

def flood_fill(img, i, j, c):
    if i >= n or j >= m or j < 0 or i < 0:
        return
    if img[i][j] != c:
        return
    img[i][j] = '*'
    flood_fill(img, i+1, j, c)
    flood_fill(img, i, j+1, c)
    flood_fill(img, i-1, j, c)
    flood_fill(img, i, j-1, c)
    flood_fill(img, i+1, j+1, c)
    flood_fill(img, i+1, j-1, c)
    flood_fill(img, i-1, j-1, c)
    flood_fill(img, i-1, j+1, c)


def flood_fill2(img, i, j, c):
    stack = list()
    stack.append((i, j))
    while stack:
        ci, cj = stack.pop(0)
        # print(ci, cj)
        if ci >= n or cj >= m or cj < 0 or ci < 0:
            continue
        if img[ci][cj] != c:
            continue
        img[ci][cj] = '*'
        stack.append((ci+1, cj))
        stack.append((ci, cj+1))
        stack.append((ci-1, cj))
        stack.append((ci, cj-1))
        stack.append((ci+1, cj+1))
        stack.append((ci+1, cj-1))
        stack.append((ci-1, cj-1))
        stack.append((ci-1, cj+1))



while True:
    n, m = map(int, input().split())
    
    if n == 0 and m == 0:
        break
    
    img = list()
    for _ in range(n):
        img.append(list(input()))

    # img = list()
    # img.append(['K']*240000)
    # n = 1
    # m = 230000
    
    c = 0
    for i in range(n):
        for j in range(m):
            if img[i][j] == '*':
                continue
            flood_fill2(img, i, j, img[i][j])
            c += 1
            # print(img)
    print(c)
    # break