0% found this document useful (0 votes)
64 views7 pages

Computer Science & Engineering Computer Science & Engineering

The document contains details of two experiments conducted: 1) Implementing a dynamic programming solution to fill words into a 10x10 crossword grid given a set of words and empty spots marked on the grid. 2) Implementing a recursive solution to calculate the nth Fibonacci number given an input n. Both problems demonstrate the use of dynamic programming and backtracking techniques.

Uploaded by

lostinspace981
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)
64 views7 pages

Computer Science & Engineering Computer Science & Engineering

The document contains details of two experiments conducted: 1) Implementing a dynamic programming solution to fill words into a 10x10 crossword grid given a set of words and empty spots marked on the grid. 2) Implementing a recursive solution to calculate the nth Fibonacci number given an input n. Both problems demonstrate the use of dynamic programming and backtracking techniques.

Uploaded by

lostinspace981
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

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 3.2

Student Name: Aditya Singh UID: 21BCS2466


Branch: CSE Section/Group: 611/B
Semester: 5th Date of Performance: 19/10/23
Subject Name: Advance Programming Lab Subject Code: 21CSP-314

1. Aim: Dynamic Programming: Implement the problems based on Dynamic


Programming.

2. Objective: a) A 10x10 Crossword grid is provided to you, along with a set of words
(or names of places) which need to be filled into the grid. Cells are marked either + or
-. Cells marked with a - are to be filled with the word list.

b) Fibonacci Sequence: Given n, return the nth number in the sequence.


3. DBMS script and output:
a)
#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;

const int GRID_SIZE = 10;

typedef struct {
int x;
int y;
bool isHoriz;
int len;
} placement;
bool sortLongestFirst(const placement& left, const placement& right){
if ([Link] != [Link]) return [Link] > [Link];
if (left.x != right.x) return left.x < right.x;
return left.y < right.y;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

bool longfirst(const string& left, const string& right){


return [Link]() > [Link]();
}

bool solve(const vector<vector<char> >& grid,


const vector<placement>& spots,
const vector<string>& words){

if ([Link]() && [Link]()){


for(int i=0; i<GRID_SIZE; i++){
for(int j=0; j<GRID_SIZE; j++){
cout << grid[i][j];
}
cout << endl;
}
return true;
}
if ([Link]() || [Link]()) return false;
if (words[0].length() != spots[0].len) return false;
vector<placement> spots2([Link]()+1, [Link]());
vector<string> words2;
for(int i=0; i<[Link](); i++){
const string& w = words[i];
vector<vector<char> > g2 = grid;
int x = spots[0].x;
int y = spots[0].y;
bool valid = true;
for(int l = 0; l<spots[0].len; l++){
if (g2[x][y]=='-' || g2[x][y]==w[l]){
g2[x][y] = w[l];
} else {
valid = false;
break;
}
if (spots[0].isHoriz) y++;
else x++;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if (!valid) continue;

[Link]();
for(int i2=0; i2<i; i2++) words2.push_back(words[i2]);
for(int i2=i+1; i2<[Link](); i2++) words2.push_back(words[i2]);
if (solve(g2, spots2, words2)) return true;
}
return false;
}

int main(void){
vector<vector<char> > grid(GRID_SIZE, vector<char>(GRID_SIZE));
for(int i=0; i<GRID_SIZE; i++){
string line;
cin >> line;
for(int j=0; j<GRID_SIZE; j++){
grid[i][j] = line[j];
}
}
string wordline;
cin >> wordline;
vector<string> words;
wordline+=";";
istringstream iss(wordline);
string item;
while(getline(iss, item, ';')) words.push_back(item);

vector<placement> spots;
placement empty_spot;
for(int i=0; i<GRID_SIZE; i++){
int gap_start = -1;
int gap_len = -1;
for(int j=0; j<GRID_SIZE; j++){
if (grid[i][j]=='-'){
if (gap_start>=0) gap_len++;
else {
gap_start = j;
gap_len = 1;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
} else {
if (gap_len>1){
empty_spot.x = i;
empty_spot.y = gap_start;
empty_spot.isHoriz = true;
empty_spot.len = gap_len;
spots.push_back(empty_spot);
}
gap_start = -1;
gap_len = -1;
}
}
if (gap_len>1){
empty_spot.x = i;
empty_spot.y = gap_start;
empty_spot.isHoriz = true;
empty_spot.len = gap_len;
spots.push_back(empty_spot);
}
}
for(int j=0; j<GRID_SIZE; j++){
int gap_start = -1;
int gap_len = -1;
for(int i=0; i<GRID_SIZE; i++){
if (grid[i][j]=='-'){
if (gap_start>=0) gap_len++;
else {
gap_start = i;
gap_len = 1;
}
} else {
if (gap_len>1){
empty_spot.x = gap_start;
empty_spot.y = j;
empty_spot.isHoriz = false;
empty_spot.len = gap_len;
spots.push_back(empty_spot);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

}
gap_start = -1;
gap_len = -1;
}
}
if (gap_len>1){
empty_spot.x = gap_start;
empty_spot.y = j;
empty_spot.isHoriz = false;
empty_spot.len = gap_len;
spots.push_back(empty_spot);
}
}
sort([Link](), [Link](), sortLongestFirst);
sort([Link](), [Link](), longfirst);
solve(grid, spots, words);
return 0;
}

Result:

b) #include <iostream>
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

using namespace std;

int fibonacci(int n) {
if(n==0)
return 0;
else
{
if(n==1)
return 1;
else
return fibonacci(n-1)+fibonacci(n-2);
}
}
int main() {
int n;
cin >> n;
cout << fibonacci(n);
return 0;
}

Result:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Learning Outcome
• Learned about backtracking
• Backtracking is often implemented using a recursive approach
• At each decision point in the problem-solving process, backtracking explores all
possible choices or options

You might also like