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

Apl3 3

Task 1 implements Marc's Cakewalk problem using branch and bound. It sorts calories in descending order and calculates the minimum miles walked based on increasing calorie factor. Task 2 checks if columns in a character grid are in ascending alphabetical order after sorting each row alphabetically. It returns "YES" if columns are sorted or "NO" otherwise. Code for both tasks sorts and iterates through vectors, returning the result.

Uploaded by

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

Apl3 3

Task 1 implements Marc's Cakewalk problem using branch and bound. It sorts calories in descending order and calculates the minimum miles walked based on increasing calorie factor. Task 2 checks if columns in a character grid are in ascending alphabetical order after sorting each row alphabetically. It returns "YES" if columns are sorted or "NO" otherwise. Code for both tasks sorts and iterates through vectors, returning the result.

Uploaded by

jatinkumar20308
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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