Advertisement
santiagol26

recorrer en espiral matriz sentido antihorario

May 1st, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. matriz=[[0,4,0,0,5],[1,0,5,0,0],[0,-9,8,0,1],[1,0,1,0,-3],[0,4,0,0,0]]
  2. nume=[]
  3. m,a,b=1,0,0
  4. pos_ = matriz[a][b]
  5. print("",matriz[0],"\n",matriz[1],"\n",matriz[2],"\n",matriz[3],"\n",matriz[4])
  6. while len(nume)<len(matriz)*len(matriz[0])-1:
  7.     while m==1: #bajar
  8.         try:
  9.             if  matriz[a+1][b]!=555:
  10.                 nume.append(pos_)
  11.                 matriz[a][b]=555
  12.                 a+=1
  13.                 pos_= matriz[a][b]
  14.             else:
  15.                 m+=1
  16.         except:
  17.             m+=1
  18.     while m==2: #derecha
  19.         try:
  20.             if matriz[a][b+1]!=555:
  21.                 nume.append(pos_)
  22.                 matriz[a][b]=555
  23.                 b+=1
  24.                 pos_= matriz[a][b]
  25.             else:
  26.                 m+=1
  27.         except:
  28.             m+=1    
  29.     while m==3:  #subir
  30.         if  a>0 and matriz[a-1][b]!=555:
  31.             nume.append(pos_)
  32.             matriz[a][b]=555
  33.             a-=1
  34.             pos_= matriz[a][b]
  35.         else:
  36.             m+=1        
  37.     while m==4:  #izquierda
  38.         if  b>0 and matriz[a][b-1]!=555:
  39.             nume.append(pos_)
  40.             matriz[a][b]=555
  41.             b-=1
  42.             pos_= matriz[a][b]
  43.         else:
  44.             m+=1
  45.     if m==5:
  46.         m=1
  47. nume.append(pos_)
  48. print(nume)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement