Problem p03157 (python)

Fixed submission:

import numpy as np
from collections import deque
import sys

def parse(inp):
    H, W = map(int, next(inp).split())
    return np.array([list(next(inp).strip()) for _ in range(H)])

dis = [0, 0, 1, -1]
djs = [1, -1, 0, 0]

def search(S, i, j, Q, R):
    H, W = S.shape
    R[i, j] = True
    Q.append((i, j))
    n0 = 0
    n1 = 0
    while Q:
        i, j = Q.popleft()
        if S[i, j] == "#":
            n1 += 1
        else:
            n0 += 1
        gen = ((i + di, j + dj) for di, dj in zip(dis, djs))
        ijs = (g for g in gen if 0 <= g[0] and g[0] < H and 0 <= g[1] and g[1] < W)
        for ni, nj in ijs:
            if not R[ni, nj] and S[ni, nj] != S[i, j]:
                R[ni, nj] = True
                Q.append((ni, nj))
    return n0 * n1


def test(S):
    i, j = 0, 0
    Q = deque()
    R = np.full(S.shape, False)
    H, W = S.shape
    n = 0
    for i in range(H):
        for j in range(W):
            if not R[i, j]:
                n += search(S, i, j, Q, R)
    return n

print(test(parse(sys.stdin)))

Buggy submission (verdict: Runtime Error):
import numpy as np
from collections import deque

@np.vectorize
def trans(s):
    return True if s == "#" else False

def parse(inp):
    H, W = map(int, next(inp).split())
    return np.array([trans([w for w in next(inp).strip()]) for _ in range(H)])

def appender(i, j, H, W, C, S, Q):
    s = S[i, j]
    if i + 1 < H and not C[i+1, j] and np.logical_xor(s, S[i+1, j]):
        Q.append((i + 1, j))
    if i > 0 and not C[i-1, j] and np.logical_xor(s, S[i-1, j]):
        Q.append((i - 1, j))
    if j + 1 < W and not C[i, j+1] and np.logical_xor(s, S[i, j+1]):
        Q.append((i, j + 1))
    if j > 0 and not C[i, j-1] and np.logical_xor(s, S[i, j-1]):
        Q.append((i, j - 1))

def search(S, start, C):
    Q = deque([start])
    H, W = S.shape
    n0 = 0
    n1 = 0
    while Q:
        i, j = Q.popleft()
        if C[i, j]:
            continue
        else:
            C[i, j] = True
            if S[i, j] == 0:
                n0 += 1
            else:
                n1 += 1
            appender(i, j, H, W, C, S, Q)
    return n0, n1


def search_all(S):
    C = np.full_like(S, False).astype(np.bool)
    D = np.full_like(S, 0)
    ret = 0
    for i in range(S.shape[0]):
        for j in range(S.shape[1]):
            if D[i, j] == 0:
                n0, n1 = search(S, (i, j), C)
                D += C
                ret += n1 * n0
                if np.all(D):
                    return ret
                else:
                    C[:, :] = False




import sys
    
print(search_all(parse(sys.stdin)))

Buggy submission (verdict: Runtime Error):
import numpy as np
from collections import deque

@np.vectorize
def trans(s):
    return 1 if s == "#" else 0

def parse(inp):
    H, W = map(int, next(inp).split())
    return np.array([trans([w for w in next(inp).strip()]) for _ in range(H)])

def search(S, start, C):
    Q = deque([start])
    H, W = S.shape
    n0 = 0
    n1 = 0
    while Q:
        i, j = Q.popleft()
        if C[i, j] != 0:
            continue
        else:
            C[i, j] = 1
            if S[i, j] == 0:
                n0 += 1
            else:
                n1 += 1
        if i + 1 < H and C[i+1, j] == 0 and S[i, j] != S[i+1, j]:
            Q.append((i + 1, j))
        if i > 0 and C[i-1, j] == 0 and S[i, j] != S[i-1, j]:
            Q.append((i - 1, j))
        if j + 1 < W and C[i, j+1] == 0 and S[i, j] != S[i, j+1]:
            Q.append((i, j + 1))
        if j > 0 and C[i, j-1] == 0 and S[i, j] != S[i, j-1]:
            Q.append((i, j - 1))
    return n0, n1


def search_all(S):
    C = np.full_like(S, 0)
    D = np.full_like(S, 0)
    ret = 0
    for i in range(S.shape[0]):
        for j in range(S.shape[1]):
            if D[i, j] == 0:
                n0, n1 = search(S, (i, j), C)
                D += C
                ret += n1 * n0
                if np.all(D):
                    return ret
                else:
                    C[:, :] = 0

    
print(search_all(parse(sys.stdin)))

Buggy submission (verdict: Runtime Error):
import numpy as np
from scipy.sparse.csgraph import dijkstra

@np.vectorize
def trans(s):
    return 1 if s == "#" else 0

H, W = map(int, input().split())
S = np.array([trans([w for w in input()]) for _ in range(H)]).flatten()

def adjacency_matrix(H, W):
    t1 = [1 if i % W != W - 1 else 0 for i in range(H * W - 1)]
    t2 = [1 for _ in range(H * W - W)]
    return np.diag(t1, k=1) + np.diag(t1, k=-1) + np.diag(t2, k=W) + np.diag(t2, k=-W)
    
def different_colors(colors):
    return np.array([np.abs(colors - c) for c in colors])


ad = adjacency_matrix(H, W)
df = different_colors(S)

di = dijkstra(ad * df)
co = np.logical_and(di > 0, di < np.inf)

print(int(np.sum(co * df) / 2))