首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >广度优先搜索算法Python太慢

广度优先搜索算法Python太慢
EN

Stack Overflow用户
提问于 2020-02-24 22:18:45
回答 1查看 107关注 0票数 0

我试着给一个人工智能编程。我为广度优先搜索的路径搜索做了这个脚本,但它似乎很慢。我不知道如何让它更快。请帮帮我。顺便说一句,如果你需要,我用了这个教程的https://www.youtube.com/watch?v=hettiSrJjM4

代码语言:javascript
复制
import queue


class AIRoadMap(object):
    def __init__(self, game):
        self.game = game
        self.road_list = []
        self.initialize_road_list()

    def initialize_road_list(self):
        # This method adds all empty coordinates to list (self.road_list)
        for y_row in self.game.map:
            y_coord = self.game.map.index(y_row)
            for x in y_row:
                if x['symbol'] == '.':
                    x_coord = y_row.index(x)
                    self.road_list.append({'x' : x_coord, 'y' : y_coord})

    def check_valid_moves(self, x_from, y_from, moves):
        unreal_x = x_from
        unreal_y = y_from
        valid = False
        for move in moves:
            if move == "L":
                unreal_x -= 1

            elif move == "R":
                unreal_x += 1

            elif move == "U":
                unreal_y -= 1

            elif move == "D":
                unreal_y += 1
            valid = False
            for item in self.road_list:
                if item['x'] == unreal_x and item['y'] == unreal_y:
                    valid = True
            if valid is False:
                break
        return valid

    def IsPathEnd(self, x_from, y_from, to_x, to_y, moves):
        unreal_x = x_from
        unreal_y = y_from
        for move in moves:
            if move == "L":
                unreal_x -= 1

            elif move == "R":
                unreal_x += 1

            elif move == "U":
                unreal_y -= 1

            elif move == "D":
                unreal_y += 1

        if unreal_y == to_y and unreal_x == to_x:
            return True
        if unreal_x != to_x or unreal_y != to_y:
            return False
        if unreal_x != to_x and unreal_y != to_y:
            return False

    def generate_road(self, from_x, to_x, from_y, to_y):
        nums = queue.Queue()
        nums.put("")
        add = ""

        while not self.IsPathEnd(from_x, from_y, to_x, to_y, add):
            add = nums.get()
            # print(add)
            for j in ["L", "R", "U", "D"]:
                put = add + j
                if self.check_valid_moves(from_x, from_y, put):
                    if len(put) < 3:
                        nums.put(put)
                    else:
                        if put[-1] == "L" and put[-2] != "R" or put[-1] == "R" and put[-2] != "L" or put[-1] == "U" and \
                                put[-2] != "D" or put[-1] == "D" and put[-2] != "U":
                            nums.put(put)
                    #nums.put(put)

        return add

方法initialize_road_list()将所有空坐标从self.game.map添加到self.road_list,这基本上是一个网格

EN

回答 1

Stack Overflow用户

发布于 2020-02-24 22:51:17

你的代码似乎有很多的改进,我现在只建议几个:

1)替换:

代码语言:javascript
复制
        valid = False
        for item in self.road_list:
            if item['x'] == unreal_x and item['y'] == unreal_y:
                valid = True
        if valid is False:
            break
    return valid

使用

代码语言:javascript
复制
       if not any(item['x'] == unreal_x and item['y'] == unreal_y for item in self.road_list):
           return False
   return True

2)替换:

代码语言:javascript
复制
    if unreal_y == to_y and unreal_x == to_x:
        return True
    if unreal_x != to_x or unreal_y != to_y:
        return False
    if unreal_x != to_x and unreal_y != to_y:
        return False

通过以下方式:

代码语言:javascript
复制
    return unreal_y == to_y and unreal_x == to_x

3)替换:

代码语言:javascript
复制
if put[-1] == "L" and put[-2] != "R" or put[-1] == "R" and put[-2] != "L" or put[-1] == "U" and \
                                put[-2] != "D" or put[-1] == "D" and put[-2] != "U":

通过以下方式:

代码语言:javascript
复制
if put[-2:] in frozenset(('LR', 'RL', 'UD', 'DU')):
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60377830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档