poj 3414 Pots BFS求解
来源:互联网 发布:python编的著名游戏 编辑:程序博客网 时间:2024/05/14 14:14
题目链接:http://poj.org/problem?id=3414
思路:很简单,就是搜索即可,只是杯中的水进行状态转移时的代码有点不太好写。
代码:
#include<iostream>#include<sstream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>#include<queue>#include<set>#include<map>#include<vector>#include<string>#define LL __int64#define INF 0xffffffffusing namespace std;struct state{ int a[2]; string p,v;};int A[2],C,vis[110][110];state change(state t,int i,int j,int &op){//进行状态转移,j对应操作的1,2,3;i为操作对象 if(j==3){ int amount=min(t.a[i],A[1-i]-t.a[1-i]); t.a[i]-=amount;t.a[1-i]+=amount; if(vis[t.a[0]][t.a[1]]) {op=0;return t;} vis[t.a[0]][t.a[1]]=1; t.p+='3';t.v+=i+'0';op=1;return t; } t.a[i]=(j==1? A[i]:0); if(vis[t.a[0]][t.a[1]]) {op=0;return t;} vis[t.a[0]][t.a[1]]=1; t.p+='0'+j;t.v+='0'+i; op=1;return t;}void bfs(){ memset(vis,0,sizeof(vis)); queue<state> Q; Q.push((state){0,0,"",""}); vis[0][0]=1; while(!Q.empty()){ state tp=Q.front(); if(tp.a[0]==C || tp.a[1]==C) { cout<<(int)tp.p.size()<<endl; for(int i=0;i<(int)tp.p.size();i++){ if(tp.p[i]=='1') printf("FILL(%c)",tp.v[i]+1); else if(tp.p[i]=='2') printf("DROP(%c)",tp.v[i]+1); else printf("POUR(%c,%c)",tp.v[i]+1,'2'-tp.v[i]+'0'); printf("\n"); } return; } Q.pop(); for(int i=0;i<2;i++) for(int j=1;j<=3;j++){ int op=0; state tp2=change(tp,i,j,op); if(op==0) continue; Q.push(tp2); } } cout<<"impossible\n";}int main(){ while(cin>>A[0]){ cin>>A[1]>>C; bfs(); } return 0;}
0 0
- poj 3414 Pots BFS求解
- POJ 3414 Pots BFS
- POJ 3414 Pots(BFS)
- poj 3414 Pots bfs
- poj 3414 Pots BFS
- poj 3414 Pots BFS
- poj 3414 Pots (bfs)
- BFS-POJ-3414-Pots
- POJ 3414 Pots BFS
- POJ 3414-Pots(BFS)
- poj-3414 Pots BFS
- poj 3414 Pots bfs
- POJ 3414 POTS BFS
- poj 3414 (POTS) (BFS)
- POJ 3414 Pots (BFS)
- POJ - 3414 Pots(BFS)
- POJ 3414 Pots (BFS)
- 【Poj 3414】 Pots BFS
- UVA - 101 The Blocks Problem
- Hibernate初步
- 八皇后问题(递归回溯+迭代回溯)
- 11.3.7 BoxLayout布局管理器
- iOS加载xib的两种方法
- poj 3414 Pots BFS求解
- Linux协议栈-netfilter(1)-框架
- delete和delete[] 的区别
- 136. 整数数组,每个数均出现两次,唯独一个数出现一次。找出该数。
- UVA - 10815 Andy's First Dictionary
- C++内存池
- Homebrew
- 开发工具之Android Studio快捷键
- 将数据库中的图片显示