瓶子装水

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

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)

题目大意:给定两个瓶子的容量要求通过三种操作达到目标容量的最少步骤。

扫描二维码关注公众号,回复: 1763533 查看本文章

1.瓶子装满。

2.瓶子倒掉

3.一个瓶子倒进另一个瓶子

直接BFS就可求出最少步骤,较为麻烦的是打印路劲,即将每一步操作打印出来

AC代码:


#include <iostream>
#include <queue>
#include <vector>
#include <map>
#include <utility>
using namespace std;

#define MAX 1000
#define FILL1 1
#define FILL2 2
#define DROP1 3
#define DROP2 4
#define POUR12 5
#define POUR21 6

struct Pots{
	int A;
	int B;
	vector<int> step;		//表示从起始点到当前点都经过了哪些步骤

	Pots(){}
	Pots( int x , int y ){
		A = x;
		B = y;
	}
};
int a,b,C;
bool visited[1000][1000];
int len[1000][1000];		//数组下标(a,b) 表示从初始顶点到(a,b)的距离 

//队列一定要用  引用  下同
void FILL( Pots pp , queue<Pots>& q ){
	//填满A
	Pots temp = pp;
	pp.A = a;
	if( !visited[pp.A][pp.B] ){
		//从前一次的状态到装满A   经过操作FILL1   存到路径里面
		pp.step.push_back(FILL1);
		visited[pp.A][pp.B] = true;
		//将新的状态入队  里面的路径同时也因为队列保存起来
		q.push(pp);
		len[q.back().A][q.back().B] = len[temp.A][temp.B] + 1; 
	}
	//填满B
	pp = temp;
	pp.B = b;
	if( !visited[pp.A][pp.B] ){
		pp.step.push_back(FILL2);
		visited[pp.A][pp.B] = true;
		q.push(pp);
		len[q.back().A][q.back().B] = len[temp.A][temp.B] + 1; 
		
	}
}
void DROP( Pots pp , queue<Pots>& q ){
	//倒掉A;
	Pots temp = pp;
	pp.A = 0;
	if( !visited[pp.A][pp.B] ){
		pp.step.push_back(DROP1);
		visited[pp.A][pp.B] = true;
		q.push(pp);
		len[q.back().A][q.back().B] = len[temp.A][temp.B] + 1; 
		
	}
	//倒掉B
	pp = temp;
	pp.B = 0;
	if( !visited[pp.A][pp.B] ){
		pp.step.push_back(DROP2);
		visited[pp.A][pp.B] = true;
		q.push(pp);
		len[q.back().A][q.back().B] = len[temp.A][temp.B] + 1; 
		
	}
}
void POUR( Pots pp , queue<Pots>& q ){
	Pots temp = pp;
	//A往B里倒
	if( pp.A > 0 ){
		if( pp.A > (b-pp.B) ){
			pp.A = pp.A - (b-pp.B);
			pp.B = b;
		}
		else{
			pp.B = pp.B+pp.A;
			pp.A = 0;
		}
	}
	if( !visited[pp.A][pp.B] ){
		pp.step.push_back(POUR12);
		visited[pp.A][pp.B] = true;
		q.push(pp);
		len[q.back().A][q.back().B] = len[temp.A][temp.B] + 1; 

	}
	//B往A里倒
	pp = temp;
	if( pp.B > 0 ){
		if( pp.B > (a-pp.A) ){
			pp.B = pp.B - (a-pp.A);
			pp.A = a;
		}
		else{
			pp.A += pp.B;
			pp.B = 0;
		}
	}
	if( !visited[pp.A][pp.B] ){
		pp.step.push_back(POUR21);
		visited[pp.A][pp.B] = true;
		q.push(pp);
		len[q.back().A][q.back().B] = len[temp.A][temp.B] + 1; 
	}
}
void println( vector<int> step ){
	if( step.size() > 0 ){
		for( int i = 0 ; i < step.size() ; i ++ ){
			if( step[i] == FILL1 )
				cout << "FILL(1)" << endl;
			else if( step[i] == FILL2 )
				cout << "FILL(2)" << endl;
			else if( step[i] == DROP1 )
				cout << "DROP(1)" << endl;
			else if( step[i] == DROP2 )
				cout << "DROP(2)" << endl;
			else if( step[i] == POUR12 )
				cout << "POUR(1,2)" << endl;
			else
				cout << "POUR(2,1)" << endl;
		}
	}
}
bool bfs( Pots pots , int C ){
	queue<Pots> q;
	q.push(pots);
	visited[pots.A][pots.B] = true;
	while( !q.empty() ){
		Pots pp = q.front();
		q.pop();
		if( pp.A == C || pp.B == C ){
			cout << len[pp.A][pp.B] << endl;
			//当找到了A==C  或 B == C 时   此时这个结构体中的路径数组保存了从状态(0,0)到状态(A,B)
			//经过的路径  按顺序打印出就好
			println( pp.step );
			return true;
		}
		FILL(pp,q); 
		DROP(pp,q);
		POUR(pp,q);

	}
	return false;
}
int main()
{

	Pots p;
	cin >> a >> b >> C;
	p.A = 0;
	p.B = 0;
	if( !bfs(p,C) )
		cout << "impossible" << endl;
	return 0;
}


猜你喜欢

转载自blog.csdn.net/a673786103/article/details/79726158