Problem p03638 (python)

Fixed submission:
h, w = map(int, input().split())
n = int(input())
colors = list(map(int, input().split()))
cmap = [(i+1,a) for i,a in enumerate(colors)]
campus=[0]*(h*w)

def printmtrx(campus):
    ts=""
    for idx,i in enumerate(campus):
        if((idx+1)%w==1):
            if idx==0:
                ts += str(i) + " "
            else:
                ts+="\n"+str(i)+" "
        else:
            if (idx+1)%w==0:
                ts += str(i)
            else:
                ts+=str(i)+" "
    print(ts)

def printmtrx2(campus):
    cnt=0
    for i in range(h):
        ts=""
        for j in range(w):
            if not j==0:
                ts+=" "
            ts+=str(campus[cnt])
            cnt+=1
        print(ts)

#print(cmap)
#print(campus)

cnt=cmap[0][1]
idx=0
campusidx=0
direct=0
while cnt:
    #print(campusidx)
    #printmtrx(campus)
    #print("")
    campus[campusidx]=cmap[idx][0]
    cnt-=1
    if(cnt<1):
        idx+=1
        if(idx>=len(cmap)):
            break
        cnt=cmap[idx][1]
    campusidx+=1-2*direct
    if campusidx%w==0 and direct==0:
        campusidx+=w-1
        direct=1
    elif campusidx%w==w-1 and direct==1:
        campusidx+=w+1
        direct=0
printmtrx2(campus)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
n = int(input())
colors = list(map(int, input().split()))
cmap = [(i+1,a) for i,a in enumerate(colors)]
campus=[0]*(h*w)

def printmtrx(campus):
    ts=""
    for idx,i in enumerate(campus):
        if((idx+1)%w==1):
            if idx==0:
                ts += str(i) + " "
            else:
                ts+="\n"+str(i)+" "
        else:
            if (idx+1)%w==0:
                ts += str(i)
            else:
                ts+=str(i)+" "
    print(ts)

#print(cmap)
#print(campus)

cnt=cmap[0][1]
idx=0
campusidx=0
direct=0
while cnt:
    #print(campusidx)
    #printmtrx(campus)
    #print("")
    campus[campusidx]=cmap[idx][0]
    cnt-=1
    if(cnt<1):
        idx+=1
        if(idx>=len(cmap)):
            break
        cnt=cmap[idx][1]
    campusidx+=1-2*direct
    if campusidx%w==0 and direct==0:
        campusidx+=w-1
        direct=1
    elif campusidx%w==4 and direct==1:
        campusidx+=w+1
        direct=0
printmtrx(campus)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
n = int(input())
colors = list(map(int, input().split()))
cmap = {i+1:a for i,a in enumerate(colors)}
colors = sorted(cmap.items(),key=lambda x:x[1],reverse=True)
campus=[0]*(h*w)

def printmtrx(campus):
    ts=""
    for idx,i in enumerate(campus):
        if((idx+1)%w==1):
            if idx==0:
                ts += str(i) + " "
            else:
                ts+="\n"+str(i)+" "
        else:
            if (idx+1)%w==0:
                ts += str(i)
            else:
                ts+=str(i)+" "
    print(ts)

def isTouching(color,i,campus):
    upi = i - w
    downi = i + w
    lefti = i - 1
    righti = i + 1
    try:
        if campus[upi] == color:
            return True
    except:
        pass
    try:
        if campus[downi]==color:
            return True
    except:
        pass
    try:
        if (campus[lefti]==color and not i%w==0):
            return True
    except:
        pass
    try:
        if (campus[righti]==color and not (i+1)%w==0):
            return True
    except:
        pass
    return False

#print(colors)
#print(cmap)
idx=0
ec=0
for i in colors:
    cnt=i[1]
    #print("cnt is "+str(cnt))
    while cnt>0:
        if i[1]==cnt and campus[idx]==0:
            campus[idx]=i[0]
            idx+=1
            cnt-=1
            #print("drawed "+str(idx-1)+" with color "+str(i[0]))
        elif isTouching(i[0],idx,campus) and campus[idx]==0:
            campus[idx]=i[0]
            idx+=1
            cnt-=1
            #print("drawed " + str(idx-1) + " with color " + str(i[0]))
        elif ec>1000000:
            #print("somthing seems went wrong")
            exit()
        else:
            for j in range(h*w):
                #print("checking where to color for color "+str(i[0])+". now checking "+str(j))
                if (isTouching(i[0],j,campus) or i[1]==cnt)and campus[j]==0:
                    idx=j
                    break
                elif(j==h*w-1):
                    #print("No place to color for color "+str(i[0]))
                    #printmtrx(campus)
                    exit()
        ec+=1

#print("done. printing result...")
printmtrx(campus)