AP Exp 9
AP Exp 9
2. Problem Statement:
The Fibonacci sequence is a series of numbers where each number (Fibonacci
number) is the sum of the two preceding ones, starting from 0 and 1. The sequence
starts as follows: 0, 1, 1, 2, 3, 5, 8, 13, ... and so on.
3. Objective:
• Implement a recursive function to find the n-th Fibonacci number.
• Given an integer n, return the value at index n in the Fibonacci sequence using a
recursive approach.
4. Algorithm:
1. Base Cases:
If n=0, return 0.
If n=1, return 1.
2. Recursive Case:
For n>1, return the sum of the two preceding Fibonacci numbers, i.e.,
F(n)=F(n−1)+F(n−2)
3. Input:
Read an integer ‘n’ from the user.
4. Recursive Function:
Call the recursive function fibonacci(n).
Return the result to the main function and print it.
5. Output:
Print the n-th Fibonacci number.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5. Implementation/Code:
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
cin >> n;
cout << fibonacci(n) << endl;
return 0;
}
6. Output:
7. Complexity:
Time Complexity: O(2n)
Space Complexity: O(n)
8. Learning Outcomes:
Learn how to break down a complex problem into simpler problems recursively.
This problem shows how back-tracking can lead to an optimal solution.
Learn to analyze the trade-off between time and space using recursion.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Problem- 2
2. Problem Statement: You are tasked with filling a crossword grid based on a given
set of words. The grid consists of cells marked with either ‘+’ (which are blocked)
or ‘-’ (which are empty and can be filled). Your goal is to place the words from the
provided list into the grid, ensuring that they fit either horizontally or vertically
without overlapping incorrectly with existing letters.
3. Objective:
The objective of the crosswordPuzzle function is to return a completed crossword
grid where all the words from the input list are placed correctly according to the
rules of crossword puzzles. The function should ensure that:
Words can only be placed in cells marked with ‘-’.
Words must fit within the bounds of the grid.
Words cannot overlap with other words unless they share the same letter.
4. Algorithm:
1. Input Parsing: Split the input string of words into a list.
2. Helper Functions:
• canPlaceWord: Check if a word can be placed at a specific position (either
horizontally or vertically) without conflicts.
• placeWord: Place a word in the grid at the specified position.
• removeWord: Remove a word from the grid, reverting the cells back to ‘-’.
3. Backtracking:
• Use a recursive function to attempt to place each word in the grid.
• For each word, iterate through each cell in the grid and try placing the word
both horizontally and vertically.
• If a word is successfully placed, recursively attempt to place the next word.
• If all words are placed successfully, return the filled grid.
• If a word cannot be placed, backtrack by removing the last placed word and
trying the next possibility.
6. Output:
7. Complexity:
2
Time Complexity: O(n! * m )
Space Complexity: O(1)
8. Learning Outcomes:
Understanding application of the backtracking algorithm.
It enhances skills in manipulating 2D arrays.
Learned how to traverse arrays in two different directions.