微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何计算算法完成迷宫所采取的步骤数

如何解决如何计算算法完成迷宫所采取的步骤数

最近,我想出了一个用于迷宫的“左手定则”算法。
但是,我需要打印出完成迷宫算法的步骤总数。
我已经完成了算法。这是用于移动精灵的类(已编辑):

class sprite(turtle.Turtle):                               # Define sprite class
    noOfSteps = 0
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("turtle")                               # Shape of the sprite
        self.color("black")                                # Colour of the sprite
        self.setheading(270)                               # Make sprite face down
        self.penup()                                       # Lift the pen up so it does not leave a trail
        self.speed('slowest')                              # Sets the speed that the sprite moves
        
    def moveDown(self):                                    # Define moveDown
        if (self.heading() == 270):                        # Check turtle facing direction
            x_walls = round(sprite.xcor(),0)               # Turtle x coords
            y_walls = round(sprite.ycor(),0)               # Turtle y coords
            if (x_walls,y_walls) in finish:               # Check if turtle coordinates is the coordinates of the end point
                print("Finished")                          # Print 'Finished' to confirm that the turtle has finished reaching the end point
                endProgram()                               # End the program
                print(str(self.noOfSteps))
            if (x_walls + 24,y_walls) in walls:           # Check to see if they are walls on the left
                if(x_walls,y_walls - 24) not in walls:    # Check to see if path ahead is clear
                    self.noOfSteps += 1
                    self.forward(24)                       # Make turtle move forward by 24 pixels(1 cell)
                else:
                    self.noOfSteps += 1
                    self.right(90)                         # Make turtle turn 90 degrees right
            else:
                self.noOfSteps += 2
                self.left(90)                              # Make turtle turn 90 degrees left
                self.forward(24)                           # Make turtle move forward by 24 pixels(1 cell)

    def moveLeft(self):                                    # Define moveLeft
        if (self.heading() == 0):                          # Check turtle facing direction
            x_walls = round(sprite.xcor(),y_walls) in finish:               # Check if turtle coordinates is the coordinates of the end point
                print("Finished")                          # Print 'Finished' to confirm that the turtle has finished reaching the end point
                endProgram()                               # End the program
                print(str(self.noOfSteps))
            if (x_walls,y_walls + 24) in walls:           # Check to see if they are walls on the left
                if(x_walls + 24,y_walls) not in walls:    # Check to see if path ahead is clear
                    self.noOfSteps += 1
                    self.forward(24)                       # Make turtle move forward by 24 pixels(1 cell)
                else:
                    self.noOfSteps += 1
                    self.right(90)                         # Make turtle turn 90 degrees right
            else:
                self.noOfSteps += 2
                self.left(90)                              # Make turtle turn 90 degrees left
                self.forward(24)                           # Make turtle move forward by 24 pixels(1 cell)

    def moveUp(self):                                      # Define moveUp
        if (self.heading() == 90):                         # Check turtle facing direction
            x_walls = round(sprite.xcor(),y_walls) in finish:               # Check if turtle coordinates is the coordinates of the end point
                print("Finished")                          # Print 'Finished' to confirm that the turtle has finished reaching the end point
                endProgram()                               # End the program
                print(str(self.noOfSteps))
            if (x_walls -24,y_walls ) in walls:           # Check to see if they are walls on the left
                if (x_walls,y_walls + 24) not in walls:   # Check to see if path ahead is clear
                    self.noOfSteps += 1
                    self.forward(24)                       # Make turtle move forward by 24 pixels(1 cell)
                else:
                    self.noOfSteps += 1
                    self.right(90)                         # Make turtle turn 90 degrees right
            else:
                self.noOfSteps += 2
                self.left(90)                              # Make turtle turn 90 degrees left
                self.forward(24)                           # Make turtle move forward by 24 pixels(1 cell)

    def moveRight(self):                                   # Define moveRight
        if (self.heading() == 180):                        # Check turtle facing direction
            x_walls = round(sprite.xcor(),y_walls - 24) in walls:           # Check to see if they are walls on the left
                if (x_walls - 24,y_walls) not in walls:   # Check to see if path ahead is clear
                    self.noOfSteps += 1
                    self.forward(24)                       # Make turtle move forward by 24 pixels(1 cell)
                else:
                    self.noOfSteps += 1
                    self.right(90)                         # Make turtle turn 90 degrees right
            else:
                self.noOfSteps += 2
                self.left(90)                              # Make turtle turn 90 degrees left
                self.forward(24)                           # Make turtle move forward by 24 pixels(1 cell)

这是激活课程的主程序:

# Main program
title = title()

building = Building()  # Enable the building class
road = Road()          # Enable the road class
start = Start()        # Enable the start class
end = End()            # Enable the end class
sprite = sprite()      # Enable the sprite class

walls = []             # Create walls coordinate list
finish = []            # Create finish list

setupMaze(mazeSet)     # Call the setup maze function

# Activation of the movement
while True:
    sprite.moveDown()
    sprite.moveLeft()
    sprite.moveUp()
    sprite.moveRight()

    time.sleep(0.02)

我的输出是:

Finished
(Supposed to print out no. of steps here)

有人知道如何解决此问题吗?对不起,如果它有点冗长。我尝试了尽可能地缩短它。

解决方法

您必须通过char访问float

noOfSteps

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。