传送门
分析:根上一题差不多,也是有6种操作方式
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include<cstring>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e2+5;
int n,m,k;
struct node{
int x,y,step,pots;
};
node pre[maxn][maxn];
string s[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
bool vis[maxn][maxn];
void output(int x,int y){
if(x==0&&y==0)
return;
node tem=pre[x][y];
output(tem.x,tem.y);
printf("%s\n",s[tem.pots].c_str());// 小技巧,.c_str()将string类转化为char类输出
}
void BFS()
{
memset(vis,false,sizeof(vis));
queue<node>q;
q.push(node{0,0,0});
while (!q.empty())
{
node now=q.front(); q.pop();
if(now.x==k||now.y==k){ printf("%d\n",now.step);output(now.x,now.y);return; }
int tx,ty;
// FILL(1)
if(n>now.x)
{
tx=n,ty=now.y;
if(!vis[tx][ty])
{
vis[tx][ty] = true;
q.push(node{tx, ty, now.step + 1, 0});
pre[tx][ty]=now,pre[tx][ty].pots=0;
}
}
// FILL(2)
if(m>now.y)
{
tx=now.x,ty=m;
if(!vis[tx][ty])
{
vis[tx][ty]=true;
q.push(node{tx,ty,now.step+1,1});
pre[tx][ty]=now,pre[tx][ty].pots=1;
}
}
// DROP(1)
if(now.x!=0)
{
tx=0,ty=now.y;
if(!vis[tx][ty])
{
vis[tx][ty]=true;
q.push(node{tx,ty,now.step+1,2});
pre[tx][ty]=now,pre[tx][ty].pots=2;
}
}
// DROP(2)
if(now.y!=0)
{
tx=now.x,ty=0;
if(!vis[tx][ty])
{
vis[tx][ty]=true;
q.push(node{tx,ty,now.step+1,3});
pre[tx][ty]=now;pre[tx][ty].pots=3;
}
}
// POUR(1,2)
if(now.x!=0)
{
if(now.x>m-now.y)
{
tx=now.x+now.y-m,ty=m;
if(!vis[tx][ty])
{
vis[tx][ty]=true;
q.push(node{tx,ty,now.step+1,4});
pre[tx][ty]=now;pre[tx][ty].pots=4;
}
} else{
tx=0,ty=now.x+now.y;
if(!vis[tx][ty]){
vis[tx][ty]=true;
q.push(node{tx,ty,now.step+1,4});
pre[tx][ty]=now,pre[tx][ty].pots=4;
}
}
}
// POUR(2,1)
if(now.y!=0)
{
if(now.y>n-now.x)
{
tx=n,ty=now.y+now.x-n;
if(!vis[tx][ty])
{
vis[tx][ty]=true;
q.push(node{tx,ty,now.step+1,5});
pre[tx][ty]=now,pre[tx][ty].pots=5;
}
} else{
ty=0,tx=now.y+now.x;
if(!vis[tx][ty]){
vis[tx][ty]=true;
q.push(node{tx,ty,now.step+1,5});
pre[tx][ty]=now,pre[tx][ty].pots=5;
}
}
}
}
printf("impossible\n");
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
BFS();
return 0;
}