(杯子倒水问题)Pots(p3414)

这个题搞了好久,到了快凌晨三点钟了,终于出来了。都是自己太不小心。还真的以为别人的代码是0MS,而我的只是TL的命。


结果是一个vist[][]里写错了一个,搞得我在输出的时候一直打圈圈了,好辛苦。。有收获,只是有一点不值得。哈哈。小心为好。


写得有一点长了,


#include<iostream>
#include<cstdio>
#include<cstring> 
#include<ctype.h>
#include<vector>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std; 

#define N 101

struct you
{
	int a,b;
}
 pre[N][N];   //记录前一个结点
bool vist[N][N];  
int a,b,c;
int fang[N][N];  //记录当前的操作方法,
struct my
{
	int d;
	int p;
	int a,b;
};

int main()
{
	int i,j,k;

	(cin>>a>>b>>c);
	{
		queue<my> q;
		while (!q.empty())
			q.pop();
		my temp;
		temp.d=0;
		temp.a=0;
		temp.b=0;
		temp.p=0;
		memset(vist,false,sizeof(vist));
		vist[0][0]=true;
		q.push(temp);
		my cur;
		for (i=0;i<N;i++)
		{
			for (j=0;j<N;j++)
				pre[i][j].a=i,pre[i][j].b=j;
		}
	
		bool ans=true;
		while (!q.empty())
		{
			temp=q.front();
			q.pop();
			if (temp.a==c||temp.b==c)
			{
				ans=false;
				break;
			}

			if (temp.a==a)
			{
				if (!vist[0][temp.b])
				{
					vist[0][temp.b]=true;
					cur.d=temp.d+1;
					cur.a=0;
					cur.b=temp.b;
					q.push(cur);
					pre[cur.a][cur.b].a=temp.a;
					pre[cur.a][cur.b].b=temp.b;
					fang[cur.a][cur.b]=1;
				}
				
			}
			
			if (temp.b==b)
			{
				if (!vist[temp.a][0])

				{
					vist[temp.a][0]=true;
					cur.d=temp.d+1;
					cur.a=temp.a;
					cur.b=0;
					q.push(cur);
					pre[cur.a][cur.b].a=temp.a;
					pre[cur.a][cur.b].b=temp.b;
					fang[cur.a][cur.b]=2;
				}
			}
			
			if (temp.a==0)
			{
				if (!vist[a][temp.b])
				{
					vist[a][temp.b]=true;
					cur.d=temp.d+1;
					cur.a=a;
					cur.b=temp.b;
					q.push(cur);
					pre[cur.a][cur.b].a=temp.a;
					pre[cur.a][cur.b].b=temp.b;
					fang[cur.a][cur.b]=3;
				}
			}
			if (temp.b==0)
			{
				if (!vist[temp.a][b])
				{
					vist[temp.a][b]=true;
					cur.d=temp.d+1;
					cur.a=temp.a;
					cur.b=b;
					q.push(cur);
					pre[cur.a][cur.b].a=temp.a;
					pre[cur.a][cur.b].b=temp.b;
					fang[cur.a][cur.b]=4;
				}
			}
			
			if (temp.a<a&&temp.b)
			{
				if (temp.a+temp.b<=a)
				{
					if (!vist[temp.a+temp.b][0])
					{
						vist[temp.a+temp.b][0]=true;
						cur.d=temp.d+1;
						cur.a=temp.a+temp.b;
						cur.b=0;
						q.push(cur);
						pre[cur.a][cur.b].a=temp.a;
						pre[cur.a][cur.b].b=temp.b;
						fang[cur.a][cur.b]=5;
					}
				}
				else
				{
					if (!vist[a][temp.b+temp.a-a])
					{
						vist[a][temp.b+temp.a-a]=true;
						cur.d=temp.d+1;
						cur.a=a;
						cur.b=temp.b+temp.a-a;
						q.push(cur);
						pre[cur.a][cur.b].a=temp.a;
						pre[cur.a][cur.b].b=temp.b;
						fang[cur.a][cur.b]=5;
					}
				}
			}
			if (temp.b<b&&temp.a)
			{
				if (temp.a+temp.b<=b)
				{
					if (!vist[0][temp.a+temp.b])
					{
						vist[0][temp.a+temp.b]=true;
						cur.d=temp.d+1;
						cur.b=temp.a+temp.b;
						cur.a=0;
						q.push(cur);
						pre[cur.a][cur.b].a=temp.a;
						pre[cur.a][cur.b].b=temp.b;
						fang[cur.a][cur.b]=6;
					}
				}
				else
				{
					if (!vist[temp.a+temp.b-b][b])
					{
						vist[temp.a+temp.b-b][b]=true;
						//vist[a][temp.b+temp.a-a]=true;         就是这个一个地方啊,,
						cur.d=temp.d+1; 
						cur.b=b;
						cur.a=temp.b+temp.a-b;
						q.push(cur);
						pre[cur.a][cur.b].a=temp.a;
						pre[cur.a][cur.b].b=temp.b;
						fang[cur.a][cur.b]=6;
					}
				}	

			}
			
			
		}
		if (ans)
		{
			cout<<"impossible"<<endl;
			return 0;
		}

		cout<<temp.d<<endl;

		stack<you> p;

		you t;
		t.a=temp.a;
		t.b=temp.b;
		while (!p.empty())
		{
			p.pop();
		}
		
		k=0;
		while ((t.a||t.b))
		{
			//k++;
			p.push(t);
			i=t.a;
			j=t.b;
			t.a=pre[i][j].a;
			t.b=pre[i][j].b;
		}
		//cout<<k<<endl;
		
		while (!p.empty())
		{
			t=p.top();
			p.pop();
			//cout<<t.a<<' '<<t.b<<' '<<fang[t.a][t.b]<<endl;
			switch(fang[t.a][t.b])
			{
				
			case 4: cout<<"FILL(2)"<<endl; break;
			case 3: cout<<"FILL(1)"<<endl; break;
			case 1: cout<<"DROP(1)"<<endl; break;
			case 2: cout<<"DROP(2)"<<endl; break;
			case 5: cout<<"POUR(2,1)"<<endl; break;
			case 6: cout<<"POUR(1,2)"<<endl; break;

				/*
			case 1: cout<<"DROP("<<2<<')'<<endl;break;
			case 2: cout<<"DROP("<<t.b<<')'<<endl; break;
			case 3: cout<<"FILL("<<t.a<<')'<<endl; break;
			case 4: cout<<"FILL("<<t.b<<')'<<endl; break;
			case 5: cout<<"POUR("<<t.a<<','<<t.b<<')'<<endl; break;
				case 6: cout<<"POUR("<<t.b<<','<<t.a<<')'<<endl; break;
				*/

			}
		}

	}

	return 0;
}





Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6621 Accepted: 2782 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 ≤ 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 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 A, B, 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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值