Computer Science Recursion & Backtracking

LET ME KNOW IF YOU CAN FINISH THIS ON TIME AND DO EXACTLY WHAT THE PROJECT ASKS FOR 

WILLING TO PAY

Haven’t Found The Relevant Content? Hire a Subject Expert to Help You With
Computer Science Recursion & Backtracking
Post Your Own Question And Get A Custom Answer
Hire Writer

Purpose:  We have discussed recursion and in particular backtracking algorithms (such as Eight Queens).  In this assignment you will get some practice at recursive programming by writing a backtracking algorithm to find phrases in a puzzle.

Idea:  “Word search” puzzles are common in newspapers, on restaurant placemats and even in their own collections within books.  These vary in format from puzzle to puzzle, but one common format is as follows:  A user is presented with a 2-dimensional grid of letters and a list of words.  The user must find all of the words from the list within the grid, indicating where they are by drawing ovals around them.  Words may be formed in any direction – up, down, left or right (we will not allow diagonals even though most puzzles do allow them) but all of the letters in a word must be in a straight line.

This idea can be extended to allow phrases to be embedded in the grids.  However, requiring an entire phrase to be in a straight line on the board is not practical, as the dimensions of the board would need to be overly large.  Therefore, we will still require the letters in each word to be in a straight line, but we are allowed to change direction between words.

For example, we might be given the following 10×10 grid of letters:

            a b s t r a c t i j

            a t a d t d a t a j

            t b c d c a g h t j

            y b c d a t g h y j

            p b c d r a g h p j

            e b c d t f l h e j

            s a r e s f o h s j

            a r e d b f o h a j

            r b c d a f c h r j

            e r e a l l y r e j

and the following phrases:

               abstract

               abstract data types

               abstract data types are really cool

               abstract data types are really awesome

Upon searching, assuming the grid starts in the upper left corner with position (0,0), and that the row is the first coordinate:

               abstract would be found in two places:

                               [(0,0) to (0,7)] and

                               [(8,4) to (1,4)]

               abstract data types would also be found in two places:

                               [(8,4) to (1,4)], [(1,5) to (1,8)], [(2,8) to (6,8)] and

                               [(8,4) to (1,4)], [(1,3) to (1,0)], [(2,0) to (6,0)]

               abstract data types are really cool would be found at:

                               [(8,4) to (1,4)], [(1,3) to (1,0)], [(2,0) to (6,0)], [(7,0) to (9,0)], [(9,1) to (9,6)], [(8,6) to (5,6)]

               abstract data types are really awesome would not be found

Details:  Your task is to write a Java program to read in a grid of letters from a file, and then interactively allow a user to enter phrases until he or she wants to quit.  For each phrase your program must output whether or not the phrase is found and, if found, specifically where it is located.

Input Details:

The grid of letters for your program will be stored in a text file formatted as follows:

            Line 1: Two integers, R and C, separated by a single blank space.  These will represent the number of rows, R and columns, C, in the grid.

            Remaining lines: R lines containing C lower case characters each

The user input will be phrases of words, with a single space between each word and no punctuation.  Each phrase will be entered on a single line.  The user may enter either upper or lower case letters, but the string should be converted to lower case before searching the grid.  The program will end when the user enters no data for the current phrase (i.e. hits <Enter> without typing any characters beforehand).

Output Details:

If a phrase is not found in the grid the output should simply state that fact.

If a phrase is found in the grid, your program must find one occurrence of the phrase, and the output must indicate this fact in two ways:

1) Show the coordinates of each word in the phrase as a pair of (row, column) pairs.

2) Show the grid with the letters of the phrase indicated in upper case

For example, for the grid above and the phrase “abstract data types” your output would be:

abstract: (8,4) to (1,4)

data: (1,5) to (1,8)

types: (2,8) to (6,8)

a b s t r a c t i j

a t a d T D A T A j

t b c d C a g h T j

y b c d A t g h Y j

p b c d R a g h P j

e b c d T f l h E j

s a r e S f o h S j

a r e d B f o h a j

r b c d A f c h r j

e r e a l l y r e j

Algorithm Details:

Your search algorithm must be a recursive, backtracking algorithm.  Note that you do not need recursion to match the letters within individual words (although you may do this recursively if you prefer).  Where the recursion is necessary is when moving from one word to the next – since it is here where you may change direction.  To make the program more consistent (and easier to grade), we will have the following requirements for the recursive process:

1) No letter / location on the grid may appear more than one time in any part of a solution.

2) When given a choice of directions, the options must be tried in the following order:  right, down, left, up.  Given this ordering and the grid above, the solution for “abstract data” would be [(8,4) to (1,4)], [(1,5) to (1,8)] rather than [(8,4) to (1,4)], [(1,3) to (1,0)], since the “right” direction is tried before the “left” direction.

3) If the last letter in a word is at location (i, j), the first letter of the next word must be at one of locations (i, j+1), (i+1, j), (i, j-1), or (i-1, j).

4) The direction chosen to find the first letter of a word is the same direction that must be used for all of the letters of the word.  For example, in the grid shown above, for the phrase “abstract data”, the following would NOT be a valid solution: [(8,4) to (1,4)], [(1,5) to (4,5)].  This solution is not legal because we proceeded right from the “T” of “abstract” to find the “D” in “data”, but then proceeded down to find the remaining letters in “data”.  Similarly, also in the grid shown above, for the phrase “abstract data types are”, the following would NOT be a valid solution: [(8,4) to (1,4)], [(1,3) to (1,0)], [(2,0) to (6,0)], [(7,0) to (7,2)].  This solution is not legal because we proceeded down from the “S” of “types” to find the “A” in “are”, but then proceeded right to find the remaining letters in “are”.

The idea is that you are building a solution word by word.   Each time you complete a word, you can look for the “next” word in any of the four directions – this is where the recursion occurs.  If the “next” word cannot be found in any of the directions, you must delete the most recently completed word and backtrack to the previous word.

For example, consider the board above with the search phrase “data types are really”.  The algorithm first tries to find the word “data” starting at position (0,0).  It tries in all four directions and does not succeed.  It proceeds through the other starting positions until it finds “data” in locations [(1,3) to (1,0)].

It now recursively tries to find “types” in the following ways:

            Going right to position (1,1) – this will not work since (1,1) is already being used in “data”

            Going down to position (2, 0) – this will succeed and “types” is found in locations [(2,0) to (6,0)].

The algorithm now recurses again, this time looking for “are” in the following ways:

                        Going right to position (6,1) – this will succeed and “are” is found in locations [(6,1) to (6,3)].

The algorithm now recurses again, this time looking for “really” in the following ways:

                                    Going right to position (6,4) – this will not work since character (6,4) is an “s”

                                    Going down to position (7,3) – this will not work since character (7,3) is a “d”

                                    Going left to position (6,2) – this will not work since (6,2) is already being used in “are”

                                    Going up to position (5,3) – this will not work since character (5,3) is a “d”

                                    Since all directions have been tried, the search for “really” has failed and the algorithm must backtrack.

                                    The word “are” is removed and the previous search for “are” resumes

                        Going down to position (7,0) – this will succeed and “are” is found in locations [(7,0) to (9,0)].

                                    The algorithm now recurses again, this time looking for “really” in the following ways:

                                    Going right to position (9,1) – this will succeed and “really” is found in locations [(9,1) to (9,6)].

                                    The last word in the phrase has been found and the algorithm succeeds

Clearly,  more backtracking may be necessary in other situations.  I recommend tracing through the process with some example phrases before you start coding your solution.

A non-trivial part of this assignment is keeping track of the “path” of the solution and printing the path out once the solution has been found.  You will likely need to use some data structure to store / update this path.  Think carefully how you can do this part of the assignment.                      

Test Files:

I have placed several test files and some sample output online.  See the CS 0445 Assignments page for these files and make sure that your program works correctly for all of the sample files.

Help:

If you are having trouble with recursion and backtracking, the program FindWord.java may be of help to you.  This program finds individual words in a grid of characters using recursion and backtracking.  It recurses on individual characters rather than on words, but the recursive process is similar in both programs.  See also test file findWord1.txt.   Important Note: The code from FindWord.java to read in and set up the grid can be taken and used directly in your program if you wish.  However,  you should NOT use the recursive part of FindWord directly in your program since it is not solving the problem you are trying to solve.   Further, you should not use this code to find individual words in your grid since FindWord allows directions to change within a single word, whereas in your assignment you are only allowed to change directions between words.   See additional comments in the FindWord.java code.

Submission:

Make sure you submit all of the following in a single .zip file:

1) All source files that you either wrote or utilized (ex: from the author’s files).  Call your main program file Assig3.java.

2) All data files

3) Your completed Assignment Information Sheet

As with Assignments 1 and 2, the idea from your submission is that your TA can compile and run your programs WITHOUT ANY additional files, so be sure to test them thoroughly before submitting them.  If you use an IDE for development, make sure your program runs from the command line without the IDE before submitting it.  If you cannot get the program working as specified, clearly indicate any changes you made and clearly indicate why, so that the TA can best give you partial credit.

Extra Credit Idea:

Allow your program to recurse diagonally as well as horizontally and vertically.  If you do this you will need to make up your own test files to demonstrate that your program works correctly.

Calculate the price of your order

