Problem p03638 (python)

Fixed submission:
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))

graph = [[-1] * W for _ in range(H)]
h, w = 0, 0 #現在地

if H == W == 1:
    print (1)
    exit()

#動ける範囲
right = W - 1
left = 0
up = 0
down = H - 1

tmp = 0
count = 1
flag = True
while count <= H * W:
    while w < right and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w += 1
    right -= 1
    while h < down and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h += 1
    down -= 1
    while left < w and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w -= 1
    left += 1
    while up < h and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h -= 1
    up += 1
    h += 1
    w += 1
    # print (h, w)
    # print ('right = ', right)
    # print ('left = ', left)
    # print ('up = ', up)
    # print ('down = ', down)
    if right == left == 1:
        right += 1
    if up == down == 1:
        down += 1
    if right == left and up == down:
        right += 1

for i in graph:
    print (*i)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))

graph = [[-1] * W for _ in range(H)]
h, w = 0, 0 #現在地

if H == W == 1:
    print (1)
    exit()

#動ける範囲
right = W - 1
left = 0
up = 0
down = H - 1

tmp = 0
count = 1
flag = True
while count <= H * W:
    if right == left == W //2 or right == left == 1:
        right += 1
    if up == down == H // 2 or up == down == 1:
        down += 1
    while w < right and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w += 1
    right -= 1
    while h < down and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h += 1
    down -= 1
    while left < w and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w -= 1
    left += 1
    while up < h and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h -= 1
    up += 1
    h += 1
    w += 1
    # print (h, w)
    # print ('right = ', right)
    # print ('left = ', left)
    # print ('up = ', up)
    # print ('down = ', down)

for i in graph:
    print (*i)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))

graph = [[-1] * W for _ in range(H)]
h, w = 0, 0 #現在地

if H == W == 1:
    print (1)
    exit()

#動ける範囲
right = W - 1
left = 0
up = 0
down = H - 1

tmp = 0
count = 1
flag = True
while count <= H * W:
    if right == left == W //2:
        right += 1
    if up == down == H // 2:
        down += 1
    while w < right and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w += 1
    right -= 1
    while h < down and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h += 1
    down -= 1
    while left < w and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w -= 1
    left += 1
    while up < h and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h -= 1
    up += 1
    h += 1
    w += 1
    # print (h, w)
    # print ('right = ', right)
    # print ('left = ', left)
    # print ('up = ', up)
    # print ('down = ', down)

for i in graph:
    print (*i)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))

graph = [[-1] * W for _ in range(H)]
h, w = 0, 0 #現在地

#動ける範囲
right = W - 1
left = 0
up = 0
down = H - 1

tmp = 0
count = 1
flag = True
while count <= H * W:
    if right == left:
        right += 1
    if up == down:
        down += 1
    while w < right and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w += 1
    right -= 1
    while h < down and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h += 1
    down -= 1
    while left < w and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w -= 1
    left += 1
    while up < h and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h -= 1
    up += 1
    h += 1
    w += 1
    # print (h, w)
    # print ('right = ', right)
    # print ('left = ', left)
    # print ('up = ', up)
    # print ('down = ', down)


for i in graph:
    print (*i)

Buggy submission (verdict: Runtime Error):
H, W = map(int, input().split())
N = int(input())
a = list(map(int, input().split()))

# if H == W == 1:
#     print (1)
#     exit()

graph = [[-1] * W for _ in range(H)]
h, w = 0, 0 #現在地

#動ける範囲
right = W - 1
left = 0
up = 0
down = H - 1

tmp = 0
count = 1
flag = True
while count <= H * W:
    if right == left:
        right += 1
    if up == down:
        down += 1
    while w < right and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w += 1
    right -= 1
    while h < down and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h += 1
    down -= 1
    while left < w and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        w -= 1
    left += 1
    while up < h and flag:
        # print (h, w)
        graph[h][w] = tmp + 1
        a[tmp] -= 1
        if a[tmp] == 0:
            tmp += 1
        count += 1
        if count > H * W:
            flag = False
            break
        h -= 1
    up += 1
    h += 1
    w += 1
    # print (h, w)
    # print ('right = ', right)
    # print ('left = ', left)
    # print ('up = ', up)
    # print ('down = ', down)
    if right == left:
        right += 1
    if up == down:
        down += 1


for i in graph:
    print (*i)