Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6638203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:24:28+00:00 2026-05-25T23:24:28+00:00

I am trying to implement the A* algorithm in python but have hit a

  • 0

I am trying to implement the A* algorithm in python but have hit a problem when trying to find the path of this map:

X X X X X X X     S = Start
0 0 0 X 0 0 0     E = End
0 S 0 X 0 E 0     X = Wall
0 0 0 X 0 0 0
0 0 0 0 0 0 0

I am using the Manhattan method. My implementation does find a path, but not the shortest one. The error starts on it’s second move — after moving right. At this point it can move up and the heuristic cost would be four (three right, one down) or down (three right, one up). Is there a way so it chooses down to get the shortest path?

Code:

class Node:
    def __init__(self, (x, y), g, h, parent):
        self.x = x
        self.y = y
        self.g = g
        self.h = h
        self.f = g+h
        self.parent = parent

    def __eq__(self, other):
        if other != None:
            return self.x == other.x and self.y == other.y
        return False

    def __lt__(self, other):
        if other != None:
            return self.f < other.f
        return False

    def __gt__(self, other):
        if other != None:
            return self.f > other.f
        return True

    def __str__(self):
        return "(" + str(self.x) + "," + str(self.y) + ") " + str(self.f)

def find_path(start, end, open_list, closed_list, map, no_diag=True, i=1):
    closed_list.append(start)
    if start == end or start == None:
         return closed_list
     new_open_list = []
     for x, y in [(-1,1),(-1,-1),(1,-1),(1,1),(0,-1),(0,1),(-1,0),(1,0)]:
        full_x = start.x + x
        full_y = start.y + y
        g = 0
        if x != 0 and y != 0:
            if no_diag:
                continue
            g = 14
        else:
            g = 10
        h = 10 * (abs(full_x - end.x) + abs(full_y - end.y))
        n = Node((full_x,full_y),g,h,start)
        if 0 <= full_y < len(map) and 0 <= full_x < len(map[0]) and map[full_y][full_x] != 1 and n not in closed_list:
            if n in open_list:
                if open_list[open_list.index(n)].g > n.g:
                    new_open_list.append(n)
                else:
                    new_open_list.append(open_list[open_list.index(n)])
            else:
                new_open_list.append(n)
    if new_open_list == None or len(new_open_list) == 0:
        return find_path(start.parent, end, open_list, closed_list, map, no_diag, i-1)
    new_open_list.sort()
    return find_path(new_open_list[0], end, new_open_list, closed_list, map, no_diag)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:24:28+00:00Added an answer on May 25, 2026 at 11:24 pm

    You seem to be constructing a new open list for each node, which contains only that node’s neighbors. This essentially makes your search a form of a depth-first search, while A* should be a best-first search.

    You need to use one open list which will be updated with each node’s neighbors as you visit that node. The old nodes in the open list must remain there until they are traversed and moved to the closed list.

    Regarding what you said in your question, It’s OK for the search to try moving up before down (since according to the heuristic, they are the same distance from the goal). What matters is that in the end, the path chosen will be the shortest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement an efficient algorithm for nearest-neighbour search problem. I have
I am trying to implement the GJK-algorithm but I got stuck instantly. The problem
I have this weird kind of error. I am trying implement basic Euclidean algorithm
I am trying to implement porter stemming algorithm but i am stuck at this
I am trying to implement porter stemming algorithm, but I stumbled at this point
I'm trying to implement this algorithm description from a previous question I had here
i'm trying to implement an algorithm which is flood-fill alike. the problem is that
I am trying to implement the algorithm explained on this paper, used to traverse
I'm trying to implement this algorithm: http://www.cs.cmu.edu/afs/cs/academic/class/15451-s06/www/lectures/scrabble.pdf but I just cannot figure out what
I'm trying to implement a Quicksort algorithm, I have read how to do it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.