Select your paper details and see how much our professional writing services will cost.

We`ll send you the first draft for approval by at
Price: $36
  • Freebies
  • Format
  • Formatting (MLA, APA, Chicago, custom, etc.)
  • Title page & bibliography
  • 24/7 customer support
  • Amendments to your paper when they are needed
  • Chat with your writer
  • 275 word/double-spaced page
  • 12 point Arial/Times New Roman
  • Double, single, and custom spacing
  • We care about originality

    Our custom human-written papers from top essay writers are always free from plagiarism.

  • We protect your privacy

    Your data and payment info stay secured every time you get our help from an essay writer.

  • You control your money

    Your money is safe with us. If your plans change, you can get it sent back to your card.

How it works

  1. 1
    You give us the details
    Complete a brief order form to tell us what kind of paper you need.
  2. 2
    We find you a top writer
    One of the best experts in your discipline starts working on your essay.
  3. 3
    You get the paper done
    Enjoy writing that meets your demands and high academic standards!

Samples from our advanced writers

Check out some essay pieces from our best essay writers before your place an order. They will help you better understand what our service can do for you.

Get your own paper from top experts

Order now

Perks of our essay writing service

We offer more than just hand-crafted papers customized for you. Here are more of our greatest perks.

  • Swift delivery
    Our writing service can deliver your short and urgent papers in just 4 hours!
  • Professional touch
    We find you a pro writer who knows all the ins and outs of your subject.
  • Easy order placing/tracking
    Create a new order and check on its progress at any time in your dashboard.
  • Help with any kind of paper
    Need a PhD thesis, research project, or a two-page essay? For you, we can do it all.
  • Experts in 80+ subjects
    Our pro writers can help you with anything, from nursing to business studies.
  • Calculations and code
    We also do math, write code, and solve problems in 30+ STEM disciplines.

Frequently asked questions

Get instant answers to the questions that students ask most often.

See full FAQ
  • What if I’m dissatisfied with the paper I get?

    The average quality score at our professional custom essay writing service is 8.5 out of 10. The high satisfaction rate is set by our Quality Control Department, which checks all papers before submission. The final check includes:
    • Compliance with initial order details.
    • Plagiarism.
    • Proper referencing.
    If for some reason we happen to leave a mistake unnoticed, you are invited to request unlimited revisions of your custom-written paper. For more information, check our Revision Policy. We will do our best to make your experience with Familiar Essays enjoyable.
  • I need an essay on the same day. Is it something you can do?

    Sure. Our writing company offers a fast service with an 8-hour deadline for orders up to master’s level. Make sure to specify the deadline in the order form and our writers will write a paper within the indicated timeslot. Just proceed to submit your requirements here Once you order a custom-written essay, our managers will assign your order to the well-suited writer, who has the best skills and experience for preparing your specific assignment. You can also request one of these extra features:
    • Choose the Writer’s Samples option – study 3 randomly-provided pages from orders that have been written by the assigned writer.
    • Request a specific writer – choose an academic writer from the dropdown list in the order’s form (optional for returning customers).
    You can be sure that your custom writing order will be accomplished by one of our 400+ professional academic writers. They all pass a series of tests to prove their writing prowess and hold the reputation of being the most professional in the industry. Want to make sure writer’s skills match your needs? Get more details on how to choose the appropriate author.
  • How can I be sure your writing service is not a scam?

    We understand that a shade of mistrust has covered the paper writing industry, and we want to convince you of our loyalty. Apart from high-quality writing services, we offer:
    • The chances of students to boost writing skills in a quick and effective way.
    • The opportunity to manage studies and free time in an enjoyable manner.
    • The possibilities to improve overall academic performance.
    Our custom writing company has been working for more than 12 years and always puts quality and clients’ needs first. Our operations are legally documented, we are easily accessible online and offline,
  • Is it legal to use your professional writing service?

    Yes. Custom writing help is not prohibited by any university or college. It’s a 100% legal way of getting professional assistance with paper writing. Hiring writers from an essay writing company is in many ways similar to consulting a tutor – we help you solve the writing issues at hand.
  • How does your service work?

    Our custom writing service is a reliable solution on your academic journey that will always help you if your deadline is too tight. You fill in the order form with your basic requirements for a paper: your academic level, paper type and format, the number of pages and sources, discipline, and deadline. Then, you describe the specific details of the paper you need: add the topic, write or paste the instructions, and attach files to be used, if you have them. After that, an online customer support representative chooses the best writer that specializes in your discipline and assigns him or her to complete the paper according to your requirements. When the paper is ready, we check it for plagiarism and send it to you. If you want to change something, you can request a free revision.
See full FAQ

Take your studies to the next level with our experienced specialists

Live ChatWhatsApp