POJ3414

 
//有两个空杯子一二,容量分别为aL,bL,现在给你一个数c;
//问是否可以用一二两个杯子 倒出cL的水,输出最少步骤的倒水的过程
//如果不能就输出impossible 
//有六种情况,	a倒进b中,或b倒进a中 
// 				a清空或b清空
// 				a倒满或b倒满 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a,b,c,k,flag;int book[110][110];int step1[1000];int step2[1000];void dfs(int x,int y,int step)
{
	int tx,ty;
	
	if(step >= k)return;
	
	if(x == c || y == c)
	{
		k = step;
		flag = 1;
		for(int i = 0; i < k; i++)
			step2[i] = step1[i];		//记录步骤 
		return ;
	}
	
	
	for(int i = 0; i < 6; i++)
	{
		if(i == 0)	    {	tx = a;		ty = y;		}       //水杯一倒满 
		
		else if(i == 1)	{	tx = x;		ty = b;		}	//水杯二倒满 
		
		else if(i == 2)	{	tx = 0;		ty = y;		}    	//水杯一清空 
		
		else if(i == 3)	{	tx = x;		ty = 0;		}	//水杯二清空 
		
		else if(i == 4)
		{
			if(x + y <= b)	    {	ty = x + y;     tx = 0;	}	  //把水杯一中的水倒进水杯二中且不会溢出 
			else                {	ty = b;         tx = x + y - b;	 }//如果溢出 
		}
		
		else if(i == 5)
		{
			if( x + y <= a)		{  tx = x + y;	 ty = 0;  }	   //把水杯二中的水倒进水杯一中且不会溢出 
			else 			{  tx = a; 	 ty = x + y - a;  }//如果溢出 
		}
			
		if(!book[tx][ty])
		{
			book[tx][ty] = 1;
			step1[step] = i;				//存储步骤 
			dfs(tx,ty,step+1);
			book[tx][ty] = 0;
		}
	}
	return ;
}
int main()
{
	while(~scanf("%d%d%d",&a,&b,&c))
	{
		memset(book,0,sizeof(book));
		k = 999999;
		flag = 0;
		book[0][0] = 1;
		dfs(0,0,0);
		if(flag)
		{
			printf("%d\n",k);
			for(int i = 0; i < k; i++)
			{
				if(step2[i] == 0)printf("FILL(1)\n");
				else if(step2[i] == 1)printf("FILL(2)\n");
				else if(step2[i] == 2)printf("DROP(1)\n");
				else if(step2[i] == 3)printf("DROP(2)\n");
				else if(step2[i] == 4)printf("POUR(1,2)\n");
				else if(step2[i] == 5)printf("POUR(2,1)\n");
			}
		}
		else printf("impossible\n");
	}
	return 0;
}
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值