有点恶心的倒水模型,但只有两杯水,其实并不是很复杂。用数组记录路径,把6个动作分别对应上序号,bfs搞一搞就出来了
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#define f first
#define s second
using namespace std;
typedef pair<int,int> PII;
typedef pair<PII,int> PIl;
const int N = 101;
int a,b,c;
int vis[N][N];
PIl p[N][N];
void pt(int x){
if(x == 1) printf("FILL(1)\n");
else if(x == 2) printf("FILL(2)\n");
else if(x == 3) printf("DROP(1)\n");
else if(x == 4) printf("DROP(2)\n");
else if(x == 5) printf("POUR(1,2)\n");
else if(x == 6) printf("POUR(2,1)\n");
}
int bfs(){
memset(vis,0,sizeof(vis));
vis[0][0] = 1;
p[0][0] = {{-1,-1},-1};
queue<PII> Q;
vector<int> ans;
Q.push({0,0});
while(Q.size()){
PII u = Q.front();Q.pop();
if(u.f == c || u.s == c) {
while(1) {
PIl t = p[u.f][u.s];
if(t.f.f == -1 && t.f.s == -1) break;
//printf("%d %d %d\n",t.f.f,t.f.s,t.s);
ans.push_back(t.s);
u.f = t.f.f;u.s = t.f.s;
}
printf("%d\n",(int)ans.size());
for(int i = ans.size()-1;i>=0;i--)
pt(ans[i]);
return 1;
}
if(!vis[a][u.s])
p[a][u.s] = {{u.f,u.s},1},vis[a][u.s] = 1,Q.push({a,u.s});
if(!vis[u.f][b])
p[u.f][b] = {{u.f,u.s},2},vis[u.f][b] = 1,Q.push({u.f,b});
if(!vis[0][u.s])
p[0][u.s] = {{u.f,u.s},3},vis[0][u.s] = 1,Q.push({0,u.s});
if(!vis[u.f][0])
p[u.f][0] = {{u.f,u.s},4},vis[u.f][0] = 1,Q.push({u.f,0});
int d1 = min(u.f,b-u.s),d2 = min(u.s,a-u.f);
if(!vis[u.f-d1][u.s+d1])
p[u.f-d1][u.s+d1] = {{u.f,u.s},5},vis[u.f-d1][u.s+d1] = 1,
Q.push({u.f-d1,u.s+d1});
if(!vis[u.f+d2][u.s-d2])
p[u.f+d2][u.s-d2] = {{u.f,u.s},6},vis[u.f+d2][u.s-d2] = 1,
Q.push({u.f+d2,u.s-d2});
}
return 0;
}
int main(){
while(~scanf("%d%d%d",&a,&b,&c)){
if(!bfs()) printf("impossible\n");
}
return 0;
}