题意:
两个壶,容量分别为A和B,问最少经过几次操作,其中一个壶的容量为C,输出流程
操作:
1.装满A或B
2.倒空A或B
3.A倒向B(要判断B已有的体积),B倒向A(要判断A已有的体积)
代码如下:
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
using namespace std;
#define INF 1e7+9
typedef long long ll;
const int N = 110;
int a, b, c;
int vis[N][N];
int f[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};
struct node
{
int a, b, st;
string ans;
};
node s, nex;
bool bfs()
{
queue<node>q;
while(!q.empty())q.pop();
vis[0][0] = 1;
s.a = 0, s.b = 0, s.st = 0;
q.push(s);
while(!q.empty())
{
s = q.front();
q.pop();
if(s.a == c || s.b == c) return true;
int x, y;
for(int i = 0; i < 6; i++)
{
if(i == 1) x = a, y = s.b;
else if(i == 2) x = s.a, y = b;
else if(i == 3)
{
if(s.a + s.b > a) x = a, y = s.a + s.b - a;
else x = s.a + s.b, y = 0;
}
else if(i == 4)
{
if(s.a + s.b > b) x = s.a + s.b - b, y = b;
else x = 0, y = s.a + s.b;
}
else if(i == 5) x = 0, y = s.b;
else x = s.a, y = 0;
if(!vis[x][y])
{
vis[x][y] = 1;
nex.a = x, nex.b = y, nex.st = s.st + 1;
char c = i + '0';
nex.ans = "";
nex.ans += (s.ans + c);
q.push(nex);
}
}
}
return false;
}
void output(char c)
{
if (c == '1')
puts("FILL(1)");
else if (c == '2')
puts("FILL(2)");
else if (c == '3')
puts("POUR(2,1)");
else if (c == '4')
puts("POUR(1,2)");
else if (c == '5')
puts("DROP(1)");
else
puts("DROP(2)");
}
int main()
{
while(~scanf("%d%d%d", &a, &b, &c))
{
memset(vis, 0, sizeof(vis));
if(bfs())
{
printf("%d\n", s.st);
for(int i = 0; i < s.st; i++)
{
output(s.ans[i]);
}
}
else puts("impossible");
}
return 0;
}