POj3414 Pots


Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9831 Accepted: 4141 Special Judge

Description

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)

思路:此题考察BFS打印路径,我用fa记录前一个操作来递归打印的

#include <cstdio>
#include <map>
#include <queue>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
const int INF=0x7fffffff;
int a,b,c;
const int maxn=100000;
bool vis[110][110];
struct cup
{
	int acup,bcup;
	int dis;//记录操作步数
	int op;//记录操作类型
	int fa;//记录前一个操作
	int id;//区分每一个操作
};
queue<cup>q;
cup cc[maxn];
int  bfs()
{
	while(!q.empty())
		q.pop();
	cc[0].acup=0,cc[0].bcup=0,cc[0].dis=0,cc[0].fa=-1,cc[0].op=-1,cc[0].id=0;
	vis[0][0]=1;
	q.push(cc[0]);
	int  cnt=1;
	while(!q.empty())
	{
		cup temp;
		temp=q.front();
		q.pop();
		if(temp.acup==0)
		{
			cc[cnt].acup=a;
			cc[cnt].bcup=temp.bcup;
			cc[cnt].dis=temp.dis+1;
			cc[cnt].op=1;
			cc[cnt].fa=temp.id;
			cc[cnt].id=cnt;
			if(!vis[cc[cnt].acup][cc[cnt].bcup])
			{
			   q.push(cc[cnt]);
			   vis[cc[cnt].acup][cc[cnt].bcup]=1;
			}
			if(cc[cnt].acup==c||cc[cnt].bcup==c)
				return cnt;
			cnt++;
		}
		if(temp.bcup==0)
		{
			cc[cnt].bcup=b;
			cc[cnt].acup=temp.acup;
			cc[cnt].dis=temp.dis+1;
			cc[cnt].op=2;
			cc[cnt].fa=temp.id;
			cc[cnt].id=cnt;
			if(!vis[cc[cnt].acup][cc[cnt].bcup])
			{
			   q.push(cc[cnt]);
			   vis[cc[cnt].acup][cc[cnt].bcup]=1;
			}
			if(cc[cnt].acup==c||cc[cnt].bcup==c)
				return cnt;
			cnt++;
		}
		if(temp.acup>0)
		{
			cc[cnt].acup=0;
			cc[cnt].bcup=temp.bcup;
			cc[cnt].dis=temp.dis+1;
			cc[cnt].op=3;
			cc[cnt].fa=temp.id;
			cc[cnt].id=cnt;
			if(!vis[cc[cnt].acup][cc[cnt].bcup])
			{
			   q.push(cc[cnt]);
			   vis[cc[cnt].acup][cc[cnt].bcup]=1;
			}
			if(cc[cnt].acup==c||cc[cnt].bcup==c)
				return cnt;
			cnt++;
		}
		if(temp.bcup>0)
		{
			cc[cnt].bcup=0;
			cc[cnt].acup=temp.acup;
			cc[cnt].dis=temp.dis+1;
			cc[cnt].op=4;
			cc[cnt].fa=temp.id;
			cc[cnt].id=cnt;
			if(!vis[cc[cnt].acup][cc[cnt].bcup])
			{
			   q.push(cc[cnt]);
			   vis[cc[cnt].acup][cc[cnt].bcup]=1;
			}
			if(cc[cnt].acup==c||cc[cnt].bcup==c)
				return cnt;
			cnt++;
		}
		if(temp.acup>0)
		{
			if(temp.acup+temp.bcup>=b)
			{
				cc[cnt].acup=temp.acup+temp.bcup-b;
				cc[cnt].bcup=b;
			}
			else
			{
				cc[cnt].acup=0;
				cc[cnt].bcup=temp.acup+temp.bcup;
			}
			cc[cnt].dis=temp.dis+1;
			cc[cnt].fa=temp.id;
			cc[cnt].id=cnt;
			cc[cnt].op=5;
			if(!vis[cc[cnt].acup][cc[cnt].bcup])
			{
			   q.push(cc[cnt]);
			   vis[cc[cnt].acup][cc[cnt].bcup]=1;
			}
			if(cc[cnt].acup==c||cc[cnt].bcup==c)
				return cnt;
			cnt++;
		}
		if(temp.bcup>0)
		{
			if(temp.acup+temp.bcup>=a)
			{
				cc[cnt].bcup=temp.acup+temp.bcup-a;
				cc[cnt].acup=a;
			}
			else
			{
				cc[cnt].bcup=0;
				cc[cnt].acup=temp.acup+temp.bcup;
			}
			cc[cnt].dis=temp.dis+1;
			cc[cnt].fa=temp.id;
			cc[cnt].id=cnt;
			cc[cnt].op=6;
			if(!vis[cc[cnt].acup][cc[cnt].bcup])
			{
			   q.push(cc[cnt]);
			   vis[cc[cnt].acup][cc[cnt].bcup]=1;
			}
			if(cc[cnt].acup==c||cc[cnt].bcup==c)
				return cnt;
			cnt++;
		}
	}
	return INF;
}
void print(cup u)
{
	if(u.id==0)
		return;
	else print(cc[u.fa]);
	switch(u.op)
	{
	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;
	}
}
int main()
{
	while(scanf("%d %d %d",&a,&b,&c)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		int ans=bfs();
		if(ans==INF)
			printf("impossible\n");
		else
		{
			printf("%d\n",cc[ans].dis);
			print(cc[ans]);
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值