예제 #1
0
    def _color(self):
        print("height %d width %d" % (self.height, self.width))
        #self._print()

        self.img = self.img.convert('RGB')
        impix = self.img.load()

        dest = [self.height - 1, -1]
        source = [0, -1]
        for i in range(0, self.width):
            if (self.data[i] > 0):
                source[1] = i
                break
        for i in range(1, self.width):
            if (self.data[(self.width * self.height) - i] > 0):
                dest[1] = self.width - i
                print(self.height * self.width - i)
                break

        print("source node: ", source)
        print("dest node: ", dest)
        pix = []
        func = BFS(source, dest, self.width, self.height)
        pix = func.bfs()
        pix.reverse()

        for i in pix:
            row = i[0]
            col = i[1]
            #print(impix[col ,row])
            impix[col, row] = (0, 0, 204)

        self.img.save(self.out)
예제 #2
0
class TestBFS(unittest.TestCase):
    def setUp(self):
        self.bfs = BFS()
        self.graph = Graph()
        self.graph.insert_edge(create_edge(0, 1, 10))
        self.graph.insert_edge(create_edge(1, 3, 10))
        self.graph.insert_edge(create_edge(0, 2, 20))
        self.graph.insert_edge(create_edge(0, 3, 30))
        self.graph.insert_edge(create_edge(2, 3, 60))
        self.graph.insert_edge(create_edge(3, 4, 120))

    def test_bfs(self):
        result = self.bfs.bfs(0, self.graph)
        expected = [0, 1, 2, 3, 4]
        self.assertEqual(expected, result)
예제 #3
0
 def print_tree(self):
     self.assign_root()
     bfs = BFS()
     bfs.bfs([self.root])
     print ''
예제 #4
0
 def print_tree(self):
     self.assign_root()
     bfs = BFS()
     bfs.bfs([self.root])
     print ''
예제 #5
0
def main(win, width):
    ROWS = 40
    grid = make_grid(ROWS, width)

    start = None
    end = None

    run = True
    started = False

    while run:
        draw(win, grid, ROWS, width)
        for event in pygame.event.get():

            # Close the window
            if event.type == pygame.QUIT:
                run = False

            if started:
                continue
            # Left Mouse Click
            if pygame.mouse.get_pressed()[0]:
                pos = pygame.mouse.get_pos()
                row, col = get_clicked_pos(pos, ROWS, width)
                spot = grid[row][col]
                if not start and spot != end:
                    start = spot
                    start.make_start()
                elif not end and spot != start:
                    end = spot
                    end.make_end()
                elif spot != end and spot != start:
                    spot.make_barrier()
            elif pygame.mouse.get_pressed()[2]:
                pos = pygame.mouse.get_pos()
                row, col = get_clicked_pos(pos, ROWS, width)
                spot = grid[row][col]
                spot.reset()
                if spot == start:
                    start = None
                elif spot == end:
                    end = None

            if event.type == pygame.KEYDOWN:
                if start and end:
                    started = True
                    for row in grid:
                        for spot in row:
                            spot.update_neighbors(grid)
                    if event.key == pygame.K_a:
                        pygame.display.set_caption(
                            'DFS Path Finding algorithm')
                        DFS.dfs(lambda: draw(win, grid, ROWS, width), grid,
                                start, end)
                    elif event.key == pygame.K_s:
                        pygame.display.set_caption(
                            'BFS Path Finding algorithm')
                        BFS.bfs(lambda: draw(win, grid, ROWS, width), grid,
                                start, end)
                    elif event.key == pygame.K_d:
                        pygame.display.set_caption('A* Path Finding algorithm')
                        ASTAR.astar(lambda: draw(win, grid, ROWS, width), grid,
                                    start, end)
                    started = False
                if event.key == pygame.K_c:
                    start = None
                    end = None
                    grid = make_grid(ROWS, width)

    pygame.quit()