[Programming thinking and practice Week2 job B] Pour Water

Meaning of the questions:

Two cups of a known volume, but now with two cups of water to give a particular capacity, filling the water glass can be emptied can pour each other.

Input:

Comprising a plurality of sets of input data. Each set of data inputs A, B, C
data range 0 <A <= B, C <= B <= 1000, A and B are relatively prime.

Sample Input:

2 7 5
2 7 4

Output:

Your program will output a series of instructions ( "fill A" represents A filling cups, "empty A" represents A cup emptied, "pour AB" A represents the water poured into the cup B and the filling the cup B A or emptied) components. These output lines can cause a jar just any unit C comprises water. Finally, each data line of output should be "success". Output lines start from the first column should not be any empty lines or trailing spaces.

Sample Output:

fill B
pour B A
success
fill A
pour A B
fill A
pour A B
success

Ideas:

After the operation amount of each of the two cups of water as a state, how to record transformed from one state to the next state, so that if each state imagine a point, from a state to another state is the dots connected, and the transitions between states only six kinds: filling a, B filling, emptying a, emptied B, a backward B, B Backward A. Our task is to find all the points connected until you find the point in question asked for (that has a capacity of a glass of water capacity is required), that is the breadth-first search from the starting state (BFS), to find the final state. After that, according to the transitions between states of our record back to the initial state approach to the final state can be found.

Code:

#include <iostream>
#include<stdio.h>
#include <string>
#include <map>
#include<queue>
using namespace std;
struct Status{
	int a;int b;
	Status(int _a=0,int _b=0)
	{
		a=_a;b=_b;
	}
	bool operator<(const Status&S)const
	{
		return a!=S.a?a<S.a:b<S.b;
	}
};

map<Status,pair<Status,string>> mp;
queue<Status> q;
void print(Status&t)
{
	if(mp.find(t)==mp.end()||(t.a==0&&t.b==0))
	{
		return;
	}
	pair<Status,string> s=mp[t];
	print(s.first);
	cout<<s.second<<endl;
}
void judge(Status &s,Status &t,const string&str)
{
	if(mp.find(t)==mp.end())
	{
		mp[t]=pair<Status,string>(s,str);
		q.push(t);
	}
}
void bfs(int A,int B,int C)
{
	Status s,t;
	s.a=0,s.b=0;
	while(!q.empty())
		q.pop();
	q.push(s);
	while(!q.empty())
	{
		s=q.front();
		q.pop();
		if(s.a==C||s.b==C)
		{
			print(s);
			return;
		}
		if(s.a>0)
		{//倒空A 
			t.a=0;
			t.b=s.b;
			judge(s,t,"empty A");
		}
		if(s.b>0)
		{//倒空B 
			t.a=s.a;
			t.b=0;
			judge(s,t,"empty B");
		}
		if(s.a!=A)
		{//倒满A 
			t.a=A;
			t.b=s.b;
			judge(s,t,"fill A");
			if(s.b!=0)
			{//B倒向A 
				if(s.b+s.a<=A)
				{
					t.a=s.a+s.b;
					t.b=0;
					judge(s,t,"pour B A");
				}
				else
				{
					t.a=A;
					t.b=s.a+s.b-A;
					judge(s,t,"pour B A");
				}
			}
		}
		if(s.b!=B)
		{//倒满B 
			t.a=s.a;
			t.b=B;
			judge(s,t,"fill B");
			if(s.a!=0)
			{//A倒向B 
				if(s.a+s.b<=B)
				{
					t.b=s.a+s.b;
					t.a=0;
					judge(s,t,"pour A B");
				}
				else
				{
					t.b=B;
					t.a=s.a+s.b-B;
					judge(s,t,"pour A B");
				}
			}
		}
	}
	
	
}


int main(int argc, char** argv) {
	int a,b,c;
	while(scanf("%d %d %d",&a,&b,&c)!=EOF)
	{
		getchar();
		bfs(a,b,c);
		cout<<"success"<<endl;
		mp.clear(); 
	}
	return 0;
}
Published 25 original articles · won praise 8 · views 548

Guess you like

Origin blog.csdn.net/weixin_44034698/article/details/104599085