M - Pots
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)
不怎么会bfs所以写的比较差,其实就是广搜,一点点找这个状态可以做哪些操作,然后慢慢搜,可以做的就存起来,然后一直下去,记得存走过的位置
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
//#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
//#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
string op[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};//存放操作,便于输出
//操作中,0是1加水,1是2加水 ,2是1倒水,3是2倒水,4是1倒给2,5是2倒给1
int A,B,C;
int visit[110][110];//用来存走过的状态,免得又走回去
struct water
{
int x,y,bits;
int a[1000];//记录经过的操作
water(){}
};
water ans;//放答案
queue<water>v;//做一个队列
void bfs(water q)
{
water t,tt;
while(!v.empty())
{
t=v.front();
v.pop();
if(t.x ==C||t.y ==C)//走到要求的了可以跳出了
{
ans=t;
return ;
}
if(visit[t.x][t.y])
continue;
else
visit[t.x][t.y]=1;
rep(i,0,6)
{
if(i==0)
{
if(t.x!=A)//判断可以做这个操作吗
{
tt=t;
tt.x=A;
tt.a[t.bits]=0;
tt.bits ++;
v.push(tt);
}
}
else if(i==1)
{
if(t.y!=B)//判断可以做这个操作吗
{
tt=t;
tt.y=B;
tt.a[t.bits]=1;
tt.bits++;
v.push(tt);
}
}
else if(i==2)
{
if(t.x)//判断可以做这个操作吗
{
tt=t;
tt.x=0;
tt.a[t.bits]=2;
tt.bits++;
v.push(tt);
}
}
else if(i==3)
{
if(t.y)//判断可以做这个操作吗
{
tt=t;
tt.y=0;
tt.a[t.bits]=3;
tt.bits++;
v.push(tt);
}
}
else if(i==4)
{
if(t.x)//判断可以做这个操作吗
{
tt=t;
if(t.x+t.y<=B)//判断会有多余的水吗
{
tt.x=0;
tt.y=t.x+t.y;
}else
{
tt.y=B;
tt.x=t.x+t.y-B;
}
tt.a[t.bits]=4;
tt.bits++;
v.push(tt);
}
}
else if(i==5)
{
if(t.y)//判断可以做这个操作吗
{
tt=t;
if(t.x+t.y<=A)//判断会有多余的水吗
{
tt.y=0;
tt.x=t.x+t.y;
}else
{
tt.x=A;
tt.y=t.x+t.y-A;
}
tt.a[t.bits]=5;
tt.bits++;
v.push(tt);
}
}
}
}
}
int main()
{
cin>>A>>B>>C;
water q;//
q.x=0;
q.y=0;
q.bits=0;
mm(visit,0);
mm(q.a,0);
v.push(q); //这些是初始化操作
bfs(q);
if(ans.bits) //最后如果有操作次数就输出,没输出impossible。。之前交了一发发现没加上这个
{
cout<<ans.bits<<endl;
rep(i,0,ans.bits)//输出操作
cout<<op[ans.a[i]]<<endl;
}
else
cout<<"impossible";
return 0;
}