Navigating the robot by finding the shortest way I have this task where I have to make my robot find the shortest way when I enter a destination point. I've created some functions to assign numbers (distances) for each square and counts its way back to my robot by deleting the other options. Then the robot should only follow the numbers. Here's also a screenshot of the map it navigates in:
<img src="https://i.stack.imgur.com/ZzM52.jpg" alt="Here's also a screenshot of the map it navigates in:
" />
My code works so far, however I think I should be able to reach the same outcome by using less for loops and with a more efficient writing. I believe if I see different ways of thinking in my earlier stages, I can have a broader perspective in the future. So, any ideas on how to reach my goal with a shorter code?
#Your Code Starts Here
&quot;&quot;&quot;
for x in range(0, map.width):
    for y in range(0, map.height): 
        if map.blocked(x, y):
            map.value[x, y] = -1 
        else:
            map.value[x, y] = 0
&quot;&quot;&quot;
def fill_around(n,m,number):
    for x in range (n-1,n+2):
        for y in range (m-1,m+2):
            if map.value[x,y] != 0:
                pass
            elif x==n+1 and y==m+1 or x==n-1 and y==m-1 or x==n-1 and y==m+1 or x==n+1 and y==m-1:
                pass
            elif map.blocked(x,y) == True:
                map.value[x,y]= -1            
            elif x==n and y==m:
                map.value[x,y]= number
            else:
                map.value[x,y]= number+1


def till_final(final_x,final_y):
    final_x=9 
    final_y=1
    fill_around(1,1,1)
    for p in range(2,17):
        for k in range(0, map.width):
            for l in range(0, map.height):
                if map.value[final_x,final_y] ==0 and map.value[k,l]==p:
                    fill_around(k,l,p)

def delete_duplicates(final_x,final_y):
    for k in range(0, map.width):
        for l in range(0, map.height):
            if map.value[k,l] == map.value[final_x,final_y] and k != final_x and l != final_y:
                map.value[k,l] = 0
if structure
        if map.value[x,y] != 0:
            pass
        elif x==n+1 and y==m+1 or x==n-1 and y==m-1 or x==n-1 and y==m+1 or x==n+1 and y==m-1:
            pass
        elif map.blocked(x,y) == True:
            map.value[x,y]= -1            
        elif x==n and y==m:
            map.value[x,y]= number
        else:
            map.value[x,y]= number+1

is a little awkward. Rephrase if; pass into if not, delete == True since it's redundant, and you have:
if map.value[x, y] == 0 and not (
    (x==n+1 or x==n-1) and 
    (y==m+1 or y==m-1)
): 
    if map.blocked(x, y):
        map.value[x, y] = -1            
    elif x == n and y == m:
        map.value[x, y] = number
    else:
        map.value[x, y] = number + 1

You can further simplify by checking for coordinate difference in a set membership operation, though I haven't tested its performance:
if map.value[x, y] == 0 and not (
    {x - n, y - m} &lt;= {-1, 1}
): 
    # ...

General
You have heavily nested loops that iterate over a matrix-like structure. That's a job for numpy. Vectorization may or may not make your code shorter, but it will definitely speed it up.