in1=open('in19_3.txt','r+') out1=open('out_23_19_3.txt','w+') matrix1=[] matrix2=[] i=2 temp=[0]*100 top=-1 x=0 def goback(m,n): global matrix2 global i global top global temp k=matrix2[m][n] c=k%10 k=k/10 i-=1 top+=1 temp[top]=m*10+n putnext(k,c) def putzero(): global matrix1 global matrix2 global i global top global temp while top!=-1: k=temp[top] c=k%10 k=k/10 matrix1[k][c]=0 matrix2[k][c]=0 top-=1 return 1 def putnext(m,n): global x global i global matrix1 global matrix2 global top if i==54: x=1 return 1 if((m-1)>=0 and (m-1)<8 and (n-2)<8 and (n-2)>=0 and matrix1[m-1][n-2]==0): matrix1[m-1][n-2]=i matrix2[m-1][n-2]=m*10+n i+=1 if(top!=-1): putzero() putnext(m-1,n-2) elif((m-1)<8 and (m-1)>=0 and (n+2)<8 and matrix1[m-1][n+2]==0): matrix1[m-1][n+2]=i matrix2[m-1][n+2]=m*10+n i+=1 if(top!=-1): putzero() putnext(m-1,n+2) elif((m+1)<8 and (n-2)<8 and (n-2)>=0 and matrix1[m+1][n-2]==0): matrix1[m+1][n-2]=i matrix2[m+1][n-2]=m*10+n i+=1 if(top!=-1): putzero() putnext(m+1,n-2) elif((m+1)<8 and (n+2)<8 and matrix1[m+1][n+2]==0): matrix1[m+1][n+2]=i matrix2[m+1][n+2]=m*10+n i+=1 if(top!=-1): putzero() putnext(m+1,n+2) elif((m-2)>=0 and (m-2)<8 and (n-1)<8 and (n-1)>=0 and matrix1[m-2][n-1]==0): matrix1[m-2][n-1]=i matrix2[m-2][n-1]=m*10+n i+=1 if(top!=-1): putzero() putnext(m-2,n-1) elif((m-2)<8 and (m-2)>=0 and (n+1)<8 and n+1>=0 and matrix1[m-2][n+1]==0): matrix1[m-2][n+1]=i matrix2[m-2][n+1]=m*10+n i+=1 if(top!=-1): putzero() putnext(m-2,n+1) elif((m+2)<8 and (n-1)<8 and (n-1)>=0 and matrix1[m+2][n-1]==0): matrix1[m+2][n-1]=i matrix2[m+2][n-1]=m*10+n i+=1 if(top!=-1): putzero() putnext(m+2,n-1) elif((m+2)<8 and (n+1)<8 and matrix1[m+2][n+1]==0): matrix1[m+2][n+1]=i matrix2[m+2][n+1]=m*10+n i+=1 if(top!=-1): putzero() putnext(m+2,n+1) else: goback(m,n) def matrics(m,n): global matrix1 global matrix2 for i in range(m): matrix1.append([]) matrix2.append([]) for i in range(m): for j in range(n): matrix1[i].append(0) matrix2[i].append(0) count=0 for row in input: if count==0: list=row.split(",") rows=int(list[0]) cols=int(list[1]) if rows==cols: matrics(rows,cols) else: print " rows and cols are not equal" else: list1=row.split(",") m=int(list1[0]) n=int(list1[1]) count+=1 matrix1[m][n]=1 matrix2[m][n]=1 t=putnext(m,n) if x==1: for j in range(rows): for k in range(cols): output.write(str(matrix1[j][k])+"\t") output.write("\n") in1.close() out1.close()