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 spreadsheet has some values entered: Cell A1 contains 10, cell A2 contains 14, cell A3 contains 7. You enter in cell A4 the fo
lesya [120]
The value displayed in A4 is 24
5 0
3 years ago
Por qué no es tan común el uso de energías mas amigables con el medio ambiente en el país donde vives?
Effectus [21]

Answer:

Explanation:

English:

All forms of electricity generation have an environmental impact on our air, water, and land, but it varies. Of the total energy consumed in the United States, about 40% is used to generate electricity, making electricity use an important part of each person’s environmental footprint.

Producing and using electricity more efficiently reduces both the amount of fuel needed to generate electricity and the number of greenhouse gases and other air pollution emitted as a result. Electricity from renewable resources such as solar, geothermal, and wind generally does not contribute to climate change or local air pollution since no fuels are combusted.

The fuel mix for U.S. electricity generation

The chart below shows that most of the electricity in the United States is generated using fossil fuels such as coal and natural gas. A small but growing percentage is generated using renewable resources such as solar and wind.

Spanish:

Todas las formas de generación de electricidad tienen un impacto ambiental en nuestro aire, agua y tierra, pero varía. Del total de energía consumida en los Estados Unidos, alrededor del 40% se usa para generar electricidad, lo que hace que el uso de electricidad sea una parte importante de la huella ambiental de cada persona.

Producir y usar electricidad de manera más eficiente reduce tanto la cantidad de combustible necesario para generar electricidad como la cantidad de gases de efecto invernadero y otra contaminación del aire emitida como resultado. La electricidad de recursos renovables como la energía solar, geotérmica y eólica generalmente no contribuye al cambio climático ni a la contaminación del aire local, ya que no se queman combustibles.

Combustible para la generación de electricidad en EE. UU.

El cuadro a continuación muestra que la mayor parte de la electricidad en los Estados Unidos se genera utilizando combustibles fósiles como el carbón y el gas natural. Un porcentaje pequeño pero creciente se genera utilizando recursos renovables como la solar y la eólica.

5 0
3 years ago
Text line breaks and returns are controlled using which tags?
eduard
<b></b> <span>element when used to strongly emphasize portions of text within a document.
<p></p> </span><span>tag defines a paragraph.
<ol></ol> </span><span>Defines an ordered list
<a></a> </span><span> tag defines a hyperlink</span>
5 0
3 years ago
Read 2 more answers
In the ISO/IEC 27002 framework, _________________ describes the use and controls related to encryption.
Nezavi [6.7K]

Answer:

In the ISO/IEC 27002 framework, Cryptography describes the use and controls related to encryption, to help an organization implement, maintain, and improve its information security management.

7 0
3 years ago
Outline four advantages of digital<br>cameras over analogue cameras​
Phoenix [80]

Answer:

Explanation:

A digital camera refers to a camera whereby the photographs are being captured in digital memory. An analogue camera refers to the traditional camera that typically sends video over cable.

The advantages of digital cameras over analogue cameras include:

1. Massive Storage Space:

There is a large storage space for photos and thus helps to prevent limitations to film. There are memory cards that can store several images.

2. Multiple functions:

The digital camera performs several functions like face detection, night and motion detection This makes capturing of images more fun and brings about better images.

3. Video Camera:

Digital cameras can also capture moving pictures while analog camera typically captures images that are still. Digital camera is vital as it can be used for live streaming.

4. Smaller and Lighter:

Digital cameras are usually smaller and lighter which makes them more portable and easy to carry about.

7 0
3 years ago
Other questions:
  • The accessibility of a website refers to which of
    6·1 answer
  • HELP QUICKLY!!! IF YOUR RIGHT I'LL MARK YOU BRAINLIEST
    15·2 answers
  • To more easily incorporate methods into a program, it is common practice to store methods in their own classes and files. Then y
    6·1 answer
  • The _____ option will require users to have a password to access the document.
    10·2 answers
  • WHO WANTS TO PLAY AMONG US
    15·2 answers
  • Phil wants to make a dark themed superhero movie. What could be his target demographic
    11·2 answers
  • Which of the following popular presentation software items do you have to purchase in order to use?
    11·2 answers
  • What is the value of creating recurring tasks?
    7·2 answers
  • Demons I shall be your eternal nightmare
    13·1 answer
  • On a leading-trailing system technician A says that both shoes do equal work when stopping while going in reverse. Technician B
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!