1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Mila [183]
3 years ago
15

In this puzzle you have a list of n pitchers, with known capacities, c1, c2, ... cn, of holding water in gallons. You have a fau

cet that can be used as often and as much as the player needs. The goal is to measure exactly g gallons of water, using nothing other than these pitchers. Suppose the current amounts of water that these pitchers hold is w1, w2, ... wn,. These numbers are assumed to be 0 initially. The puzzle is solved as soon as wi = g for any i. A player can perform the following operations:
 Fill pitcher i (from the faucet). The precondition of this operation is: ci > wi ≥ 0. The effect is: wi = ci.

 Empty pitcher i. The precondition of this operation is: ci ≥ wi > 0. The effect is: wi = 0.

 Pour pitcher i to pitcher j. The precondition of this operation is: (ci ≥ wi > 0) and (cj > wj ≥ 0). In words, pitcher i must have some water to pour, and pitcher j must have some unused capacity to receive it. The (partial) effect is: (wi = 0) or (wj = cj) or both. In words, the pour operation must continue until pitcher i becomes empty (and its content is added to pitcher j's content), or pitcher j becomes full (and pitcher i retains the remainder), whichever occurs first. They may occur simultaneously.

A Sample Run

Select the puzzle to solve:

1. Pitchers

2. Eight puzzle

Your selection: 1

Enter the number of pitchers: 3

Enter the capacities of the 3 pitchers (gallons): 2, 5, 10

Enter the goal (gallons): 1

Current configuration: [0, 0, 0]

Please select your next move from the following choices:

1. Fill pitcher 1

2. Fill pitcher 2

3. Fill pitcher 3

Your selection: 2

Current configuration: [0, 5, 0]

Please select your next move from the following choices:

1. Fill pitcher 1

2. Fill pitcher 3

3. Empty pitcher 2

4. Pour pitcher 2 to 1

5. Pour pitcher 2 to 3

Your selection: 4

Current configuration: [2, 3, 0]

Please select your next move from the following choices:

1. Fill pitcher 2

2. Fill pitcher 3

3. Empty pitcher 1

4. Empty pitcher 2

5. Pour pitcher 1 to 2

6. Pour pitcher 1 to 3

7. Pour pitcher 2 to 3

Your selection: 3 Current configuration: [0, 3, 0]

Please select your next move from the following choices:

1. Fill pitcher 1

2. Fill pitcher 2

3. Fill pitcher 3

4. Empty pitcher 2

5. Pour pitcher 2 to 1

6. Pour pitcher 2 to 3

Your selection: 5 Current configuration: [2, 1, 0]

Great! You have reached the goal in 4 moves. Bye.
Computers and Technology
1 answer:
oee [108]3 years ago
4 0

Answer:

See explaination

Explanation:

#include <iostream>

#include <vector>

using namespace std;

// function to print configuration for each pitcher

void printConfig(vector<int> w, int n)

{

cout << "Current configuration: [" << w[0];

for(int i=1; i<n; i++)

{

cout << ", " << w[i];

}

cout << "]" << endl;

}

void getOptions(vector<int> c, vector<int> w, int n, vector<pair<int, pair<int, int>>> &options)

{

// options count

int count = 0;

// fill

for(int i=0; i<n; i++)

{

if(c[i] > w[i] && w[i] >= 0)

{

count ++;

cout << count << ". Fill pitcher " << (i+1) << endl;

// add options to list

options.push_back(make_pair(1, make_pair(i, 0)));

}

}

// empty

for(int i=0; i<n; i++)

{

if(c[i] >= w[i] && w[i] > 0)

{

count++;

cout << count << ". Empty pitcher " << (i+1) << endl;

// add options to list

options.push_back(make_pair(2, make_pair(i, 0)));

}

}

// Pour pitcher i to pitcher j

for(int i=0; i<n; i++)

{

if(c[i] >= w[i] && w[i] > 0)

{

for(int j=0; j<n; j++)

{

if(i!=j && c[j] > w[j] && w[j] >= 0)

{

count++;

cout << count << ". Pour pitcher " << (i+1) << " to " << (j+1) << endl;

// add options to list

options.push_back(make_pair(3, make_pair(i, j)));

}

}

}

}

}

void execute(vector<int> c, vector<int> &w, pair<int, pair<int, int>> option)

{

int i = option.second.first;

// for fill

if(option.first == 1)

{

w[i] = c[i];

}

// empty

else if(option.first == 2)

{

w[i] = 0;

}

// pour

else

{

int j = option.second.second;

if(w[i] >= c[j])

{

w[i] = w[i] - c[j];

w[j] = c[j];

}

else

{

w[j] = w[i];

w[i] = 0;

}

}

}

int main()

{

// required variables

int choice, n, temp, g, flag, moves = 0;

// vectors are dynamic sized arrays

vector<int> c, w;

vector<pair<int, pair<int, int>>> options;

// select puzzle

cout << "Select the puzzle to solve:\n";

cout << "1. Pitchers\n";

cout << "2. Eight puzzle\n";

cout << "Your selection: ";

cin >> choice;

if(choice == 1)

{

// input values required

cout << "Enter the number of pitchers: ";

cin >> n;

// array for pitchers

cout << "Enter the capacities of the " << n << " pitchers (gallons): ";

for(int i=0; i<n; i++)

{

cin >> temp;

c.push_back(temp);

w.push_back(0);

}

cout << "Enter the goal (gallons): ";

cin >> g;

// start game

while(true)

{

// print configuration

printConfig(w, n);

// check for goal state

flag = 0;

for(int i=0; i<n; i++)

{

if(w[i] == g)

{

flag = 1;

break;

}

}

if(flag)

{

cout << "Great! You have reached the goal in " << moves << " moves. Bye." << endl;

break;

}

// get and print options

//empty previous options

options.clear();

getOptions(c, w, n, options);

// ask for user's selection

cout << "Your selection: ";

cin >> choice;

// update moves

moves++;

// perform according to the selection

execute(c, w, options[choice-1]);

}

}

return 0;

}

You might be interested in
What is a difference between a waxing crescent and a waning gibbous? (1 point) waxing crescent: first quarter waning gibbous: th
iren2701 [21]

Answer:

  waxing crescent: first quarter waning gibbous: third quarter

Explanation:

The moon is waxing (getting larger) in the first quarter after the new moon. It is waning (getting smaller) in the third quarter, after the full moon at the end of the second quarter.

The shape of the moon is a "crescent" when it is less than a semicircle. It is "gibbous" when it is more than a semicircle. As it waxes, the crescent grows until the moon is half-full at the end of the first quarter, then it is a waxing gibbous moon through the second quarter until it is full at the end of the quarter. Following the full moon, it is waning gibbous until it becomes half-full again at the end of the third quarter. Finally, it is a waning crescent through the fourth quarter until the new moon.

  waxing crescent: first quarter waning gibbous: third quarter

5 0
3 years ago
You are a disgruntled employee with a master’s degree in computer sciences who was recently laid off from a major technology com
WARRIOR [948]

Answer:

If you know what you're doing, I'd suggest hacking into the company's website...

Explanation:

3 0
3 years ago
The photo shows a group of girls reacting to comments they have read about a classmate on a social media site.
shepuryov [24]

Answer:

no

Explanation:

beacuse

3 0
3 years ago
Sorry but, what are brainliest for?
Advocard [28]

Answer:

smartest and accurate answer

Explanation:

5 0
3 years ago
Read 2 more answers
Why would someone make a histogram instead of a bar chart?
Natasha_Volkova [10]

Answer:

Someone can make a histogram instead of a bar chart if distributions of variables are need to be represented and if data is quantitative.

Explanation:

Histograms are drawn to represent distributions of variables whereas bar charts are used to compare various variables. Histograms plot quantitative data whereas bar charts plot categorical data.

So, someone can make a histogram instead of a bar chart if distributions of variables are needed to be represented and if data is quantitative.

8 0
3 years ago
Other questions:
  • To write 10 lines of code on paper:
    10·1 answer
  • An individual is first with the network before they are authorized to access resources on the network A. countermeasure B. vulne
    11·1 answer
  • Given the following code segment, what is output after "result = "? int x = 1, y = 1, z = 1; y = y + z; x = x + y; cout &lt;&lt;
    7·1 answer
  • A person who wants to buy a compact disc (cd) has just enough money to buy one, and chooses cd a instead of cd
    10·2 answers
  • As the new manager at your local grocery store, you want to create a more efficient inventory process by allowing outside vendor
    6·1 answer
  • Write an algorithm that gets as input your current credit card balance, the total dollar amount of new purchases, and the total
    8·1 answer
  • People, can anyone point me a highlighter for google chorme ?
    11·1 answer
  • Class C Airspace inner ring begins at the __________ and extends vertically (by definition) to MSL charted values that generally
    5·1 answer
  • I only put one answer and didn’t specify what it answered
    14·2 answers
  • In PowerPoint, a picture might be a photograph, a shape you draw, a piece of clip art, or an illustration created using a graphi
    7·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!