Advertisement
Guest User

Spiral_Square_matrix

a guest
Jun 23rd, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.22 KB | None | 0 0
  1. #!/usr/bin/python
  2. """(C)24th June 2013, WhiZTiM
  3.   contact: ionogu@acm.org
  4.         twitter.com/ionogu
  5.         facebook.com/onogu
  6.   blog.whiztim.com
  7.   PREAMBLE:
  8.    this program transverse a square grid in spiral form... and its design is:
  9.    nextMove()
  10.     this function is the traveller... and it attempts to move up, right, down and left the grid
  11.     it returns the successful move.
  12. """
  13.  
  14. def nextMove(x, y, state_grid, lst = 0):
  15.     """ @param:x and @param:y
  16.         This represents an (x,y) coordinate
  17.     @param:state_grid:
  18.         this is a mutuable grid of boolean values to indicate whether we have
  19.         travelled to a coordinate, if not... we go according to @param:lst and
  20.         set that coordinates travel-able value to False
  21.     @param:lst:
  22.         this helps to bring spiral transversal form to life....
  23.         Its values ranges from 0 to 3 and is used to make direction preference
  24.    """
  25.    
  26.     limit = len(state_grid[0])      #dimension of the square array
  27.    
  28.     """ 'first' is a preferencial determiner, i.e
  29.     if the while loop executes once without success, preference is reset to 0
  30.     and if there is still no success, it will break by the else clause
  31.    """
  32.     first = True
  33.    
  34.     while(True):
  35.     if(x-1 >= 0 and lst < 1):       #1st preference: move left
  36.         if(state_grid[x-1][y]):     #if its visit-able
  37.         state_grid[x-1][y] = False  #then make it unvisit-able cause we are going to visit it
  38.         lst = 0             #set preference to this direction
  39.         return (True, (x-1), y, lst)    #return turple
  40.        
  41.     if(y+1 < limit and lst < 2):        #2nd preference: move up
  42.         if(state_grid[x][y+1]):     #similar as above
  43.         state_grid[x][y+1] = False
  44.         lst = 1
  45.         return (True, x, (y+1), lst)
  46.        
  47.     if(x+1 < limit and lst < 3):        #3rd preference: move right
  48.         if(state_grid[x+1][y]):     #similar as above
  49.         state_grid[x+1][y] = False
  50.         lst = 2
  51.         return (True, (x+1), y, lst)
  52.        
  53.     if(y-1 >= 0 and lst < 4):       #4th preference: move down
  54.         if(state_grid[x][y-1]):     #similar as above
  55.         state_grid[x][y-1] = False
  56.         lst = 3
  57.         return (True, x, (y-1), lst)
  58.        
  59.     if(first):      #we will be here if we have tried preferences without success
  60.         lst = 0     #lower or reset preference
  61.         first = False   #dont execute this block again
  62.     else: break     #free to execute the next one
  63.    
  64.     return (False, x, y, lst)   #Finally failed! stop!!!
  65.  
  66. def readinput():
  67.     """ Read the space separated n * n elements from stdin
  68.     Uses first line to automatically determine the size of the array
  69.    """
  70.     first = True        #tells us whether we are reading the first line
  71.     n = 999999999L  #number of lines, we assume the grids will never reach the square of this
  72.     rtn = []        #return value. a 2D array(list)
  73.     while(True):
  74.     if(n == 0): break   #if the last line has been read, break
  75.     w = raw_input()     #get raw_input() from stdin
  76.     if(first):      #if first line
  77.         n = len(w.split())  #use the number of items to automatically determine
  78.                     #the number of lines to be read
  79.         first = False   #we are no more in the first line
  80.        
  81.     rtn.append(w.split())   #split the whitespace delimited elements in the line and append to return variable
  82.     n -= 1          #we now need to read fewer lines
  83.    
  84.     return rtn
  85.  
  86. def main():
  87.     """ read input;
  88.     setup variables for calling on the traveller
  89.     ...till the traveller returns false
  90.    """
  91.     array = readinput()
  92.     # 't' represents the dimension of the array
  93.     t = len(array[0])
  94.    
  95.     # state_grid represents a 2D array(list) of boolean values all initialized to True
  96.     # and its used for indicating whether a coordinate is visitable or not
  97.     state_grid = [[True for i in range(t)] for i in range(t)]
  98.    
  99.     rtn = []    #holds return value
  100.     x = 0   # 'x' or 'i'th dimension
  101.     y = 0   # 'y' or 'j'th dimension
  102.     lst = 0 #variable to indicate last direction of travel
  103.    
  104.     #we append the first item to our return, and set it's visit-able state_grid to False
  105.     rtn.append(array[x][y])
  106.     state_grid[x][y] = False
  107.    
  108.     while(True):
  109.     #next move returns a turple of types(bool, int, int, int)
  110.     c, x, y, lst = nextMove(x, y, state_grid, lst)
  111.    
  112.     if not c: #test if there is no more move
  113.         break
  114.     #append results
  115.     rtn.append(array[x][y])
  116.    
  117.     #print answer
  118.     print rtn
  119.  
  120. if(__name__ == "__main__"):
  121.     main()
  122.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement