POJ-3414 Pots

本文提供了一道经典的水壶转移问题——POJ-3414Pots的解决方案。通过使用广度优先搜索(BFS)算法,在给定两个不同容量的水壶的情况下,找到能够恰好测量特定水量的最短操作步骤。文章详细展示了如何构建状态图,并通过队列实现BFS算法来遍历所有可能的状态,最终输出操作序列。

POJ-3414 Pots

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i) empty the pot i to the drain;
  3. 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)

my analysis

This problem is implicit graph, using BFS to solve, and finally using stack output operation

solution(C++)

#include<iostream>
#include<queue>
#include<stack>
#include<string>
using namespace std;
int A, B, a = 0, b = 0, C;
bool inq[10000][10000] = { false };
struct bottle
{
	int a;
	int b;
	int pre;
	int now;
	string ope;
}bot;
struct bottle path[10000];
int sum = 0;
int BFS()
{
	queue<bottle> q;
	bot.a = 0, bot.b = 0;
	bot.pre = -1, bot.now = 0;
	path[0] = bot;
	q.push(bot);
	while (!q.empty())
	{
		bottle top = q.front();
		q.pop();
		if (top.a == C || top.b == C)
			return top.now;
		//six operations
		if (top.a < A&&inq[A][top.b] == false)
		{
			inq[A][top.b] = true;
			bot.a = A, bot.b = top.b;
			bot.pre = top.now, bot.now = ++sum;
			bot.ope = "FILL(1)";
			path[sum] = bot;
			q.push(bot);
		}
		if (top.b < B&&inq[top.a][B] == false)
		{
			inq[top.a][B] = true;
			bot.a = top.a, bot.b = B;
			bot.pre = top.now, bot.now = ++sum;
			bot.ope = "FILL(2)";
			path[sum] = bot;
			q.push(bot);
		}
		if (top.a != 0 && inq[0][top.b] == false)
		{
			inq[0][top.b] = true;
			bot.a = 0, bot.b = top.b;
			bot.pre = top.now, bot.now = ++sum;
			bot.ope = "DROP(1)";
			path[sum] = bot;
			q.push(bot);
		}
		if (top.b != 0 && inq[top.a][0] == false)
		{
			inq[top.a][0] = true;
			bot.a = top.a, bot.b = 0;
			bot.pre = top.now, bot.now = ++sum;
			bot.ope = "DROP(2)";
			path[sum] = bot;
			q.push(bot);
		}
		if (top.b + top.a >= B && inq[top.a - (B - top.b)][B] == false)//pour A to B
		{
			inq[top.a - (B - top.b)][B] = true;
			bot.a = top.a - (B - top.b), bot.b = B;
			bot.pre = top.now, bot.now = ++sum;
			bot.ope = "POUR(1,2)";
			path[sum] = bot;
			q.push(bot);
		}
		if (top.b + top.a < B && inq[0][top.b + top.a] == false)
		{
			inq[0][top.b + top.a] = true;
			bot.a = 0, bot.b = top.b + top.a;
			bot.pre = top.now, bot.now = ++sum;
			bot.ope = "POUR(1,2)";
			path[sum] = bot;
			q.push(bot);
		}
		if (top.b + top.a >= A && inq[A][top.b-(A-top.a)] == false)//pour B to A
		{
			inq[A][top.b - (A - top.a)] = true;
			bot.a = A, bot.b = top.b - (A - top.a);
			bot.pre = top.now, bot.now = ++sum;
			bot.ope = "POUR(2,1)";
			path[sum] = bot;
			q.push(bot);
		}
		if (top.b + top.a < A && inq[top.b + top.a][0] == false)
		{
			inq[top.b + top.a][0] = true;
			bot.a = top.b + top.a, bot.b = 0;
			bot.pre = top.now, bot.now = ++sum;
			bot.ope = "POUR(2,1)";
			path[sum] = bot;
			q.push(bot);
		}
	}
	return -1;
}
int main()
{
	cin >> A >> B >> C;
	stack<bottle> p;
	int res = BFS();
	if (res == -1)
		cout << "impossible" << endl;
	else
	{
		while (res != 0)
		{
			p.push(path[res]);
			res = path[res].pre;
		}
		cout << p.size() << endl;
		while (!p.empty())
		{
			cout << p.top().ope << endl;
			p.pop();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值