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.

主要是记录路径

猜你喜欢

转载自blog.csdn.net/liuliu2333/article/details/81428564