0% found this document useful (0 votes)
9 views6 pages

AP Exp 9

Uploaded by

Sushmit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

AP Exp 9

Uploaded by

Sushmit Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING


Experiment - 9
Student Name: Sambhav Dua UID: 22BCS11570
Branch: BE-CSE Section/Group: 640-B
Semester: 5th Date of Performance: 22 Oct, 2024
Subject Name: Advanced Programming Lab-1 Subject Code: 22CSP-314

1. Aim: To implement the concepts of Back-tracking.

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

1. Aim: To implement the concept of Back-tracking.

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.

4. Output: Return the completed crossword grid.


DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5. Implementation/Code:
#include <bits/stdc++.h>
using namespace std;
vector<string> crosswordPuzzle(vector<string> crossword, string words) {
vector<string> wordList;
stringstream ss(words);
string word;
while (getline(ss, word, ';')) {
wordList.push_back(word);
}
auto canPlaceWord = [&](int row, int col, const string& word, bool horizontal) {
if (horizontal) {
if (col + word.size() > 10) return false;
for (int i = 0; i < word.size(); i++) {
if (crossword[row][col + i] != '-' && crossword[row][col + i] != word[i]) {
return false;
}
}
} else {
if (row + word.size() > 10) return false;
for (int i = 0; i < word.size(); i++) {
if (crossword[row + i][col] != '-' && crossword[row + i][col] != word[i]) {
return false;
}
}
}
return true;
};
auto placeWord = [&](int row, int col, const string& word, bool horizontal) {
if (horizontal) {
for (int i = 0; i < word.size(); i++) {
crossword[row][col + i] = word[i];
}
} else {
for (int i = 0; i < word.size(); i++) {
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
crossword[row + i][col] = word[i];
}
}
};
auto removeWord = [&](int row, int col, const string& word, bool horizontal) {
if (horizontal) {
for (int i = 0; i < word.size(); i++) {
crossword[row][col + i] = '-';
}
} else {
for (int i = 0; i < word.size(); i++) {
crossword[row + i][col] = '-';
}
}
};
function<bool(int)> backtrack = [&](int index) {
if (index == wordList.size()) return true;
string currentWord = wordList[index];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (canPlaceWord(i, j, currentWord, true)) {
placeWord(i, j, currentWord, true);
if (backtrack(index + 1)) return true;
removeWord(i, j, currentWord, true);
}
if (canPlaceWord(i, j, currentWord, false)) {
placeWord(i, j, currentWord, false);
if (backtrack(index + 1)) return true;
removeWord(i, j, currentWord, false);
}
}
}
return false;
};
backtrack(0);
return crossword;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

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.

You might also like