POJ 3414 Pots(宽搜)

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)

题意:给你两个壶的容积,你需要用这两个壶来得到题目所给水的量。
你有六种操作:
FILL(1):灌满水壶1
FILL(2):灌满水壶2
DROP(1):把水壶1的水倒掉
DROP(2):把水壶2的水倒掉
POUR(1,2):把水壶1的水倒入水壶2中(水壶2若倒满了,剩余的水要留在水壶1中)
POUR(2,1):把水壶2的水倒入水壶1中(水壶1若倒满了,剩余的水要留在水壶2中)
你需要求出最少操作次数并把这些操作输出出来,若无解则输出:“impossible”。
题解:BFS,用字符串保存所有操作

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
struct cc{
	string x;//保存过程 
	int step;//保存次数 
	int a,b;//保存两个壶的状态 
};
queue<cc>q;
bool vis[105][105];//记录当前两个壶的状态 
int main()
{
	int a,b,k;
	scanf("%d%d%d",&a,&b,&k);
	cc flag;
	flag.step=-1;
	for(int i=1;i<=6;i++)
	{
		if(i==1)
		{
			q.push((cc){"1",1,a,0});
		}
		if(i==2)
		{
			q.push((cc){"2",1,0,b});
		}
		if(i==3)
		{
			q.push((cc){"3",1,0,0});
		}
		if(i==4)
		{
			q.push((cc){"4",1,0,0});
		}
		if(i==5)
		{
			q.push((cc){"5",1,0,0});
		}
		if(i==6)
		{
			q.push((cc){"6",1,0,0});
		}
	}
	while(!q.empty()) 
	{
		cc u=q.front(); q.pop();
		if(u.a==k||u.b==k)
		{
			flag=u;
			break;
		}
		for(int i=1;i<=6;i++)
		{
			if(i==1)
			{
				if(!vis[a][u.b])
				{
					q.push((cc){u.x+"1",u.step+1,a,u.b});
				    vis[a][u.b]=1;
				}
			}
			if(i==2)
			{
				if(!vis[u.a][b])
				{
					q.push((cc){u.x+"2",u.step+1,u.a,b});
					vis[u.a][b]=1;
				}
			}
			if(i==3)
			{
				if(!vis[0][u.b])
				{
					q.push((cc){u.x+"3",u.step+1,0,u.b});
					vis[0][u.b]=1;
				}
				
			}
			if(i==4)
			{
				if(!vis[u.a][0])
				{
					q.push((cc){u.x+"4",u.step+1,u.a,0});
					vis[u.a][0]=1;
				}
			}
			if(i==5)
			{
				if(u.a<=b-u.b)
				{
					if(!vis[0][u.a+u.b])
					{
						q.push((cc){u.x+"5",u.step+1,0,u.a+u.b});
						vis[0][u.a+u.b]=1;
					}
				}
				else
				{
					if(!vis[u.a-(b-u.b)][b])
					{
						q.push((cc){u.x+"5",u.step+1,u.a-(b-u.b),b});
						vis[u.a-(b-u.b)][b]=1;
					}
				}
			}
			if(i==6)
			{
				if(u.b<=a-u.a)
				{
					if(!vis[u.a+u.b][0])
					{
						q.push((cc){u.x+"6",u.step+1,u.a+u.b,0});
						vis[u.a+u.b][0]=1;
					}
				}
				else
				{
					if(!vis[a][u.b-(a-u.a)])
					{
						q.push((cc){u.x+"6",u.step+1,a,u.b-(a-u.a)});
						vis[a][u.b-(a-u.a)]=1;
					}
				}
			}
		}
	}
	if(flag.step!=-1)
	{
		printf("%d\n",flag.step);
		int l=flag.x.length();
		for(int i=0;i<l;i++)
		{
			if(flag.x[i]=='1')
			{
				printf("FILL(1)\n");
			}
			if(flag.x[i]=='2')
			{
				printf("FILL(2)\n");
			}
			if(flag.x[i]=='3')
			{
				printf("DROP(1)\n");
			}
			if(flag.x[i]=='4')
			{
				printf("DROP(2)\n");
			}
			if(flag.x[i]=='5')
			{
				printf("POUR(1,2)\n");
			}
			if(flag.x[i]=='6')
			{
				printf("POUR(2,1)\n");
			}
		}
	}
	else
	{
		printf("impossible\n");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值