poj3414 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 ≤ ≤ 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 AB, 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)

题意: 给你两个杯子,每个杯子容量不一定,每次对一个杯子可以进行如下操作: 倒满,倒水,将水倒到另一个杯子中去。求最短路径使得任一杯子中有C大小的水。


分析:我们需要模拟倒水的操作。可以发现,初始状态相同时结果是一样的,所以需要一个记忆化数组来避免重复操作(这里TLE了)。然后就是六个方向的bfs。。。

难点在于如何打印路径,我选择将每一个状态的路径记录到一个vector里,最后通过转化函数(turn)来打印路径。


代码:

#include<iostream>
#include<cstdio>
#include<string.h>
#include<math.h>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
using namespace std;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;

struct sta {
	int p1, p2;
	vector<int> act;		//记录每次操作
	int cnt;				//步数
	sta(int _cnt = 0) {
		cnt = _cnt;
	}
};
int a, b, c;
void fillpot(sta &temp, int i)
{
	temp.cnt++;
	if (i == 1) {
		temp.p1 = a;
		temp.act.push_back(1);
	}
	else {
		temp.p2 = b;
		temp.act.push_back(2);
	}
	return;
}
void drop(sta &temp, int i)
{
	temp.cnt++;
	if (i == 1) {
		temp.p1 = 0;
		temp.act.push_back(10);
	}
	else {
		temp.p2 = 0;
		temp.act.push_back(20);
	}
	return;
}
void pour(sta &temp, int i, int j)          // pour water from i to j
{
	temp.cnt++;
	if (i == 1) {
		temp.act.push_back(12);
		if (temp.p2 + temp.p1 > b) {
			int left = b - temp.p2;
			temp.p1 = temp.p1 - left;
			temp.p2 = b;
		}
		else {
			temp.p2 = temp.p2 + temp.p1;
			temp.p1 = 0;
		}
	}
	else {
		temp.act.push_back(21);
		if (temp.p1 + temp.p2 > a) {
			int left = a - temp.p1;
			temp.p2 = temp.p2 - left;
			temp.p1 = a;
		}
		else {
			temp.p1 = temp.p1 + temp.p2;
			temp.p2 = 0;
		}
	}
}
void turn(int t)
{
	if (t == 1) {
		printf("FILL(1)\n");
	}
	else if (t == 2) {
		printf("FILL(2)\n");
	}
	else if (t == 12) {
		printf("POUR(1,2)\n");
	}
	else if (t == 21) {
		printf("POUR(2,1)\n");
	}
	else if (t == 10) {
		printf("DROP(1)\n");
	}
	else {
		printf("DROP(2)\n");
	}
}
int main()
{
	while (scanf("%d%d%d", &a, &b, &c) != EOF) {
		int vis[200][200];			//记忆化数组
		memset(vis, 0, sizeof(vis[0][0]) * 200 * 200);
		vis[0][0] = 1;
		queue<sta> q;
		sta temp;
		temp.p1 = 0;
		temp.p2 = 0;
		q.push(temp);
		int flag = 0;
		while (!q.empty()) {
			temp = q.front();
			q.pop();
			if (temp.p1 == c || temp.p2 == c) {
				flag = 1;		//成功
				printf("%d\n", temp.cnt);
				while (!temp.act.empty()) {
					int t = temp.act.front();
					temp.act.erase(temp.act.begin());
					turn(t);
				}
				break;
			}
			if (temp.p1 != a && temp.p2 != b) {
				sta p1 = temp;
				fillpot(p1, 1);
				if (vis[p1.p1][p1.p2] == 0) {
					vis[p1.p1][p1.p2] = 1;
					q.push(p1);
				}
			}
			if (temp.p2 != b && temp.p1 != a) {
				sta p2 = temp;
				fillpot(p2, 2);
				if (vis[p2.p1][p2.p2] == 0) {
					vis[p2.p1][p2.p2] = 1;
					q.push(p2);
				}
			}
			if (temp.p1 != 0 && temp.p2 != 0) {
				sta p3 = temp;
				drop(p3, 1);
				if (vis[p3.p1][p3.p2] == 0) {
					vis[p3.p1][p3.p2] = 1;
					q.push(p3);
				}
			}
			if (temp.p2 != 0 && temp.p1 != 0) {
				sta p4 = temp;
				drop(p4, 2);
				if (vis[p4.p1][p4.p2] == 0) {
					vis[p4.p1][p4.p2] = 1;
					q.push(p4);
				}
			}
			if (temp.p1 != 0 && temp.p2 != b) {
				sta p5 = temp;
				pour(p5, 1, 2);
				if (vis[p5.p1][p5.p2] == 0) {
					vis[p5.p1][p5.p2] = 1;
					q.push(p5);
				}
			}
			if (temp.p1 != a && temp.p2 != 0) {
				sta p6 = temp;
				pour(p6, 2, 1);
				if (vis[p6.p1][p6.p2] == 0) {
					vis[p6.p1][p6.p2] = 1;
					q.push(p6);
				}
			}
		}
		if (flag == 0) printf("impossible\n");

	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值