Pots POJ - 3414 (bfs+记录路径)

两杯水问题求解
本文探讨了使用两种不同容量的容器如何通过一系列操作得到指定容量的水的问题。通过广度优先搜索算法寻找最短的操作路径,并详细展示了算法的具体实现过程。

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

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
int a,b,c,vis[106][106];
struct nomd 
{
	int a,b,num,f;nomd *pre;
};
int ans=-1;
stack<int >r;
void bfs(){
	memset(vis,0,sizeof(vis));
	nomd e;nomd w[1000];
	e.a=0;e.b=0;e.num=0;e.f=0;e.pre=NULL;
	vis[0][0]=1;
	queue<nomd> que;
	que.push(e);
	int count=-1;
	while(!que.empty())
	{
		w[++count]=que.front();
		que.pop();
		if(w[count].a==c||w[count].b==c)
		{
			ans=w[count].num;
			while(w[count].pre)
			{
				r.push(w[count].f);
				w[count]= *w[count].pre;
			}
			break;
		}
		for(int i=1;i<=6;i++)
		{
		   e=w[count];
			if(i==1)
			{
			 e.a=a;
			}
			if(i==2){
				e.b=b;
			}
			if(i==3)
			{
			  e.a=0;
			}
			if(i==4){
				e.b=0;
			}
			if(i==5){
				e.a=w[count].a-(b-w[count].b);e.b=w[count].a+w[count].b;
				if(e.b>b)  e.b=b;
				if(e.a<0) e.a=0;
			}
			if(i==6){
				e.b=w[count].b-(a-w[count].a);e.a=w[count].a+w[count].b;
				if(e.a>a) e.a=a;
				if(e.b<0) e.b=0;
			}
			if(vis[e.a][e.b]==1) continue;
			e.f=i; e.pre=&w[count];
			e.num=w[count].num+1;
			vis[e.a][e.b]=1;
			que.push(e);
		}
		
	}
}
int main()
{

	scanf("%d %d %d",&a,&b,&c);
	bfs();
//	for(int i=0;i<=a;i++)
//	{
//		for(int j=0;j<=b;j++)
//		printf("%d ",vis[i][j]);
//		printf("\n");
//	}
	if(ans==-1)
	{
		printf("impossible\n");
	}
	else
	printf("%d\n",ans);
	while(!r.empty())
	{
		int i = r.top();
		r.pop();
		switch(i)
		{
			case 1:cout<<"FILL(1)"<<endl;break;
			case 2:cout<<"FILL(2)"<<endl;break;
			case 3:cout<<"DROP(1)"<<endl;break;
			case 4:cout<<"DROP(2)"<<endl;break;
			case 5:cout<<"POUR(1,2)"<<endl;break;
			case 6:cout<<"POUR(2,1)"<<endl;break;
		}
	}
	return 0;
}

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)

这道题的意思是两个杯子6种操作,1.将a装满 2.将b装满 3.将a倒光 4.将b倒光 5.将a倒入b 6 将b倒入a.

主要是记录路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值