题目
2152:Pots
总时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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).
输出
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)
代码
#include <bits/stdc++.h>
using namespace std;
struct Node{
int a,//容量
b,//水量
x;//记住当次操作
vector<int> op;//步骤
}node,now;//宽搜传递变量
bool k[101][101];//宽搜标记,标明哪个操作已进行
string ope[6]={
"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};//六种操作
int main(){
freopen("data.cpp","r",stdin);
int a,b,c; cin>>a>>b>>c;
queue<Node> q;//宽搜队列
q.push(Node{
0,0,0});k[0][0]=1;//从两空杯开始
bool ans=0;//结果标记
while(!q.empty()){
//队列非空就宽搜
node=q.front();q.pop();//队首,取得出发状态,并从队列清除
//cout<<"始发状态:"<<node.a<<","<<node.b<<endl;
if(node.a==c||node.b==c){
//取得目标水量,达到目的
ans=1;break;
}
for(int i=0;i<6;i++){
//六种操作
now=node;//始发状态变成目标状态,并修改
if(i==0)now.a=a,now.x=i;//FILL(1)
else if(i==1)now.b=b,now.x=i;//FILL(2)
else if(i==2)now.

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



