Question

Problem 5.2 [4 POINTS): Suppose you are writing a program to make a turtle walk through a maze. In order for the turtle to ma

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This is an example of rat in a maze problem. This is solved by backtracking using the data structure stack. The assumption used is the turtle starts from the coordinates (0,0). A structure node is used which will have members, the x coordinate of the node, the y coordinate of the node, and a variable dir which will keep track of which directions we have tried and which direction to choose next. We will be trying all the directions in anti-clockwise starting from the direction up. Initially dir is assigned 0

  • If dir = 0, try up.
  • If dir=1 try left.
  • If dir=2 try down
  • If dir=3 try right

Else retract back to the previous block of the maze.

Initially, a node with indexes i=0, j=0 and dir=0 is pushed into the stack. Then, we move to all the direction of the topmost node one by one in an anti-clockwise manner and each time as we try out a new path that node is pushed in the stack. The dir variable of the topmost node will be increased each time so that we can try a new direction each time unless all the directions are explored ie. dir=4. If dir equals to 4 we will pop that node from the stack that means we are backtracking one step to the path where we came from. This trial and error method requires pushing and popping operation so that we can maintain the right path in the stack contents. The stack data structure allows us to do the desired operations hence it is used here.

Hope this helped. Please do upvote and if there are any queries please ask in comments section.

Add a comment
Know the answer?
Add Answer to:
Problem 5.2 [4 POINTS): Suppose you are writing a program to make a turtle walk through...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Writing the code in IDLE. Make sure the code is working. thank you so much Create the following turtle pattern. Using the turtle and random modules, write a program that creates an output as...

    Writing the code in IDLE. Make sure the code is working. thank you so much Create the following turtle pattern. Using the turtle and random modules, write a program that creates an output as the one shown in the picture below. Consider the following: . Change the color of the window to "red" using wn.bgcolor property of the screen variable you create. . Your turtle variable has to have a pen color "white", you have to use the .pencolor property...

  • Suppose you are writing software for a radio station that manages its playlist. The program will...

    Suppose you are writing software for a radio station that manages its playlist. The program will generate candidate playlists from record library automatically and station personnel can then check and modify them. Disc jockeys must also be able to change playlists when they are used because what is actually played is often different from what is planned. The playlist are then used to generate reports for paying royalties. You must decide what sort of data structure to use to store...

  • Suppose you are writing software for a radio station that manages its playlists. The program will...

    Suppose you are writing software for a radio station that manages its playlists. The program will generate candidate playlists from a record library automatically and station personnel can then check and modify them. Disc jockeys must also be able to change playlists when they are used because what is actually played is often different from what is planned. The playlists are then used to generate reports for paying royalties. You must decide what sort of data structure to use to...

  • If your program does not compile, you will not receive any points! You will write a program to keep up with a Jewelry...

    If your program does not compile, you will not receive any points! You will write a program to keep up with a Jewelry store and some of the Inventory (rings/necklaces/earrings) purchased there (The Jewelry store can be real or not) using Object-Oriented Programming (OOP). The important aspect of object-oriented programming is modular development and testing of reusable software modules. You love selling rings, necklaces, and earrings as well as programming and have decided to open a Jewelry store. To save...

  • I am writing a Python program to serve as a fitness tracker. I would love some...

    I am writing a Python program to serve as a fitness tracker. I would love some input as to how you would code this. I've included what I want this program to accomplish, what I will keep track of, and below all of that I have posted the assignment guidelines. Any suggestions on how to make this better/ more efficient would be welcome! I'm thinking of making a weekly tracker that takes input per day, and outputs at the end...

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words as...

  • This project is meant to give you experience writing linked lists and graphs. As such, you...

    This project is meant to give you experience writing linked lists and graphs. As such, you are not permitted to use arrays or any data structure library. You may, however, make use of code presented in class and posted to Blackboard. Objective Your goal for this project is to take a block of text, analyze it, and produce random sentences in the style of the original text. For example, your program, given Wizard of Oz, might produce: how quite lion...

  • Write a program in java. You are tasked with writing an application that will keep track...

    Write a program in java. You are tasked with writing an application that will keep track of Insurance Policies. Create a Policy class called InsurancePolicies.java that will model an insurance policy for a single person. Use the following guidelines to create the Policy class: • An insurance policy has the following attributes: o Policy Number o Provider Name o Policyholder’s First Name o Policyholder’s Last Name o Policyholder’s Age o Policyholder’s Smoking Status (will be “smoker” or “non-smoker”) o Policyholder’s...

  • Project 1. Dog Door You are asked to create a dog door for a client. You...

    Project 1. Dog Door You are asked to create a dog door for a client. You are programming the remote that will do things such as open and close, etc. You must create both the program and write a white paper explaining your design • It should open (saying "The dog door is open.") and close (saying "The dog door is closed.). • It should take into account a dog going outside and coming back in; it should open when...

  • List Python

    A car park management system uses a list to represent which spaces in a car park are currently occupied and which are currently unoccupied. An empty space is represented by a 0 and a full space by a 1. Then the state of the car park would be represented by the following list: [1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0] The designers of the car park management system require a program that will accept a list such as the one above (although of course the length will not always be 11) and calculate what percentage of the spaces are currently occupied, rounded to the nearest percent. In the example above 7 spaces are occupied and there are 11 altogether, so the percentage occupied is: (7 / 11) x 100 = 63.63… = 64% to the nearest whole number. i.What are the admissible inputs of the problem? ii.What is the output of this problem? iii.Write three tests for this problem. The inputs should be different from the example given in the scenario description above. At least one of your tests must be a borderline case. Present the tests in a table, with a column for each input and output, and an extra column with a brief explanation of why you selected each test iv.Decompose the problem into sub-problems. Use the > notation and state in brackets the type of the problem and of each sub-problem v. Choose the patterns for the subproblem types you identified, instantiate the patterns into an algorithm, and translate the algorithm into code.This part of the question involves writing Python code. Write your code in this file and then submit the completed code file as part of the answer to this part of the question. Note: Although Python has a built-in function to find the sum of a list, please do not use it to compute how many 1s there are in the list. We want you to write your own code , because the purpose of this question is to practice following the problem solving approach described there, which will equip you to solve problems in other cases for which there is no built-in Python function. A Python program which is not preceded by a corresponding decomposition will gain at most half marks. Again, the reason for this is that we want you to follow a systematic problem-solving approach and not dive straight into code. Please therefore make sure that you have supplied an answer to iv. If you find it difficult to get the decomposition quite right at first there is no reason why you cannot review it when you have written the code. The main thing is to have a clear idea of what the solution needs to do (the decomposition) that ties in correctly with how it does it (the program). Make sure you use appropriate variable names and comments and that you have tested your code with the inputs of your test table. (Keep each of your test inputs in your code, but precede all of the test inputs, except for one, with the # symbol. Recall that # signals a comment, which is ignored by the Python interpreter.)

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT