code: def laser_mirrors(rows, cols, mir):
      
      # build structures
      n = len(mir)
      orien = [None] * (n + 2)
      orien[n] = 0      # arbitrary orientations
      orien[n + 1] = 0
      succ = [[None for direc in range(4)] for i in range(n + 2)]
      L = [(mir[i][0], mir[i][1], i) for i in range(n)]
      L.append((0, -1, n))                  # enter
      L.append((0, cols, n + 1))            # exit
      last_r, last_i = None, None
      for (r, c, i) in sorted(L):           # sweep by row
          if last_r == r:
              succ[i][LEFT] = last_i
              succ[last_i][RIGHT] = i
          last_r, last_i = r, i
      last_c = None
      for (r, c, i) in sorted(L, key=lambda rci: (rci[1], rci[0])):
          if last_c == c:                   # sweep by column
              succ[i][UP] = last_i
              succ[last_i][DOWN] = i
          last_c, last_i = c, i
      if solve(succ, orien, n, RIGHT):      # exploration
          return orien[:n]
      else:
          return None
docs: Orienting mirrors to allow reachability by laser beam
  
      :param int rows:
      :param int cols: rows and cols are the dimension of the grid
      :param mir: list of mirror coordinates, except
                  mir[0]= laser entrance,
                  mir[-1]= laser exit.
      :complexity: :math:`O(2^n)`