Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11885 Accepted: 5025 Special Judge
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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
以前曾经做过的一道题,今天再做发现没有思路,看来要滚滚原题了,这个题就是把所有的情况分为六种操作,bfs这六种操作
#include <map>
#include <list>
#include <climits>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-9
#define LL unsigned long long
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define CRR fclose(stdin)
#define CWW fclose(stdout)
#define RR freopen("input.txt","r",stdin)
#define WW freopen("output.txt","w",stdout)
const int Max = 10010;
struct node
{
int a;
int b;
int step;
string str[120];
};
int A,B,C;
bool vis[120][120];
bool BFS()
{
memset(vis,false,sizeof(vis));
node f,s;
f.a=0;
f.b=0;
f.step=0;
queue<node>Q;
Q.push(f);
vis[0][0]=true;
while(!Q.empty())
{
f=Q.front();
// cout<<f.a<<" "<<f.b<<endl;
Q.pop();
if(f.a==C||f.b==C)
{
cout<<f.step<<endl;
for(int i=1;i<=f.step;i++)
{
cout<<f.str[i]<<endl;
}
return true;
}
if(f.a==0)
{
s=f;
s.a=A;
s.step++;
s.str[s.step]="FILL(1)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
if(f.a<=A&&f.a)
{
s=f;
s.a=0;
s.step++;
s.str[s.step]="DROP(1)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
if(f.b<B&&f.a)
{
s=f;
s.step++;
if(s.a+s.b<=B)
{
s.b+=s.a;
s.a=0;
}
else
{
s.a=s.a+s.b-B;
s.b=B;
}
s.str[s.step]="POUR(1,2)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
if(f.b==0)
{
s=f;
s.b=B;
s.step++;
s.str[s.step]="FILL(2)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
if(f.b<=B&&f.b)
{
s=f;
s.b=0;
s.step++;
s.str[s.step]="DROP(2)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
if(f.a<A&&f.b)//这里要特别注意,不要写错顺序
{
s=f;
s.step++;
if(s.a+s.b<=A)
{
s.a+=s.b;
s.b=0;
}
else
{
s.b=s.a+s.b-A;
s.a=A;
}
s.str[s.step]="POUR(2,1)";
if(!vis[s.a][s.b])
{
vis[s.a][s.b]=true;
Q.push(s);
}
}
}
return false;
}
int main()
{
while(~scanf("%d %d %d",&A,&B,&C))
{
if(!BFS())
{
printf("impossible
");
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。