week2 job B

Meaning of the questions:
pour problem "fill A" represents the filling A cup, "empty A" represents emptied A cup, "pour AB" represents the water poured into the A and B cup to B cup filling or emptying A.
Input:
Input comprising a plurality of sets of data. Each set of data inputs A, B, C data range 0 <A <= B, C <= B <= 1000, A and B are relatively prime.
Output:
Your program's output will be a series of instructions. 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.
INPUT the Sample:
2. 7 2. 5. 4. 7
the Sample Output:
Fill B
pour BA
Success
Fill A
pour AB
Fill A
pour AB
Success
the Notes:
If your output with Sample Output different, it does not matter. For the answer to this question is multiple solutions in a "AB C", not by the standard text comparison to determine the correctness of your program. So this question by the SPJ (Special Judge) program to determine whether the code you write correctly.
Ideas:
Definition of a structure, the amount of which is defined in the two cups. There are six states, fill A, fill B, empty A, empty B, pour AB, pour BA stored in an array. Defined in six different states, namely how to achieve. Direct it to 0, fill in the state, so two bottles of water in all its full amount in the empty state. In pour state, when the pour AB, if a cup of water is not 0, if the amount of the conventional two cups of water are added cup capacity is greater than b, it is the amount of b cup full amount, and amount is an amount of a cup after the filling of the remaining cups b. If two cups of existing amount is smaller than b cup capacity, amount (b) of the cup and the amount of existing cups, and a cup blank. Analyzing by bfs, the result will be pressed into the container, the order of the result stored in the container is achieved if the output quantity of a conventional cup or cup b is the required amount.
Code:
#include
// # the include
#include
#include
#include
#include
the using namespace STD;
String TEMP [. 6] = { "Fill A", "empty A", "B Fill", "empty B", "pour AB "," pour BA "};
// int VIS [1005] [1005];
struct the Node
{
int bottlea, bottleb;
Vector TEMP;
};
void BFS (A int, int B, int C)
{
int VIS [1000] [1000] = {0};
queue q;
struct Node node;
node.bottlea=0;
node.bottleb=0;
q.push(node);
vis[0][0]=1;
memset(vis,0,sizeof(vis));
while(!q.empty())
{
node=q.front();
q.pop();
for(int i=0;i<6;i++)
{
struct Node now=node;
switch(i)
{
case 0:
now.bottlea=a;
break;
case 1:
now.bottlea=0;
break;
case 2:
now.bottleb=b;
break;
case 3:
now.bottleb=0;
break;
case 4:
if(now.bottlea!=0)
{
if(now.bottlea<=b-now.bottleb)
{
now.bottleb=now.bottleb+now.bottlea;
now.bottlea=0;
}
else
{
now.bottlea=now.bottlea+now.bottleb-b;
now.bottleb=b;
}
}
break;
case 5:
if(now.bottleb!=0)
{

             if(now.bottleb+now.bottlea<=a)
             {
              now.bottlea=now.bottlea+now.bottleb;
              now.bottleb=0;
 }
 else
 {
  now.bottleb=now.bottleb+now.bottlea-a;
  now.bottlea=a;
 }
 }
 break;

}
now.temp.push_back(i);
if(now.bottlebc||now.bottleac)
{
for (vector::iterator tem = now.temp.begin(); tem != now.temp.end(); tem++){
cout << temp[*tem] << endl;}
cout<<“success”<<endl;
return;
}
else
{
if(!vis[now.bottlea][now.bottleb])

    q.push(now);
vis[now.bottlea][now.bottleb]=true;

}
}
}
}
int main()
{
int A,B,C;
//while(scanf("%d%d%d",&a,&b,&c))
while(cin>>A>>B>>C)
{
bfs(A,B,C);
}
return 0;
}

Published 19 original articles · won praise 0 · Views 221

Guess you like

Origin blog.csdn.net/weixin_45117273/article/details/104738476