Pots POJ - 3414

Pots

  POJ - 3414 

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 jis 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)
题解:
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>

#define mod 1000000007
const int INF = 0x7f7f7f7f;
typedef long long ll;
const int maxn = 10001;
using namespace std;

struct node
{
	int value;//杯子装有的水
	int capa;//杯子容量
	int step;
	int pre;
	int fro;
	int num;
	node(int _v, int _c, int _s, int _p,int _f,int _n){value = _v; capa = _c;step = _s; pre = _p; fro = _f;num= _n;}
	node(){}
}vec[maxn];
int z[maxn],A,B,c,k=0;
char h[6][11]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
bool vis[105][105];

int bfs()
{
	node a, b;
	a.value = b.value = 0;
	a.capa = A;
	b.capa = B;
	queue<node> que;
	vec[0].value = vec[0].capa = vec[0].step = 0;
	vec[0].pre = -1; vec[0].fro = -1; vec[0].num = 0;
	que.push(vec[0]); // 第一个杯子现有水,第二个杯子现有水。
	vis[0][0] = true;
	int cnt = 1;
	while(!que.empty())
	{
		node n = que.front();
		if(n.value == c || n.capa == c)
		{
			int temp = n.num;
			for(int i = 0; vec[temp].fro!=-1; i++)
			{
				z[i] = vec[temp].pre;
				temp = vec[temp].fro;
			}
			return n.step;
		}
		for(int i = 0; i < 6; i++)
		{
			a.value = n.value;
			b.value = n.capa;
			switch(i)
			{
			case 0:a.value = a.capa;break;
			case 1:b.value = b.capa;break;
			case 2:a.value = 0; break;
			case 3:b.value = 0; break;
			case 4:if(b.value + a.value > b.capa)
				   {
					   a.value = a.value - b.capa + b.value;
					   b.value = b.capa;
				   }
				   else
				   {
					   b.value = b.value + a.value;
					   a.value = 0;
				   }
				   break;
			case 5:if(b.value + a.value > a.capa)
				   {
					   b.value = b.value - a.capa + a.value;
					   a.value = a.capa;
				   }
				   else
				   {
					   a.value = b.value + a.value;
					   b.value = 0;
				   }
				   break;
			}
			if(!vis[a.value][b.value])
			{
				vis[a.value][b.value] = true;
				vec[cnt].value = a.value;
				vec[cnt].capa = b.value;
				vec[cnt].step = n.step + 1;
				vec[cnt].pre = i;
				vec[cnt].fro = n.num;
				vec[cnt].num = cnt;
				que.push(vec[cnt++]);
			}
		}
		que.pop();
	}
	return 0;
}
int main()
{
	while(scanf("%d%d%d", &A, &B, &c) != EOF)
	{
		memset(vis, 0, sizeof(vis));
		int k = bfs();
		if(k)
		{
			printf("%d\n", k);
			for(int i = k-1; i >= 0; i--)
				puts(h[z[i]]);
		}
		else
		{
			printf("impossible\n");
		}
	}
}


猜你喜欢

转载自blog.csdn.net/qq_40758751/article/details/80374589