/*
Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1980 Accepted: 866 Special Judge
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
1. FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
2. DROP(i) empty the pot i to the drain;
3. POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
Source
Northeastern Europe 2002, Western Subregion*/
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int visited[100][100] = {0};
class ab
{
public:
int A;
int B;
int sign;
ab *p;
};
void solution(int A,int B,int C)
{
queue<ab> q;
stack<ab> out;
ab tmpg[10000];
int g = 0;
ab tmp,tmpp;
int time = 0;
tmp.A = 0;
tmp.B = 0;
tmp.sign = 0;
tmp.p = NULL;
q.push(tmp);
visited[tmp.A][tmp.B] = 1;
while(q.empty()!=1)
{
tmp = q.front();
if(tmp.A == C || tmp.B == C)
{
for(;tmp.sign != 0;)
{
out.push(tmp);
time++;
tmp = *(tmp.p);
}
cout<<time<<endl;
for(int i = 0;i < time;i++)
{
tmp = out.top();
out.pop();
if(tmp.sign == 1)
{
cout<<"FILL(1)"<<endl;
continue;
}
if(tmp.sign == 2)
{
cout<<"FILL(2)"<<endl;
continue;
}
if(tmp.sign == 3)
{
cout<<"DROP(1)"<<endl;
continue;
}
if(tmp.sign == 4)
{
cout<<"DROP(2)"<<endl;
continue;
}
if(tmp.sign == 5)
{
cout<<"POUR(1,2)"<<endl;
continue;
}
if(tmp.sign == 6)
{
cout<<"POUR(2,1)"<<endl;
continue;
}
}
return;
}
else
{
q.pop();
tmpp.A = A;
tmpp.B = tmp.B;
if(visited[tmpp.A][tmpp.B] == 0)
{
tmpp.sign = 1;
tmpg[g] = tmp;
tmpp.p = &tmpg[g];
g++;
q.push(tmpp);
visited[tmpp.A][tmpp.B] = 1;
}
tmpp.A = tmp.A;
tmpp.B = B;
if(visited[tmpp.A][tmpp.B] == 0)
{
tmpp.sign = 2;
tmpg[g] = tmp;
tmpp.p = &tmpg[g];
g++;
q.push(tmpp);
visited[tmpp.A][tmpp.B] = 1;
}
tmpp.A = 0;
tmpp.B = tmp.B;
if(visited[tmpp.A][tmpp.B] == 0)
{
tmpp.sign = 3;
tmpg[g] = tmp;
tmpp.p = &tmpg[g];
g++;
q.push(tmpp);
visited[tmpp.A][tmpp.B] = 1;
}
tmpp.A = tmp.A;
tmpp.B = 0;
if(visited[tmpp.A][tmpp.B] == 0)
{
tmpp.sign = 4;
tmpg[g] = tmp;
tmpp.p = &tmpg[g];
g++;
q.push(tmpp);
visited[tmpp.A][tmpp.B] = 1;
}
tmpp.A = tmp.A;
tmpp.B = tmp.B;
tmpp.B = tmp.B + tmp.A;
if(tmpp.B > B)
{
tmpp.B = B;
tmpp.A = tmp.A - (B - tmp.B);
}
else
{
tmpp.A = tmpp.A - (tmpp.B - tmp.B);
}
if(visited[tmpp.A][tmpp.B] == 0)
{
tmpp.sign = 5;
tmpg[g] = tmp;
tmpp.p = &tmpg[g];
g++;
q.push(tmpp);
}
tmpp.A = tmp.A;
tmpp.B = tmp.B;
tmpp.A = tmp.A + tmp.B;
if(tmpp.A > A)
{
tmpp.A = A;
tmpp.B = tmp.B - (A - tmp.A);
}
else
{
tmpp.B = tmpp.B - (tmpp.A - tmp.A);
}
if(visited[tmpp.A][tmpp.B] == 0)
{
tmpp.sign = 6;
tmpg[g] = tmp;
tmpp.p = &tmpg[g];
g++;
q.push(tmpp);
}
}
}
cout<<"impossible"<<endl;
return;
}
int main(void)
{
int A,B,C;
cin>>A>>B>>C;
solution(A,B,C);
return 0;
}
这题卡了24小时。。。。。。。。。。WA。。。不知道哪错。。。。想了个招抓了别人AC的代码然后我的代码和别人AC的代码小改下输入变成循环。。。A,B,C从0开始到100嵌套。。。然后两个程序的结果输出到TXT文本。。。最后再写个fstream的小程序抓两个文本的东西每行比对看哪里不同。。。。。。。。结果从0到100刷不动电脑死机了。。。。。。。。。从0到39完全一样。。。。最后从90到100发现有不样的地方。。。可惜步骤太多都是100多两百多步的根本没法查。。。。。。。。。。投降之。。。我又发别人的代码AC当作过了一题。。。。。。。。。不是我无耻谁让我笨呢-__-|||