POJ 3414 pots

Description Special Judge

You are given two pots, having the volume of AandB 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 potj 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 potj).

Write a program to find the shortest possible sequenceof these operations that will yield exactlyC liters of water in one ofthe 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 andC≤max(A,B).

Output

The first line of the output must contain the lengthof the sequence of operationsK. The following K lines must eachdescribe 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 andonly 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的1、2号杯子,通过装满1或2,将1中水倒入2中,2中水倒入1中,将1中水倒掉,将2中水倒掉,六个操作,来得到C升水。

方法:广度优先搜索。觉得自己的方法有些复杂,又用了数组又用了队列,感觉两者功能上有些重复了,可以进一步简化。反正大概就是这样嘛。

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;

struct operate
{
	int num1;
	int num2;
	int op;
	int front;
}pot[100001];

int flag[110][110], j = 1;

queue<int> Q;

void solve(int x, int y, int z, int m)
{
	if(flag[x][y])
		return;
	flag[x][y] = 1;
	pot[j].num1 = x;
	pot[j].num2 = y;
	pot[j].op = z;
	pot[j].front = m;
	Q.push(j);
	j++;
};

void Printf(int x)
{
	int count = 0;
	int op[100001];
	while(x!=0)
	{
		op[count++] = pot[x].op;
		x = pot[x].front;
	}
	printf("%d\n",count);
	for(int i = count - 1;i>=0;i--)
	{
		if(op[i]==1)
		{
			printf("FILL(1)\n");
		}
		else if(op[i]==2)
		{
			printf("FILL(2)\n");
		}
		else if(op[i]==3)
		{
			printf("DROP(1)\n");
		}
		else if(op[i]==4)
		{
			printf("DROP(2)\n");
		}
		else if(op[i]==5)
		{
			printf("POUR(2,1)\n");
		}
		else if(op[i]==6)
		{
			printf("POUR(1,2)\n");
		}
	}
};

int BFS(int a,int b, int c)
{
	
	pot[0].num1 = 0;
	pot[0].num2 = 0;
	pot[0].op = 0;
	pot[0].front = -1;
	flag[0][0] = 1;
	Q.push(0);
	while(!Q.empty())
	{
		int temp  = Q.front();
		Q.pop();
		if(pot[temp].num1==c||pot[temp].num2==c)
		{
			Printf(temp);
			return 1;
		}

		int x, y;
		for(int k = 1;k<=6;k++)
		{
			switch(k)
			{
			case 1:
				x = a;
				y = pot[temp].num2;
				solve(x,y,k,temp);	
				break;
			case 2:
				x = pot[temp].num1;
				y = b;
				solve(x,y,k,temp);
				break;
			case 3:
				x = 0;
				y = pot[temp].num2;
				solve(x,y,k,temp);
				break;
			case 4:
				x = pot[temp].num1;
				y = 0;
				solve(x,y,k,temp);
				break;
			case 5:
				if(pot[temp].num1+pot[temp].num2>a)
				{
					x = a;
					y = pot[temp].num2 + pot[temp].num1 - a;
				}
				else
				{
					x = pot[temp].num1+pot[temp].num2;
					y = 0;
				}
				solve(x,y,k,temp);
				break;
			case 6:
				if(pot[temp].num1+pot[temp].num2>b)
				{
					x = pot[temp].num1 - b + pot[temp].num2;
					y = b;
				}
				else
				{
					x = 0;
					y = pot[temp].num1+pot[temp].num2;
				}
				solve(x,y,k,temp);
				break;
			}
		}
	}
	return -1;
};

int main()
{
	int A, B, C;
	int ans;
	scanf("%d%d%d",&A, &B, &C);
	ans = BFS(A, B, C);
	if(ans==-1)
	{
		printf("impossible\n");
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值