简体   繁体   中英

Finding the local minima with a robot in python

So I have been given this question and I have spent hours trying to solve it but I can't seem to get it right.

Here's the question:

In the number grid below, we will define a local minima as a square with the smallest number out of its four surrounding squares (above, below, to the left and to the right) that are not obstacles.

Program the robot to find and paint a square that is a local minima.

Functions: turnLeft(), turnRight(), forward(), paint(), squareNumber(), obstacleAhead().

paint() will paint the square beneath the robot, squareNumber() checks the number of the square beneath the robot, and obstacleAhead() checks if there is a square in front of the robot.

Also, using lists or defining new functions is not allowed.

The program is tested with multiple grids so I need to make a solution that works for any grid.

I am not sure how to make the robot move in a way that it checks all the squares but also doesn't get stuck. Any help would be appreciated!

Picture of a grid

You think you need to try every square, but you don't.

If a square isn't a local minimum, that means there's an adjacent square with a lower number. If that square isn't a local minimum, there's an adjacent square with a lower number. If that square isn't a local minimum, etc.

If you follow a path of decreasing numbers, that path can't go on forever, because the grid is finite. Eventually, the path has to end, on a local minimum.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM