[广搜BFS] Pots

描述

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 ≤ ≤ 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.

输入

On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

输出

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’.

样例输入

3 5 4

样例输出

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
解题分析

首先,我们观察一下题目,可以发现,题目对某一个状态,给出了一些操作,让我们去求一个最小的操作次数,达到一个指定的目标状态。那么很明显了基本上,这就是一道广搜BFS的题目,关键在于,我们如何去实现题目所提到的这些操作,如何去设置我们的访问数组来表示当前这个状态我们已经达到过了。其实比较容易,我们设置节点Node里面包含一个字符串记录我们的操作,然后两个数A,B记录我们这个节点的情况下,A,B容器中水的含量。而水的含量直接也成为我们的状态,我们设置一个二维数组去标记我们的状态,如果已经访问过了,我们在后续就不需要再放入队列了。最后我们数一数字符串中操作的次数,或者说我们还可以进一步地在Node节点里面设置一个step变量记录我们的操作长度,这都是可以的。最后我们处理一下操作的字符串,输出答案即可。

代码演示
#include <iostream>
#include <cmath>
//#include <iomanip>
#include <string>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>
#include <queue>
#include <stack>
#include <cstdlib>
using namespace std;
int A,B,C;
char op[]={'F','D','P'};
string ans="no";
bitset<105> visited[105];

struct Node{
	string seq;
	int A,B;
	Node(string s="",int a=0,int b=0) : seq{s} , A(a) , B(b) { }
};

void bfs(){
	queue<Node> q;
	q.push({"",0,0});
	char c;
	while(!q.empty()){
		Node tmp=q.front();
		q.pop();
		visited[tmp.A][tmp.B]=1;
		if(tmp.A==C || tmp.B==C){
			ans=tmp.seq;
			break;
		}
		for(int i=0;i<3;i++){
			c=op[i];
			string nseq=tmp.seq+c;
			int nA,nB;
			if(c=='F'){
				for(int j=1;j<=2;j++){
					nA=tmp.A;
					nB=tmp.B;
					string m=nseq+char(j+'0');
					if(j==1) nA=A;
					else nB=B;
					if(!visited[nA][nB])
					q.push({m,nA,nB});
				}
			}
			else if(c=='D'){
				for(int j=1;j<=2;j++){
					nA=tmp.A;
					nB=tmp.B;
					string m=nseq+char(j+'0');
					if(j==1) nA=0;
					else nB=0;
					if(!visited[nA][nB])
					q.push({m,nA,nB});
				}
			}
			else if(c=='P'){
				for(int i=1;i<=2;i++)
					for(int j=1;j<=2;j++){
						if(i==j) continue;
						if(i==1 && j==2){
							string m = nseq + char(i+'0') + char(j+'0');
							nB=tmp.B+tmp.A;
							nA=0;
							if(nB>B){
								nA=nB-B;
								nB=B;
							}
							if(!visited[nA][nB])
							q.push({m,nA,nB});
						}
						else if(i==2 && j==1){
							string m = nseq + char(i+'0') + char(j+'0');
							nA=tmp.B+tmp.A;
							nB=0;
							if(nA>A){
								nB=nA-A;
								nA=A;
							}
							if(!visited[nA][nB])
							q.push({m,nA,nB});
						}
					}
			}
		}
	}
}


int main(){
	scanf("%d%d%d",&A,&B,&C);
	bfs();
	int len=ans.size();
	char a,b;
	if(ans!="no"){
		int count=0;
	for(int i=0;i<len;i++){
		if(!isdigit(ans[i])){
			count++;
		}
	}
		printf("%d\n",count);
	for(int i=0;i<len;i++){
		if(ans[i]=='F'){
			a=ans[++i];
			printf("FILL(%d)\n",a-'0');
		}
		else if(ans[i]=='P'){
			a=ans[++i];
			b=ans[++i];
			printf("POUR(%d,%d)\n",a-'0',b-'0');
		}
		else if(ans[i]=='D'){
			a=ans[++i];
			printf("DROP(%d)\n",a-'0');
		}
	}
	}
	else printf("impossible\n");
	return 0;
}

  • 20
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值