3414
来源:互联网 发布:网络真人赌博有猫腻吗 编辑:程序博客网 时间:2024/06/05 18:38
经过这道题目,加深了对bfs 以及 回溯的理解。
对于bfs 而言,重要的结点 node 的构建 ,而结点就代表着一种状态。其中包括了进行的操作,a, b 两个的容量。路径长度,要进行回溯的要就要在node 中添加一个pre 指针。
在bfs 中,关键的还是返回条件(其中一个的值与c相等就结束了,记录路程,并返回)
#include <iostream>#include <queue>#include <stack>#include <memory.h>#define max(a,b) (a>b?a:b)#define min(a,b) (a<b?a:b)using namespace std;int a,b,c,ans;int vis[110][110];struct node{ int a,b; int step; int flag; node *pre;};stack<int> sta;queue<node> q;void bfs(){ node cur,map[1000]; cur.a=0; cur.b=0; cur.step=0; cur.flag=0; cur.pre=NULL; q.push(cur); int count=-1; while(!q.empty() ) { cur=q.front(); q.pop(); node next; map[++count]=cur; if(cur.a==c||cur.b==c) { ans=cur.step; while(cur.pre!=NULL) { sta.push(cur.flag); cur=*cur.pre; } return ; } for(int i=1;i<=6;i++) { if(i==1) { if(cur.a==a) continue; next.a=a; next.b=cur.b; } else if(i==2) { if(cur.b==b) continue; next.b=b; next.a=cur.a; } else if(i==3) { if(cur.a==0) continue; next.a=0; next.b=cur.b; } else if(i==4) { if(cur.b==0) continue; next.a=cur.a; next.b=0; } else if(i==5) { next.a=max(0,cur.a+cur.b-b); next.b=min(b,cur.a+cur.b); } else if(i==6) { next.a=min(a,cur.a+cur.b); next.b=max(0,cur.a+cur.b-a); } next.flag=i; if(!vis[next.a][next.b] ) { vis[next.a][next.b]=1; next.step=cur.step+1; next.pre=&map[count]; q.push(next); } } }}int main(){ while(cin>>a>>b>>c) { memset(vis,0,sizeof(vis ) ); bfs(); if(ans==0 ) { cout<<"impossible"<<endl; continue; } cout<<ans<<endl; while(!sta.empty() ) { int i=sta.top(); sta.pop(); if(i==1) cout<<"FILL(1)"; else if (i==2) cout<<"FILL(2)"; else if (i==3) cout<<"DROP(1)"; else if (i==4) cout<<"DROP(2)"; else if (i==5) cout<<"POUR(1,2)"; else cout<<"POUR(2,1)"; cout<<endl; } }}
0 0
- 3414
- 3414
- POJ 3414
- POJ 3414
- POJ 3414
- poj 3414
- ZOJ 3414
- POJ 3414
- POJ 3414
- poj-3414
- poj 3414
- 3414POJ
- poj 3414
- POJ 3414
- poj 3414
- POJ-3414
- POJ 3414
- POJ 3414
- excel上传并读取
- 【计算机概论】 计算机:辅助人脑的好工具
- Struts2之struts-2.3.20开发环境的搭建并实现第一个Hello World小应用
- jpa多对多
- PAT-1066 Root of AVL Tree(解题报告)
- 3414
- UIWebView---JavaScriptCore和JS交互
- Apache + tomcat实现高并发负载均衡方案(三)----实现session复制的集群
- js动态引入JS文件
- PUTTY配置导出
- flashback query
- Trie(Codeforces Round #291 (Div. 2))
- 微信分享的完善,以及剪贴板的应用
- 新ip地址