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
A storyboard is an example of an implementation tool.
alexdok [17]

Answer:

That's not true. A storyboard is an organizer to plan out certain things and to know how something should look before doing the finished product.

Explanation:

7 0
3 years ago
science and technology are interdependent advances in one lead to advances in the other. give an example of this phenomenon.
goblinko [34]
Wouldn't it Be Stem ? 
Science 
technology
Engineering 
Mathmatics 
5 0
3 years ago
The set of coordinating colors applied to backgrounds, objects, and text in a presentation is called:
Sonja [21]

Answer:

theme colors

Explanation:

As said, a group of colors that are used to format text and objects in a document. When you open the Color menu, these colors determine what you see.

3 0
2 years ago
What is a neuromorphic chip?
Dominik [7]

Answer:

A neuromorphic computer is a machine comprising many simple processors / memory structures (e.g. neurons and synapses) communicating using simple messages (e.g. spikes). ... Neuromorphic computing systems excel at computing complex dynamics using a small set of computational primitives (neurons, synapses, spikes).

Explanation:

The structure of neuromorphic computers makes them much more efficient at training and running neural networks. They can run AI models at a faster speed than equivalent CPUs and GPUs while consuming less power. This is important since power consumption is already one of AI's essential challenges.

3 0
2 years ago
Which query will give the following result when it it applied on table 1????!!!!!
Mandarinka [93]

Answer:

B

Explanation:

You need Name , Age and Gender

the second requirement should only match the last row where age is 20 and gender is male.

thrid requirement of the query is that its syntax should be correct.

Option B staisfies all of required so it is the correct option.

6 0
3 years ago
Other questions:
  • The adjustable contact of a potentiometer is placed at the center of its adjustment. If the total resistance is 5 kOhms, what is
    9·1 answer
  • Spreadshet formula to add totals​
    12·1 answer
  • In an inheritance situation, the new class that you create from an existing class is known as the:
    5·1 answer
  • What is the meaning for science?
    14·1 answer
  • the technique of blocking some parts of a photograph to emphasize (or draw attention to) another part of the same photograph is
    8·1 answer
  • How many bytes are there in 256 Kbytes?
    6·1 answer
  • When you send an email to your professor, a server holds that email until the professor requests it.
    8·1 answer
  • A _____ can be used to create and test prototypes, develop interfaces, and simulate factory layouts and assembly lines, without
    10·1 answer
  • Types of Computer games​
    6·1 answer
  • Range paramters - for loop<br> question in picture
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!