Pots POJ - 3414

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)

**题意:**题意就是用两个杯子a和b,然后两个杯子之间相互倒水,最终任意一个杯子里的水达到c水量游戏结束,求需要的最少步骤和每个具体的步骤。
**题解:**思路是用bfs来搜索每一种可能性,然后将每一个步骤记录下来,最后回溯输出每一个步骤,用vis二维数组来标记a,b杯的水量是否达到过,从而进行每个状态的搜索。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cctype>
#include<iomanip>
#include<map>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<set>
#include<cctype>
#include<string>
#include<stdexcept>
#include<fstream>
#include<sstream>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 10000007
#define debug() puts("what the fuck!")
#define dedebug() puts("what the fuck!!!")
#define ll long long
#define ull unsigned long long
#define speed {ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); };
using namespace std;
const double PI = acos(-1.0);
const int maxn = 1e6 + 50;
const int N = 1e2+50;
const int INF = 0x3f3f3f3f;
const int inf = 0xfffffff;//比INF小,防止累加爆int
const double esp_0 = 1e-6;
const double gold = (1 + sqrt(5)) / 2;
int gcd(int x, int y) {
	return y ? gcd(y, x % y) : x;
}
int vis[N][N];
string str[10] = {"", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)","POUR(2,1)"};
int flag=0;
int a,b,c;
struct node{
	int x,y,step;
	string s;
	node(int x,int y,int step,string s):x(x),y(y),step(step),s(s){};
};
void bfs(){
	queue<node>q;
	vis[0][0]=1;
	q.push(node(0,0,0,"0"));
	while(!q.empty()){
		node temp=q.front();
		q.pop();
		if(temp.x==c||temp.y==c){
			flag=1;
			cout<<temp.step<<endl;
			for(int i=1;i<temp.s.length();++i)
			cout<<str[temp.s[i]-'0']<<endl;
			return;
		}
		if(temp.x<a){//a杯未满 
			if(!vis[a][temp.y]){
				vis[a][temp.y]=1;
				q.push(node(a,temp.y,temp.step+1,temp.s+"1"));
			}
		}
		if(temp.y<b){//b杯未满 
			if(!vis[temp.x][b]){
				vis[temp.x][b]=1;
				q.push(node(temp.x,b,temp.step+1,temp.s+"2"));
			}
		}
		if(temp.x!=0){//a杯不为空,drop 
			if(!vis[0][temp.y]){
				vis[0][temp.y]=1;
				q.push(node(0,temp.y,temp.step+1,temp.s+"3"));
			}
		}
		if(temp.y!=0){//b杯不为空 ,drop 
			if(!vis[temp.x][0]){
				vis[temp.x][0]=1;
				q.push(node(temp.x,0,temp.step+1,temp.s+"4"));
			}
		}
		if(temp.x!=0&&temp.y<b){//a杯不为空,且b杯未满 
			int next_x,next_y;
			if(temp.x<=b-temp.y)next_x=0,next_y=temp.x+temp.y;
			else next_x=temp.x-b+temp.y,next_y=b;
			if(!vis[next_x][next_y]){
				vis[next_x][next_y]=1;
				q.push(node(next_x,next_y,temp.step+1,temp.s+"5"));
			}
		}
		if(temp.y!=0&&temp.x<a){//b杯不为空,且a杯未满 
			int next_x,next_y;
			if(temp.y<=a-temp.x)next_x=temp.y+temp.x,next_y=0;
			else next_x=a,next_y=temp.y-a+temp.x;
			if(!vis[next_x][next_y]){
				vis[next_x][next_y]=1;
				q.push(node(next_x,next_y,temp.step+1,temp.s+"6"));
			}
		}
	}
}
int main(){
	speed;
	cin>>a>>b>>c;
	mem(vis,0);
	bfs();
	if(!flag)puts("impossible");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值