You are on page 1of 6

Experiment 3.

Student Name: Jatin UID: 21BCS1949


Branch: BE-CSE Section/Group: 902 A
Semester: 5th Date: 27 October 2023
Subject Name: Advance Programming Lab Subject Code: 21CSP314

1. Aim: Implement the problems based on Branch and Bound.

2. Objective:
Task 1: Marc's Cakewalk - Marc loves cupcakes, but he also likes to stay fit. Each
cupcake has a calorie count, and Marc can walk a distance to expend those calories. If
Marc has eaten j cupcakes so far, after eating a cupcake with c calories he must walk
at least 2j * c miles to maintain his weight.
Task 2: Grid Challenge - Given a square grid of characters in the range ascii[a-
z],rearrange elements of each row alphabetically, ascending. Determine if the
columns are also in ascending alphabetical order, top to bottom. Return YES if they
are or NO if they are not.

3. Code and output:


Task 1: Marc's Cakewalk
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
long marcsCakewalk(vector<int> calorie)

sort(calorie.rbegin(), calorie.rend());
long miles = 0;
long calorieFactor = 1;
for (int i = 0; i < calorie.size(); i++)
{
miles += calorie[i] * calorieFactor;
calorieFactor *= 2;
}
return miles;
int main()
{
int n;
cin >> n;
vector<int> calorie(n);
for (int i = 0; i < n; i++)
{
cin >> calorie[i];
}
long result = marcsCakewalk(calorie);
cout << result << endl;
return 0;
}

Output:
Task 2: Grid Challenge
Code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
string gridChallenge(vector<string> grid)
{
int n = grid.size();
int m = grid[0].size();
for (int i = 0; i < n; i++)
{

sort(grid[i].begin(), grid[i].end());
}
for (int j = 0; j < m; j++)
{
for (int i = 0; i < n - 1; i++)
{
if (grid[i][j] > grid[i + 1][j])
{
return "NO";
}
}
}
return "YES";
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n; // Number of rows
vector<string> grid(n);
for (int i = 0; i < n; i++)
{
cin >> grid[i];

}
string result = gridChallenge(grid);
cout << result << endl;
}
return 0;
}
Output:

You might also like