Files
CSCI-1200/lectures/13_advanced_recursion_II
Jidong Xiao d7f19cb35d adding 13
2025-02-26 12:16:53 -05:00
..
2025-02-26 12:16:53 -05:00

Test 2 Information

Students will be randomly assigned to a test room and seating zone will be on Submitty soon.

  • Test 2 will be held Thursday, 02/27/2025 from 6-7:50pm.

If you have a letter from Disability Services for Students and you have not already emailed it to ds_instructors@cs.rpi.edu, please do so ASAP. Meredith Widman will be in contact with you about your accommodations for the test.

  • Coverage: Lectures 1-13, Labs 1-5, and Homeworks 1-5.
  • Practice problems from previous exams are available on the course website. The best way to prepare is to completely work through and write out your solution to each problem, before looking at the answers.
  • OPTIONAL: you are allowed to bring one physical piece of 8.5x11” paper, thats two “sides”. We will check at the start of the exam that you do not have more than one piece of paper for your notes!
  • Bring to the exam room: Your Rensselaer photo ID card. Pencil(s) & eraser (pens are ok, but not recommended). The exam will involve handwriting code on paper (and other short answer problem solving). Neat legible handwriting is appreciated. Computers, cell-phones, smart watches, calculators, music players, etc. are not permitted. Please do not bring your laptop, books, backpack, etc. to the exam room leave everything in your dorm room. Unless you are coming directly from another class or sports/club meeting.

Lecture 13 --- Advanced Recursion, Part II

  • Advanced Recursion — problems that cannot be easily solved using iteration (for or while loops):
    Non-linear maze search
  • Take a look at the following grid of characters.
heanfuyaadfj
crarneradfad
chenenssartr
kdfthileerdr
chadufjavcze
dfhoerpadlfc
neicoetrtlkf
paermpuohtrr
diofetaycrhg
daldruetryrt
  • If you can start from any location of this grid, and go forward, backward, up and down. Can you find the word computer in this grid? (Note: The same letter cell may not be used more than once.)
  • A sketch of the solution is as follows: The grid of letters is represented as vector<vector<char>> grid; Each vector<char> represents a row. We can treat this as a two-dimensional array.
    A word to be sought, such as “computer” is read as a string.
    A pair of nested for loops searches the grid for occurrences of the first letter in the string. Call such a location (r, c).
    • At each location where the first letter is found, a search of the second letter is initiated in the 4 neighboring locations of location (r, c).
    • Make this process recursive: at each location where the ith letter is found, a search of the (i+1)th letter is initiated in the 4 neighboring locations.
    • The search can stop when all letters of the string are found - this is the base case of the recursion.
    • Question: how to make sure we do not use the same letter more than once on our success path?

13.2 Exercise: Complete the implementation

13.3 Summary of Nonlinear Word Search Recursion

  • Recursion starts at each location where the first letter is found.
  • Each recursive call attempts to find the next letter by searching around the current position. When it is found, a recursive call is made.
  • The current path is maintained at all steps of the recursion.
  • The “base case” occurs when the path is full or all positions around the current position have been tried.