POJ 3414 Pots(寬搜)

Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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)

題意:給你兩個壺的容積,你需要用這兩個壺來得到題目所給水的量。
你有六種操作:
FILL(1):灌滿水壺1
FILL(2):灌滿水壺2
DROP(1):把水壺1的水倒掉
DROP(2):把水壺2的水倒掉
POUR(1,2):把水壺1的水倒入水壺2中(水壺2若倒滿了,剩餘的水要留在水壺1中)
POUR(2,1):把水壺2的水倒入水壺1中(水壺1若倒滿了,剩餘的水要留在水壺2中)
你需要求出最少操作次數並把這些操作輸出出來,若無解則輸出:“impossible”。
題解:BFS,用字符串保存所有操作

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
struct cc{
	string x;//保存過程 
	int step;//保存次數 
	int a,b;//保存兩個壺的狀態 
};
queue<cc>q;
bool vis[105][105];//記錄當前兩個壺的狀態 
int main()
{
	int a,b,k;
	scanf("%d%d%d",&a,&b,&k);
	cc flag;
	flag.step=-1;
	for(int i=1;i<=6;i++)
	{
		if(i==1)
		{
			q.push((cc){"1",1,a,0});
		}
		if(i==2)
		{
			q.push((cc){"2",1,0,b});
		}
		if(i==3)
		{
			q.push((cc){"3",1,0,0});
		}
		if(i==4)
		{
			q.push((cc){"4",1,0,0});
		}
		if(i==5)
		{
			q.push((cc){"5",1,0,0});
		}
		if(i==6)
		{
			q.push((cc){"6",1,0,0});
		}
	}
	while(!q.empty()) 
	{
		cc u=q.front(); q.pop();
		if(u.a==k||u.b==k)
		{
			flag=u;
			break;
		}
		for(int i=1;i<=6;i++)
		{
			if(i==1)
			{
				if(!vis[a][u.b])
				{
					q.push((cc){u.x+"1",u.step+1,a,u.b});
				    vis[a][u.b]=1;
				}
			}
			if(i==2)
			{
				if(!vis[u.a][b])
				{
					q.push((cc){u.x+"2",u.step+1,u.a,b});
					vis[u.a][b]=1;
				}
			}
			if(i==3)
			{
				if(!vis[0][u.b])
				{
					q.push((cc){u.x+"3",u.step+1,0,u.b});
					vis[0][u.b]=1;
				}
				
			}
			if(i==4)
			{
				if(!vis[u.a][0])
				{
					q.push((cc){u.x+"4",u.step+1,u.a,0});
					vis[u.a][0]=1;
				}
			}
			if(i==5)
			{
				if(u.a<=b-u.b)
				{
					if(!vis[0][u.a+u.b])
					{
						q.push((cc){u.x+"5",u.step+1,0,u.a+u.b});
						vis[0][u.a+u.b]=1;
					}
				}
				else
				{
					if(!vis[u.a-(b-u.b)][b])
					{
						q.push((cc){u.x+"5",u.step+1,u.a-(b-u.b),b});
						vis[u.a-(b-u.b)][b]=1;
					}
				}
			}
			if(i==6)
			{
				if(u.b<=a-u.a)
				{
					if(!vis[u.a+u.b][0])
					{
						q.push((cc){u.x+"6",u.step+1,u.a+u.b,0});
						vis[u.a+u.b][0]=1;
					}
				}
				else
				{
					if(!vis[a][u.b-(a-u.a)])
					{
						q.push((cc){u.x+"6",u.step+1,a,u.b-(a-u.a)});
						vis[a][u.b-(a-u.a)]=1;
					}
				}
			}
		}
	}
	if(flag.step!=-1)
	{
		printf("%d\n",flag.step);
		int l=flag.x.length();
		for(int i=0;i<l;i++)
		{
			if(flag.x[i]=='1')
			{
				printf("FILL(1)\n");
			}
			if(flag.x[i]=='2')
			{
				printf("FILL(2)\n");
			}
			if(flag.x[i]=='3')
			{
				printf("DROP(1)\n");
			}
			if(flag.x[i]=='4')
			{
				printf("DROP(2)\n");
			}
			if(flag.x[i]=='5')
			{
				printf("POUR(1,2)\n");
			}
			if(flag.x[i]=='6')
			{
				printf("POUR(2,1)\n");
			}
		}
	}
	else
	{
		printf("impossible\n");
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章