poj 1606 Jugs(广搜BFS+路径输出)

这篇博客详细解析了POJ 1606 Jugs问题,该问题可以通过广度优先搜索(BFS)策略来解决。博主提供了题目链接以及与POJ 3414题目的相似性分析,并给出了相应的代码实现。
部署运行你感兴趣的模型镜像

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://poj.org/problem?id=1606

此题和poj3414一样:http://blog.csdn.net/u012860063/article/details/37768979


Description

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle. 

You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A. 

A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are 

fill A 
fill B 
empty A 
empty B 
pour A B 
pour B A 
success 

where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished. 

You may assume that the input you are given does have a solution.

Input

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

Output

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

Sample Input

3 5 4 
5 7 3 

Sample Output

fill B 
pour B A 
empty A 
pour B A 
fill B 
pour B A 
success 
fill A 
pour A B 
fill A 
pour A B 
empty B 
pour A B 
success 

代码如下:

#include <iostream>
#include <algorithm>
using namespace std;
#include <cstring>
#include <queue>
#include <stack>
struct cup
{
	int x, y;
	int step;
	int flag;//标记操作
	cup *pre;//记录路径
};
queue<cup>Q;
stack<int>R;
int a, b, e;
int vis[117][117];//标记当前状态是否到达过
int ans;
void BFS(int x, int y)
{
	cup c;
	cup t[317];//目前瓶子里剩余的水量
	c.x = 0, c.y = 0;
	c.flag = 0;
	c.pre = NULL;
	c.step = 0;
	Q.push(c);
	vis[x][y] = 1;
	int count = -1;
	while(!Q.empty())
	{
		count++;
		t[count] = Q.front();
		Q.pop();
		for(int i = 1; i <= 6; i++)
		{
			switch(i)
			{
				case 1:						//fill a
					c.x = a;
					c.y = t[count].y;
					c.flag = 1;
					break;
				case 2:						//fill b
					c.x = t[count].x;
					c.y = b;
					c.flag = 2;
					break;
				case 3:						//drop a
					c.x = 0;
					c.y = t[count].y;
					c.flag = 3;
					break;
				case 4:						//drop b
					c.x = t[count].x;
					c.y = 0;
					c.flag = 4;
					break;
				case 5:						//pour a to b
					if(t[count].x > b-t[count].y)
					{
						c.x = t[count].x-(b-t[count].y);
						c.y = b;
					}
					else
					{
						c.x = 0;
						c.y = t[count].y+t[count].x;
					}
					c.flag = 5;
					break;
				case 6:						//pour b to a
					if(t[count].y > a-t[count].x)
					{
						c.y = t[count].y - (a-t[count].x);
						c.x = a;
					}
					else
					{
						c.x = t[count].x+t[count].y;
						c.y = 0;
					}
					c.flag = 6;
					break;
			}
			if(vis[c.x][c.y])
				continue;
			vis[c.x][c.y] = 1;
			c.step = t[count].step+1;
			c.pre = &t[count];
			if(c.x == e || c.y == e)
			{
				ans = c.step;
				while(c.pre)
				{
					R.push(c.flag);
					c = *c.pre;
				}
				return;
			}
			Q.push(c);
		}
	}
}
void print()
{
	while(!R.empty())
	{
		int i = R.top();
		R.pop();
		switch(i)
		{
			case 1:cout<<"fill A"<<endl;break;
			case 2:cout<<"fill B"<<endl;break;
			case 3:cout<<"empty A"<<endl;break;
			case 4:cout<<"empty B"<<endl;break;
			case 5:cout<<"pour A B"<<endl;break;
			case 6:cout<<"pour B A"<<endl;break;
		}
	}
	cout<<"success"<<endl;
}
int main()
{
	while(cin >>a>>b>>e)
	{
		while(!Q.empty())
			Q.pop();
		while(!R.empty())
			R.pop();
		memset(vis,0,sizeof(vis));
		BFS(0,0);
		print();
	}
	return 0;
}


您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值