CS1010 Notes
  • Welcome
  • Lec/Tut/Lab/Exes
    • Lecture
      • Lec 01 - Computational Problem Solving
      • Lec 02 - Functions and Types
      • Lec 03 - Basic C Programming
      • Lec 04 - Conditionals
      • Lec 05 - Loops
      • Lec 06 - Call Stacks, Arrays
        • Diagnostic Quiz
      • Lec 07 - Pointers, Memory management
        • Diagnostic Quiz
      • Lec 08 - Multi-d Array, Efficiency
        • Diagnostic Quiz
      • Lec 09 - Searching and Sorting
        • Diagnostic Quiz
      • Lec 10 - More Recursion
        • Diagnostic Quiz
      • Lec 11 - Strcut & Standard I/O
        • Diagnostic Quiz
      • Lec 12 - Recap
    • Tutorial
      • Tut 01 - Computational Problem-Solving
      • Tut 02 - Functions and Conditionals
      • Tut 03 - More on Conditionals
      • Tut 04 - Loops
      • Tut 08 - Searching and Sorting
    • Lab
      • Lab 01 - Unix/Vim Setup
      • Lab 02 - Debugging
      • Lab 03 - Assert
      • Lab 04 - Test Cases
      • Lab 05 - Arrays
      • Lab 06 - Memory Errors
      • Lab 07 - Compiling with Clang
      • Lab 08 - C Preprocessor
      • Lab 09 - Backtracking
      • Lab 10 - Struct and Wrap up
    • Exercises
      • Exercise 3 - Fixed-Length Arrays
      • Exercise 4 - Dynamic Arrays and Strings
      • Exercise 6 - Searching and Sorting
      • Exercise 7 - More Recursion
      • Exercise 8 - Struct
  • Past Year Exam
    • Midterm PE
      • PE1 (AY18/19)
      • PE1 (AY20/21)
      • PE1 (AY21/22)
      • PE0 (AY22/23)
      • PE0 (AY23/24)
    • Midterm Paper
      • Midterm (AY18/19)
      • Midterm (AY20/21)
      • Midterm (AY21/22)
      • Midterm (AY22/23)
    • PE1 Review
      • PE1 (AY23/24)
    • PE2 Review
      • PE2 (AY18/19)
      • PE2 (AY20/21)
      • PE2 (AY21/22)
      • PE2 (AY22/23)
      • PE2 (AY23/24)
    • Final Paper
      • Final (AY18/19)
      • Final (AY20/21)
      • Final (AY21/22)
      • Final (AY22/23)
      • Final (AY23/24)
  • Current Year Exam
    • PE0 (AY24/25)
    • PE1 (AY24/25)
    • PE2 (AY24/25)
    • Final (AY24/25)
  • Toolbox
    • Vim & Unix
    • GDB
  • After CS1010
Powered by GitBook
On this page
  • Problems
  • 1. Fill
  • 2. Maze
  • 3. Walk
  • 4. Sudoku
  • Tips
Edit on GitHub
  1. Lec/Tut/Lab/Exes
  2. Exercises

Exercise 7 - More Recursion

PreviousExercise 6 - Searching and SortingNextExercise 8 - Struct

Last updated 7 months ago

Problems

1. Fill

The troublesome part of this question is the creation and read of 3-D array. Also, understand this question is a bit hard. (Refer to this about "flood filling algorithm" for clearer understanding)

2. Maze

The main take-away it to use another array which is the same size as the input 2-D char array to store the explored grids. This idea of unvisited_array is used very often!

3. Walk

This is a classic dynamic programming problem. This one is just a easy version which only needs to build the table according to the "upper" and "left"

4. Sudoku

A variation problem of (find one solution version).

This enhances our wishful thinking in deciding the return type of our recursion function. Let's see how.

In this question, we are supposed to find one solution. So, our recursive function should return whether we have found a solution or not (This is an obvious boolean question). Now, use wishful thinking, after some try on the current solution and if there is a solution to the smaller problem, we will return TRUE, which means we have found a solution.

In this exercise, the trickest part is to print the puzzles. There are two things to keep in mind:

  1. The moment you "update" every cell, print the puzzles ("update" here doesn't confirm the solution is correct, we may still need the backtracking)

  2. Print the result at last after you find the solution

Tips

  1. When writing recursion in 2-D array, always think about how would you not enter an "infinite loop". This is usually down in your condition to enter the recursion. Some common methods are:

    1. mark the attendance of the "cell" you have updated. (a.k.a build an unvisited_array)

    2. there is some initial "constant" that you can compare to

video
N-Queens