题意 给定两个两个容量分别为 a,b 的水壶,现在给定六种操作
- fill A
- fill B
- empty A
- empty B
- pour A B
- pour B A
现在问需要如何操作使得任意杯子里面的水有
n
加仑
思路:简单的bfs,用一个
#include <cstdio>
#include <string>
#include<iostream>
#include<vector>
#include <stack>
#include <queue>
#include <map>
#include <cstdlib>
#include<string.h>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef pair<int, int>pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef vector<vector<ll> >vvi;
typedef vector<ll> vi;
struct node
{
int x, y;
vector<int>op;
};
const int MAXN = 1010;
bool vis[MAXN][MAXN];
int main()
{
int a, b, n;
while (scanf("%d%d%d", &a, &b, &n) != EOF)
{
memset(vis, false, sizeof vis);
queue<node>q;
node start;
start.x = 0, start.y = 0;
q.push(start);
node ans;
ans.x = -1;
while (!q.empty())
{
node tmp = q.front();
q.pop();
if (vis[tmp.x][tmp.y])continue;
vis[tmp.x][tmp.y] = true;
if (tmp.x == n || tmp.y == n)
{
ans = tmp;
break;
}
node in = tmp;
in.x = a;
in.op.push_back(0);
q.push(in);
in = tmp;
in.y = b;
in.op.push_back(1);
q.push(in);
in = tmp;
in.x = 0;
in.op.push_back(2);
q.push(in);
in = tmp;
in.y = 0;
in.op.push_back(3);
q.push(in);
in = tmp;
int x = a - in.x;
if (x > in.y)
in.x += in.y, in.y = 0;
else in.x = a, in.y -= x;
in.op.push_back(5);
q.push(in);
in = tmp;
x = b - in.y;
if (x > in.x)
in.y += in.x, in.x = 0;
else in.y = b, in.x -= x;
in.op.push_back(4);
q.push(in);
}
vector<int> &v = ans.op;
for (int i = 0; i < v.size(); i++)
{
if (v[i] == 0)puts("fill A");
else if (v[i] == 1)puts("fill B");
else if (v[i] == 2)puts("empty A");
else if (v[i] == 3)puts("empty B");
else if (v[i] == 4)puts("pour A B");
else if (v[i] == 5)puts("pour B A");
}
puts("success");
}
}