|
| 1 | +# distutils: language=c++ |
| 2 | +import numpy as np |
| 3 | +cimport numpy as np |
| 4 | +import cython |
| 5 | +from libcpp cimport bool |
| 6 | +from libcpp.queue cimport queue |
| 7 | +from libcpp.vector cimport vector |
| 8 | +from libcpp.pair cimport pair |
| 9 | +from libcpp.map cimport map |
| 10 | + |
| 11 | +cdef struct coords: |
| 12 | + int x |
| 13 | + int y |
| 14 | + |
| 15 | +cdef struct qitem: |
| 16 | + int x |
| 17 | + int y |
| 18 | + int distance |
| 19 | + |
| 20 | + |
| 21 | +@cython.boundscheck(False) |
| 22 | +@cython.wraparound(False) |
| 23 | +cdef bool directions2reachable(np.ndarray[np.int8_t, ndim=2] directions, int w, int h): |
| 24 | + with cython.nogil: |
| 25 | + for x in range(w): |
| 26 | + for y in range(h): |
| 27 | + with cython.gil: |
| 28 | + if directions[x, y] == ord(b' '): |
| 29 | + return False |
| 30 | + return True |
| 31 | + |
| 32 | + |
| 33 | +@cython.boundscheck(False) |
| 34 | +@cython.wraparound(False) |
| 35 | +cpdef flood(np.ndarray[np.int8_t, ndim=2] maze, int w, int h): |
| 36 | + cdef int x, y, i |
| 37 | + cdef queue[qitem] q |
| 38 | + cdef np.ndarray[np.int32_t, ndim=2] distances |
| 39 | + cdef np.ndarray[np.int8_t, ndim=2] directions |
| 40 | + distances = np.full((w, h), -1, dtype='int32') |
| 41 | + directions = np.full((w, h), b' ', dtype=('a', 1)) |
| 42 | + |
| 43 | + with cython.nogil: |
| 44 | + for x in range(w): |
| 45 | + for y in range(h): |
| 46 | + with cython.gil: |
| 47 | + if maze[x, y] < 0: |
| 48 | + directions[x, y] = b'#' |
| 49 | + elif maze[x, y] == 1: |
| 50 | + q.push(qitem(x, y, 0)) |
| 51 | + distances[x, y] = 0 |
| 52 | + directions[x, y] = b'X' |
| 53 | + |
| 54 | + cdef coords *dir_offsets = [coords(1, 0), coords(-1, 0), coords(0, 1), coords(0, -1)] |
| 55 | + cdef np.int8_t *dir_chars = [b'^', b'v', b'<', b'>'] |
| 56 | + while not q.empty(): |
| 57 | + item = q.front() |
| 58 | + q.pop() |
| 59 | + for i in range(4): |
| 60 | + offset = dir_offsets[i] |
| 61 | + x = item.x + offset.x |
| 62 | + y = item.y + offset.y |
| 63 | + if 0 <= x < w and 0 <= y < h: |
| 64 | + if maze[x, y] >= 0 and distances[x, y] == -1: |
| 65 | + distances[x, y] = item.distance+1 |
| 66 | + directions[x, y] = dir_chars[i] |
| 67 | + q.push(qitem(x, y, item.distance+1)) |
| 68 | + |
| 69 | + return distances, directions, directions2reachable(directions, w, h) |
| 70 | + |
| 71 | + |
| 72 | +@cython.boundscheck(False) |
| 73 | +@cython.wraparound(False) |
| 74 | +cpdef build_path(np.ndarray[np.int8_t, ndim=2] directions, int row, int column): |
| 75 | + if directions[row, column] == b'#' or directions[row, column] == b' ': |
| 76 | + raise NoPathExistsException |
| 77 | + cdef vector[pair[int,int]] path |
| 78 | + cdef map[char,coords] dirs |
| 79 | + dirs.insert(pair[char,coords](b'v', coords(1, 0))) |
| 80 | + dirs.insert(pair[char,coords](b'^', coords(-1, 0))) |
| 81 | + dirs.insert(pair[char,coords](b'>', coords(0, 1))) |
| 82 | + dirs.insert(pair[char,coords](b'<', coords(0, -1))) |
| 83 | + path.push_back(pair[int,int](row, column)) |
| 84 | + while directions[row, column] != b'X': |
| 85 | + d = dirs[directions[row, column]] |
| 86 | + row += d.x |
| 87 | + column += d.y |
| 88 | + path.push_back(pair[int,int](row, column)) |
| 89 | + return path |
| 90 | + |
| 91 | + |
| 92 | +cdef class NoPathExistsException(Exception): |
| 93 | + pass |
| 94 | + |
| 95 | + |
| 96 | +class MazeAnalysis: |
| 97 | + |
| 98 | + def __init__(self, maze): |
| 99 | + maze = np.atleast_2d(maze.astype('int8')) # fix matrix type & dims |
| 100 | + self.distances, self.directions, self.is_reachable = flood(maze, *maze.shape) |
| 101 | + |
| 102 | + def path(self, row, column): |
| 103 | + return build_path(self.directions, row, column) |
| 104 | + |
| 105 | + |
| 106 | +cpdef analyze(maze): |
| 107 | + return MazeAnalysis(maze) |
0 commit comments