http://noi.openjudge.cn/_2.5基本算法之搜索_2152:Pots
文章目录
- 题目
- 代码
- 结果
- 过程
- 小结
- 丑代码
题目
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.a=0,now.x=i;//DROP(1)else if(i==3)now.b=0,now.x=i;//DROP(2)else if(i==4){//POUR(1,2)if(now.a<=b-now.b)now.b+=now.a,now.a=0;//a水装满b杯,不富裕 else now.a-=b-now.b,now.b=b;//有剩余 now.x=i;//记住取得该状态的操作 }else if(i==5){//POUR(2,1)if(a-now.a>=now.b)now.a+=now.b,now.b=0;//两步顺序不能错else now.b-=a-now.a,now.a=a;now.x=i;}//cout<<now.x<<"操作"<<now.a<<","<<now.b<<"\t";if(k[now.a][now.b])continue;//同一状态不重复操作 k[now.a][now.b]=1;//宽搜标记 now.op.push_back(now.x);q.push(now);}cout<<endl;}if(ans){cout<<node.op.size()<<endl;for(int i=0;i<node.op.size();i++)cout<<ope[node.op[i]]<<endl;}else cout<<"impossible"; return 0;
}
结果
过程
小结
- 关键是完成状态之间的转移,剩余的交给枚举
- now.b-=a-now.a,now.a=a;两步先后顺序错,找了好久
丑代码
#include <bits/stdc++.h>
using namespace std;
struct operation{int mv[2],//容量 v[2],//水量 c;//目标水量 vector<string> s; operation(){memset(mv,0,sizeof(mv));memset(v,0,sizeof(v));}operation(int a,int b,int cx){mv[0]=a,mv[1]=b,c=cx;v[0]=v[1]=0;}bool ok(){return v[0]==c||v[1]==c;}void view(string s,int x,int y){if(x!=-1){if(y==-1)cout<<s<<":"<<x<<endl;else cout<<s<<":从"<<x<<"到"<<y<<endl; }else cout<<"-------------------"<<s<<endl;cout<<"c:"<<c<<"\ta="<<v[0]<<",b="<<v[1]<<endl;}
}op;
bool k[101][101];int main(){freopen("data.cpp","r",stdin);int a,b,c;cin>>a>>b>>c;operation ope=operation{a,b,c};queue<operation> q;q.push(ope);k[0][0]=1;bool ans=0;while(!q.empty()){ope=q.front();q.pop();if(ope.ok()){ans=1;break;}for(int o=0;o<3;o++){op.view("始发",-1,-1);if(o==0){for(int i=0;i<2;i++){op=ope;if(op.v[i]==op.mv[i])continue;else{op.v[i]=op.mv[i];if(k[op.v[0]][op.v[1]])continue;k[op.v[0]][op.v[1]]=1;op.view("fill",i,-1);op.s.push_back("FILL("+string(1,char(i+1+'0'))+")");q.push(op);} }}else if(o==1){for(int i=0;i<2;i++){op=ope;if(op.v[i]==0)continue;else {op.v[i]=0;if(k[op.v[0]][op.v[1]])continue;k[op.v[0]][op.v[1]]=1;op.view("drop",i,-1);op.s.push_back("DROP("+string(1,char(i+1+'0'))+")");q.push(op);} }}else{for(int i=0;i<2;i++){op=ope;int j=1-i;if(op.v[i]>0){if(op.mv[j]-op.v[j]>=op.v[i])op.v[j]+=op.v[i],op.v[i]=0;else op.v[i]-=op.mv[j]-op.v[j],op.v[j]=op.mv[j];op.view("pour",i,j);if(k[op.v[0]][op.v[1]])continue;op.s.push_back("POUR("+string(1,char(i+1+'0'))+string(1,',')+string(1,char(j+1+'0'))+")");k[op.v[0]][op.v[1]]=1;q.push(op);} }}}}if(ans){cout<<ope.s.size()<<endl;for(int i=0;i<ope.s.size();i++)cout<<ope.s[i]<<endl;}else cout<<"impossible"; return 0;
}