Question: [ Python ] writing the program Demon: The floor of hell can be seen as a 2-dimensional grid, as shown below. From the top left

[Python] writing the program

Demon: The floor of hell can be seen as a 2-dimensional grid, as shown below. From the top left corner, the floor continues indefinitely to the east and indefinitely to the south. There are infinitely-long walls running along the northern border and the western border.

[Python] writing the program Demon: The floor of hell can be seen

Jack: Whoa, you didnt tell me the floor is infinite.

Demon: Im telling you. The robot will initially be placed in the cell in the top left corner, facing the east. The robot can be programmed to travel from one cell to another. It has a sensor in the front which detects whether there is a wall immediately ahead. The robot accepts the following commands:

turnLeft(): Turn to the left of itself by 90 degrees

turnRight(): Turn to the right of itself by 90 degrees

moveOne(): Move to the next cell ahead

There is also a special read-only variable, named wall_ahead, which will have the value 1 if the sensor detects a wall immediately ahead, and the value 0 otherwise. Obviously, if wall_ahead is 1, you must not command the robot to move ahead, or else it would crash into the wall.

Jack: Can I use all constructs and built-in functions in Python?

Demon: Yes. The robot supports Python 3.5, but with two exceptions. First, due to the limited memory, you may declare up to 10 variables only. Each variable can only store an integer-valued between -2 billion to 2 billion. Secondly, also due to the limited memory, you cannot use recursion.

Jack: But space is infinite. It will not be possible to clean all the cells in a finite amount time.

Demon: Thats correct. I only require that every cell in the grid will eventually be cleaned after some finite amount of time.

Jack: Does it mean the program will run indefinitely without terminating?

Demon: Yes. Thats what the program is supposed to do. But if the program works correctly, for every cell in the grid, if I wait long enough (a finite amount of time, could be 10 minutes, 10 years, 100 years, or longer), that robot will eventually reach that cell.

The demon then gave Jack the robot and a computer to work on. Still being a little puzzled but having no other choice, Jack then started writing the program .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!