POJ - 3414 Pots


                                                                                                      Pots

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 operationsK. 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,需要称量出c体积,输出需要几步,和每步做法,FILL(1)把a杯装满

FILL(2)把b杯装满

DROP(1)把a杯清空

DROP(2)把b杯清空;

POUR(1,2)把a杯的水到如b杯中

POUR(2,1)把b杯的水到如a杯中;

数据较简单,广搜和神搜都可以。

深搜:

#include<stdio.h>
#include<string.h>
int s[1000],book[110][110],s1[1000];
int a,b,c,flag,k;

void dfs(int x,int y,int step)
{
	if(step>=k)
	return ;
	if(x==c||y==c)
	{
		k=step;
		for(int i=0;i<k;i++)
		s1[i]=s[i];
		flag=1;
		return ;
	}
	int tx,ty;
	for(int i=1;i<=6;i++)
	{
		if(i==1)
		tx=a,ty=y;
	    else if(i==2)
	    tx=x,ty=b;
	    else if(i==3)
	    tx=0,ty=y;
	    else if(i==4)
	    tx=x,ty=0;
	    else if(i==5)
	    {
	    	if(x+y<=b)
			ty=x+y,tx=0;
			else ty=b,tx=x+y-b;
		}
		else if(i==6)
		{
			if(x+y<=a)
			tx=x+y,ty=0;
			else tx=a,ty=x+y-a;
		}
		if(!book[tx][ty])
		{
			book[tx][ty]=1;
			s[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));
		book[0][0]=1;k=99999;flag=0;
		dfs(0,0,0);
	    if(flag)
	    {
			printf("%d\n",k);
			for(int i=0;i<k;i++)
			{
				if(s1[i]==1)
				printf("FILL(1)\n");
				else if(s1[i]==2)printf("FILL(2)\n");
				else if(s1[i]==3)printf("DROP(1)\n");
				else if(s1[i]==4)printf("DROP(2)\n");
				else if(s1[i]==5)printf("POUR(1,2)\n");
				else if(s1[i]==6)printf("POUR(2,1)\n");
			}
		}
		else printf("impossible\n");
	}
	return 0;
}

广搜:


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
	
int n,m,t,book[110][110];
int flag;	

struct note
{
	int x;
	int y;
	int step;
	int s[1011];
};


void bfs()
{
	queue <note> Q ;
	note p,q;
	int tx,ty;
    p.x=0;
    p.y=0;
	p.step=0;
	book[p.x][p.y]=1;
	Q.push(p);
	while(!Q.empty())
	{
		p=Q.front();
		q=p;
		Q.pop();
		if(p.x==t||p.y==t)
		{
			printf("%d\n",p.step);
			for(int i=1;i<=p.step;i++)
			{
				if(p.s[i]==1)
				printf("FILL(1)\n");
				else if(p.s[i]==2)printf("FILL(2)\n");
				else if(p.s[i]==3)printf("DROP(1)\n");
				else if(p.s[i]==4)printf("DROP(2)\n");
				else if(p.s[i]==5)printf("POUR(1,2)\n");
				else if(p.s[i]==6)printf("POUR(2,1)\n");
			}
			flag=1;
			break;
		}
		for(int i=1;i<=6;i++)
		{
			if(i==1)
			tx=n,ty=p.y;
			else if(i==2)
			tx=p.x,ty=m;
			else if(i==3)
			tx=0,ty=p.y;
			else if(i==4)
			tx=p.x,ty=0;
			else if(i==5)
			{
				if(p.x+p.y<=m)
				ty=p.x+p.y,tx=0;
				else ty=m,tx=p.x+p.y-m;
			}else if(i==6)
			{
				if(p.x+p.y<=n)
				tx=p.x+p.y,ty=0;
				else tx=n,ty=p.x+p.y-n;
			}
			if(book[tx][ty])
			continue;
			book[tx][ty]=1;
			q.x=tx;
			q.y=ty;
			q.step=p.step+1;
			q.s[q.step]=i;
	      Q.push(q);
		}
	}
	return ;
}
int main()
{
	while(~scanf("%d %d %d",&n,&m,&t))
	{
		memset(book,0,sizeof(book));
		flag=0;
	    bfs();
	    if(!flag)
	    printf("impossible\n");
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值