poj3414 倒水并输出过程

Description
You are given two pots, having the volume of A and B liters respectively.
The following operations can be performed:
FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i)      empty the pot i to the drain;
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)
思路:bfs爆搜。首先确定状态:两个桶中水的体积,采取行动的次数,当前状态是由6种操作中那种得来的。
关键:
1.实现6种操作。
2.要用vis记录数组,否则最后的结果不一定是采取行动次数最少。
3.如何回溯输出路径。(借助二维结构体)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define N 105

using namespace std;

typedef struct note {
	int v1, v2;//桶1,2中的水的体积
	int step;
	int type;//1到6分别表示FILL(1),FILL(2),DROP(1),DROP(2),POUR(1,2),POUR(2,1)
}cup;

cup pre[N][N];//二维结构体记录路径
cup cur, tmp;//用来记录队列,队首元素和下一个可能的元素
int a, b, c;//输入
int vis[N][N];//两维分别表示,两个桶的状态

void choose(int i)
{
	int sum;
	switch (i)
	{
	case 1:
		tmp.v1 = a;
		break;
	case 2:
		tmp.v2 = b;
		break;
	case 3:
		tmp.v1 = 0;
		break;
	case 4:
		tmp.v2 = 0;
		break;
	case 5:
		sum = tmp.v1 + tmp.v2;
		if (sum <= b)//倒完
		{
			tmp.v1 = 0;
			tmp.v2 = sum;
		}
		else
		{
			tmp.v1 -= b - tmp.v2;
			tmp.v2 = b;
		}
		break;
	case 6:
		sum = tmp.v1 + tmp.v2;
		if (sum <= a)//倒完
		{
			tmp.v1 = sum;
			tmp.v2 = 0;
		}
		else
		{
			tmp.v2 -= a - tmp.v1;
			tmp.v1 = a;
		}
		break;
	}
}

void print(cup t)//回溯输出路径
{
	if (pre[t.v1][t.v2].v1 != 0 || pre[t.v1][t.v2].v2 != 0)
		print(pre[t.v1][t.v2]);
	switch (t.type)
	{
	case 1: printf("FILL(1)\n"); break;
	case 2: printf("FILL(2)\n"); break;
	case 3: printf("DROP(1)\n"); break;
	case 4: printf("DROP(2)\n"); break;
	case 5: printf("POUR(1,2)\n"); break;
	case 6: printf("POUR(2,1)\n"); break;
	default : break;
	}
}

int bfs()
{
	//初始化队列
	queue<cup>	q;
	while (q.size())	q.pop();
	//初始化初状态并入队
	cup t;
	t.v1 = 0; t.v2 = 0; t.step = 0;// t.type = -1;
	q.push(t);
	vis[0][0] = 1;
	pre[0][0].v1 = 0; pre[0][0].v2 = 0;// pre[0][0].type = -1;

	while (q.size())
	{
		cur = q.front(); q.pop();
		if (cur.v1 == c || cur.v2 == c)//两桶中有一个水的体积达到c
		{
			printf("%d\n", cur.step);
			return 1;
		}
		for (int i = 1; i <= 6; i++)//共6种情况
		{
			tmp = cur;
			choose(i);//采取行动
			if (!vis[tmp.v1][tmp.v2])
			{
				tmp.step++; 
				tmp.type = i;
				q.push(tmp);
				vis[tmp.v1][tmp.v2] = 1;
				pre[tmp.v1][tmp.v2] = cur;//记录路径
			}
		}
	}

	return 0;
}

int main()
{
	int ans;
	while (~scanf("%d%d%d", &a, &b, &c))
	{
		memset(vis, 0, sizeof(vis));
		ans = bfs();
		if (ans)
			print(cur);
		else
			printf("impossible\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值