UVa:571 Jugs
2013-10-07 19:48
316 查看
题意:给你两个给定容积的桶,无限量的水,要求你输出倒出指定容积水的步骤。
思路:bfs+剪枝。一定要判重,否则会RE。递归打印步骤。
思路:bfs+剪枝。一定要判重,否则会RE。递归打印步骤。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; struct State { int x[3],id; }; struct Path { int user,op,pre,id; }; Path p[1000000]; int X[3],N; bool vis[1005][1005],ok; void Fill(int &a,int &b,int A,int B) { if(a+b<=B) { b=a+b; a=0; } else { a=a+b-B; b=B; } } void Judge(State ss) { if(ss.x[1]==N||ss.x[2]==N) ok=true; } void Print_path(Path pth) { if(pth.op==1) { if(pth.user==1)puts("fill A"); else puts("fill B"); } else if(pth.op==2) { if(pth.user==1)puts("empty A"); else puts("empty B"); } else if(pth.op==3) { if(pth.user==1)puts("pour A B"); else puts("pour B A"); } } void Out_put(Path pth) { if(pth.id==0) return; Out_put(p[pth.pre]); Print_path(pth); } int main() { while(scanf("%d%d%d",&X[1],&X[2],&N)!=EOF) { State st; memset(vis,0,sizeof(vis)); int _id=0; st.x[1]=st.x[2]=st.id=0; p[_id].pre=-1; p[_id].id=_id++; queue<State> q; q.push(st); ok=false; while(!q.empty()&&!ok) { State tmp=q.front(); q.pop(); for(int i=1; i<=2&&!ok; ++i) { for(int j=1; j<=3&&!ok; ++j) //1 倒满 2 倒空 3 倒到另一个中 { State t=tmp; if(j==1) { if(tmp.x[i]==X[i]) continue; t.x[i]=X[i]; } else { if(!tmp.x[i]==X[i]) continue; if(j==2) t.x[i]=0; else if(j==3) { if(i==1) Fill(t.x[1],t.x[2],X[1],X[2]); else Fill(t.x[2],t.x[1],X[2],X[1]); } } if(vis[t.x[1]][t.x[2]]) continue; else { vis[t.x[1]][t.x[2]]=true; t.id=_id; p[_id].pre=tmp.id; p[_id].user=i; p[_id].op=j; p[_id].id=_id; _id++; q.push(t); } Judge(t); } } } Out_put(p[_id-1]); puts("success"); } return 0; }
相关文章推荐
- UVa 571 Jugs(经典倒水问题)
- UVa - 571 - Jugs -(倒水问题)
- POJ1606 HDU1415 UVA571 UVALive5502 Jugs【倒水+模拟】
- Jugs uva571
- UVA571 - Jugs(数论)
- uva 571 Jugs
- UVA571 - Jugs(数论)
- uva 571 jugs
- Jugs - UVa 571 bfs
- UVA 571 Jugs ADD18 小白书10 数学Part1 专题
- UVA571- Jugs
- UVA 571 Jugs(经典倒水问题)
- uva571 - Jugs(水壶)
- UVA - 571 Jugs
- uva 571 - Jugs
- UVA 571 Jugs
- UVA - 571 - Jugs (数论 - 经典倒水问题)
- UVA 571 Jugs
- uva 571 Jugs
- UVa 571 - Jugs (数论)