zoj 1005 Jugs

zoj 1005 Jugs

/*
今天的ZOJ好卡。。。
一道题提交后几分钟才出来结果
直接模拟即可
大致过程是这样的
如果goal是偶数就先fill B
然后就是把B倒进A
满的话就清空A
然后再把B倒进A
满的话就清空A
直到B空后fill B重复上述过程
或者B中的水==goal时直接跳出
goal是偶数时反过来即可
但是要注意以下几点
按照sample output的提示
应该是只有B中的水==goal时才跳出
还有就是要注意A的容积小于B
所以会出现从B往A中倒水时A满清空了几次
还会出现B中剩余的水大于A的容积的情况
还有一些其他的细节不再一一详述
*/
#define LOCAL
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<iomanip>
#include<string>
#include<algorithm>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
int main()
{
#ifdef LOCAL
       freopen("input.txt","r",stdin);
       freopen("output.txt","w",stdout);
#endif
 
	   int a,b,ca,cb,goal;
	   while(cin>>ca>>cb>>goal)
	   {
			a=b=0;
			if(goal%2==1)
		   {
			   while(1)
				{
					a=ca;puts("fill A");
					if(a+b<cb)
					{
						b+=a;puts("pour A B");
						if(b==goal){puts("success");goto out;;}
						a=0;
					}
					else
					{
						a=a+b-cb;puts("pour A B");
						b=cb;
						if(b==goal){puts("success");goto out;;}
						b=0;puts("empty B");
						if(b==goal){puts("success");goto out;;}
						b+=a;puts("pour A B");
						if(b==goal){puts("success");goto out;;}
						a=0;
					}
			   }
			}
			else
			{
				while(1)
				{
					b=cb;puts("fill B");
					if(b==goal){puts("success");goto out;;}
					if(b+a<ca)
					{
						a+=b;puts("pour B A");b=0;
						if(b==goal){puts("success");goto out;;}
					}
					else
					{
						while(b+a>goal)
						{
							b-=(ca-a);puts("pour B A");a=ca;
							if(b==goal){puts("success");goto out;}
							a=0;puts("empty A");
						}
						a=b;puts("pour B A");
						b=0;
						if(b==goal){puts("success");goto out;}
					}
				}
			}
			out:;
	   }
		return 0;
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注