poj3414 Pots

 

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 ≤ ≤ 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 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)

 

 该题大意:输入A,B的容水量,然后再输入一个整数表示最终A或B内的水;刚开始A和B内斗没有水;输出倒得次数,然后输出倒得路线。

  广搜题,该题一共有六种情况,把A装满;把B装满;把A内的水放光;把B内的水放光;把A的水放到B中;把B的水放到A中。
  每次斗扩展六种情况,最后输出。

  写的略繁琐。。。。。


#include <stdio.h>
const int max = 110;

struct node
{
	int x,y;
}queue[max*max];
int vis[max][max];
int shortest[max][max];
int pre[max][max][2];
int record[max * max];
void bfs(int x0,int y0,int z)
{
	
	int fr=0,end=0;
	queue[end].x=0;
	queue[end].y=0;
	end++;
    vis[0][0]=7;
    int tx,ty;
    int flag=0;
	while(fr<end)
	{
		node now=queue[fr];
		fr++;
		if(now.x==z||now.y==z)
		{
			printf("%d\n",shortest[now.x][now.y]);
			tx=now.x;
			ty=now.y;
			flag=1;
			break;
		}
	    for(int i=1;i<=6;i++)
	   {
	      int x1,y1;	   	   
		   if(i==1)
		   {
             x1=x0;
             y1=now.y;
             
		   }
		   if(i==2)
		   {
             x1=now.x;
             y1=y0;
             
		   }
		   if(i==3)
		   {
		   	 x1=0;
		   	 y1=now.y;
		   	 
		   }
		   if(i==4)
		   {
		   	 x1=now.x;
		   	 y1=0;
		   	 
		   }
		   if(i==5)
		   {
             int x2=now.x+now.y;
             if(x2>x0)
             {
             	x1=x0;
             	y1=x2-x0;
             }
             else 
             {
             	x1=x2;
             	y1=0;
             }             
		   }
		   if(i==6)
		   {
		   	 int y2=now.y+now.x;
		   	 if(y2>y0)
		   	 {
		   	 	y1=y0;
		   	 	x1=y2-y0;
		   	 }
		   	 else
		   	 {
		   	 	y1=y2;
		   	 	x1=0;
		   	 }		   	 
		   }
		   if(vis[x1][y1])continue;
		   vis[x1][y1]=i;
		    
		     queue[end].x=x1;
		     queue[end].y=y1;
		     end++;
		     shortest[x1][y1]=shortest[now.x][now.y]+1;
		     pre[x1][y1][0]=now.x;
		     pre[x1][y1][1]=now.y;
		     x1=now.x;
	   	     y1=now.y;
	   }
	  
	}
	if(!flag)
	{
		printf("impossible\n");
	}
	else
	{
	 int x2=tx,y2=ty;
	 int total=0;
	 int record[max*max];
	 while(pre[x2][y2][0]!=-1)
	 {
       record[total]=vis[x2][y2];
       int a=pre[x2][y2][0];
       int b=pre[x2][y2][1];
       x2=a;
       y2=b;
       total++;
	 }
	 for(int i=total-1;i>=0;i--)
	 {
		int k=record[i];
		if(k==1)
		{
			printf("FILL(1)\n");
		}
		else if(k==2)
		{
			printf("FILL(2)\n");
		}
		else if(k==3)
		{
			printf("DROP(1)\n");
		}
		else if(k==4)
		{
			printf("DROP(2)\n");
		}
		else if(k==5)
		{
			printf("POUR(2,1)\n");
		}
		else 
		{
			printf("POUR(1,2)\n");
		}
	 }
	}
}
int main()
{
	int n,m,z;
	while(scanf("%d%d%d",&n,&m,&z)!=EOF)
	{
		for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=m;j++)
		{
			pre[i][j][0]=pre[i][j][1]=-1;
			vis[i][j]=0;
		}
	}
		bfs(n,m,z);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值