H - Pots POJ - 3414

H - 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");
		}
	}
}

AC代码2:

#include <cstdio>
#include <map>
#include <vector>
#include <queue>
#include <cstring>
#include <iostream>

#define ll long long
using namespace std;
const int maxn = 105;
const int INF = 0x3f3f3f3f;
/*
fill(1) 1
drop(1) 2
pour12 3
pour21 4
fill2 5
drop2 6
*/
int a, b, c;
vector<int> ans;
vector<int> arr;
vector<int> brr;
bool vis[maxn][maxn];
int flag = false;
struct Node
{
	int x, y;
	int num;
	Node(int _x, int _y, int _num) {x=_x;y=_y;num=_num;}
};
void bfs()
{
	queue<Node> que;
	que.push(Node(0,0,-1));
	int cnt = 0;
	while (que.size())
	{
		Node nn = que.front(); que.pop();
		int i = nn.x, j = nn.y;
		vis[i][j] =true;
		if(i == c || j == c)
		{
			flag = true;
			int k= 0;
			for (int i = nn.num; i!=-1; i=arr[i])
			{
				brr.push_back(i);
				k++;
			}
			printf("%d\n", k);
			for(int i = brr.size()-1; i >= 0; --i)
			{
				switch (ans[brr[i]])
				{
				case 1:printf("FILL(1)\n");break;
				case 2:printf("DROP(1)\n");break;
				case 3:printf("POUR(1,2)\n");break;
				case 4:printf("POUR(2,1)\n");break;
				case 5:printf("FILL(2)\n");break;
				case 6:printf("DROP(2)\n");break;
				}
			}
			return;
		}
		if(!vis[a][j])
		{
			ans.push_back(1);
			arr.push_back(nn.num);
			que.push(Node(a, j,cnt++));
		}
		if(!vis[0][j])
		{
			ans.push_back(2);
			arr.push_back(nn.num);
			que.push(Node(0, j,cnt++));
		}
		if(!vis[i][b])
		{
			ans.push_back(5);
			arr.push_back(nn.num);
			que.push(Node(i, b,cnt++));
		}
		if(!vis[i][0])
		{
			ans.push_back(6);
			arr.push_back(nn.num);
			que.push(Node(i, 0,cnt++));
		}
		if(i+j<=a && !vis[i+j][0])
		{
			ans.push_back(4);
			arr.push_back(nn.num);
			que.push(Node(i+j, 0,cnt++));
		}
		if(i+j<=b && !vis[0][i+j])
		{
			ans.push_back(3);
			arr.push_back(nn.num);
			que.push(Node(0, i+j,cnt++));
		}
		if(i+j>b && !vis[i-b+j][b])
		{
			ans.push_back(3);
			arr.push_back(nn.num);
			que.push(Node(i-b+j, b,cnt++));
		}
		if (i+j>a && !vis[a][j-a+i])
		{
			ans.push_back(4);
			arr.push_back(nn.num);
			que.push(Node(a, j-a+i,cnt++));
		}
	}
}
int main() {
	scanf("%d%d%d", &a, &b, &c);getchar();
	memset(vis,0,sizeof vis);
	bfs();
	if(!flag)
		printf("impossible\n");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值