H - Pots

You are given two pots, having the volume of Aand 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.

Input

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

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的空罐,可以进行6种操作,把一个罐灌满,把一个罐倒光,把一个罐往另一个罐中倒(一个罐另一个罐有余,或者一个罐空),问当其中一个罐的水量达到C的最小操作步骤

思路:bfs找出所有过程,用一个结构体数组v记录点(x,y)的步数sp,是由哪个点(fromX,fromY)操作过来的,上个点的操作是什么op,根据这个回去找每一步的点和操作,用栈存储然后输出

而不是把这些都记录在bfs的结构体中,之前就这么做然后Runtime Error。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
const int MAX = 1e5+50;
int A,B,C;
struct visit{
	int fromX,fromY,sp,op;
}v[205][205];
struct node{
	int a,b;
	node(int _a,int _b):
		a(_a), b(_b){}
};
void up(int x,int y,int fx,int fy,int op){
	v[x][y].sp=v[fx][fy].sp+1;
	v[x][y].fromX=fx;
	v[x][y].fromY=fy;
	v[x][y].op=op;
}
queue<node> q;
int main(){
	cin>>A>>B>>C;
	node p(0,0);
	q.push(p);
	while(q.size()){
		p=q.front();
		if(p.a==C || p.b==C){
			break;
		}
		q.pop();
		if(p.a!=A && !v[A][p.b].sp){//F(1)->1
			up(A,p.b,p.a,p.b,1);
			q.push(node(A,p.b));
		}
		if(p.b!=B && !v[p.a][B].sp){//F(2)->2
			up(p.a,B,p.a,p.b,2);
			q.push(node(p.a,B));
		}
		if(p.a!=0 && !v[0][p.b].sp){//D(1)->3
			up(0,p.b,p.a,p.b,3);
			q.push(node(0,p.b));
		}
		if(p.b!=0 && !v[p.a][0].sp){//D(2)->4
			up(p.a,0,p.a,p.b,4);
			q.push(node(p.a,0));
		}
		if(p.a!=0){//P(1,2)->5
			int aa,bb; 
			if(p.a+p.b<=B){
				aa=0; bb=p.a+p.b;
			}
			else{
				aa=p.a+p.b-B; bb=B;
			}
			if(!v[aa][bb].sp){
				up(aa,bb,p.a,p.b,5);
				q.push(node(aa,bb));	
			}
		}
		if(p.b!=0){//P(2,1)->6
			int aa,bb; 
			if(p.a+p.b<=A){
				aa=p.a+p.b; bb=0;
			}
			else{
				aa=A; bb=p.a+p.b-A;
			}
			if(!v[aa][bb].sp){
				up(aa,bb,p.a,p.b,6);
				q.push(node(aa,bb));	
			}
		}
	}
	if(q.size()){
		int a=p.a,b=p.b,x,y;
		int sp=v[a][b].sp;
		cout<<sp<<"\n";
		stack<int> st;
		for(int i=0;i<sp;i++){
			st.push(v[a][b].op);
			x=a; y=b;
			a=v[x][y].fromX; b=v[x][y].fromY;
		}
		while(st.size()){
			int op=st.top();
			st.pop();
			if(op==1) printf("FILL(1)");
			else if(op==2) printf("FILL(2)");
			else if(op==3) printf("DROP(1)");
			else if(op==4) printf("DROP(2)");
			else if(op==5) printf("POUR(1,2)");
			else if(op==6) printf("POUR(2,1)");
			printf("\n");
		}
	}
	else printf("impossible\n");
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值