Optimized Prim's algorithm, to prevent continuous looping when creating maze