POJ 3414-Pots

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21559 Accepted: 9205 Special Judge

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)

Source

Northeastern Europe 2002, Western Subregion

             emmm,就是告诉你有两个最大容积分别为a,b的杯子,请问经过一系列操纵能不能让某个杯子容积是c状态~~操作有倒满,清空,把一个杯子中的水倒到另一个里面,溢出的不倒进去;

             其实就是一个模拟嘛~用了个BFS加路径的存储,我傻子一样开一个一个六位数的数组,将一个六位数的前三位代表a中的容量,后三位代表b中的容量,(ps其实开一个两维就是了~~);


#include<iostream>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
using namespace std;
int sta[500500];
int path[500500];
int a, b, c;
int minn(int a, int b)
{
	if (a < b)
	{
		return a;
	}
	return b;
}
void init()
{
	for (int s = 0; s < 100400; s++)
	{
		sta[s] = -1;
	}
}
int hh(int li, int ri,int now)
{
	if (now == 0)
	{
		return a * 1000 + ri;
	}
	if (now == 1)
	{
		return li * 1000 + b;
	}
	if (now == 2)
	{
		int h=ri;
		if (li + h > a)
		{
			h = a - li;
		}
		li += h;
		ri -= h;
		return li * 1000 + ri;
	}
	if (now == 3)
	{
		int h = li;
		if (ri + h > b)
		{
			h = b - ri;
		}
		ri += h;
		li -= h;
		return li * 1000 + ri;
	}
	if (now == 4)
	{
		return ri;
	}
	return li * 1000;
}
int da(int x)
{
	if (x == 0)
	{
		return 1;
	}
	if (x == 1)
	{
		return 10;
	}
	return 100;
}
int main()
{
	while (scanf("%d%d%d", &a, &b, &c)!= EOF)
	{
		init();
		queue<int>q;
		q.push(0);
		sta[0] = -2;
		int spot = 0;
		int last;
		while (!q.empty())
		{
			int t = q.front();
			int tem = t;
			q.pop();
			int fir = 0, sec = 0;
			for (int s = 0; s < 3; s++)
			{
				sec = sec + da(s)*(t % 10);
				t /= 10;
			}
			for (int s = 0; s < 3; s++)
			{
				fir = fir + da(s)*(t % 10);
				t /= 10;
			}
	//		cout << fir << " " << sec << endl;
			if (fir == c || sec == c)
			{
				spot = 1;
				last = tem;
				break;
			}
			for (int s = 0; s < 6; s++)
			{
				int k = hh(fir, sec, s);
				if (sta[k] == -1)
				{	
					q.push(k);
					sta[k] = tem;
					path[k] = s;
		//			cout << k << " " << tem << " "<< s <<endl;
		//			cout << "                 asdasdsad  " << path[0] << endl;
				}
			}
		}
		if (spot == 0)
		{
			cout << "impossible" << endl;
		}
		else
		{
			stack<string>qu;
			int sum = 0;
			int k = last;
			while (k != -2)
			{
				sum++;
				string l;
				if (path[k] == 0)
				{
					l = "FILL(1)";
				}
				if (path[k] == 1)
				{
					l = "FILL(2)";
				}
				if (path[k] == 2)
				{
					l = "POUR(2,1)";
				}
				if (path[k] == 3)
				{
					l = "POUR(1,2)";
				}
				if (path[k] == 4)
				{
					l = "DROP(1)";
				}
				if (path[k] == 5)
				{
					l = "DROP(2)";
				}	
				qu.push(l);
				k = sta[k];
			}
			sum--;
			qu.pop();
			cout << sum << endl;
			while (!qu.empty())
			{
				cout << qu.top() << endl;
				qu.pop();
			}
		//	cout << path[0] << endl;
		//	cout << hh(0, 0, path[0]) << endl;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值