poj 3414 搜索BFS
来源:互联网 发布:手机网络相关参数含义 编辑:程序博客网 时间:2024/05/30 04:40
题意:有A,B两个瓶子,开始是空的,有六种操作,问最少几步能让其中一个瓶子是C的体积;
#include<stdio.h>#include<string.h>struct node{ int a; int b; int parent; int level; int op;} pot[10005];int stk[10005];int visit[105][105];void output(int p){ int top=0; stk[0]=p; while(pot[stk[top]].parent!=-1) { top++; stk[top]=pot[stk[top-1]].parent; } for(int i=top; i>=0; i--) { switch(pot[stk[i]].op) { case 0: { printf("DROP(1)\n"); } break; case 1: { printf("FILL(1)\n"); } break; case 2: { printf("DROP(2)\n"); } break; case 3: { printf("FILL(2)\n"); } break; case 4: { printf("POUR(1,2)\n"); } break; case 5: { printf("POUR(2,1)\n"); } break; } }}void bfs(int A,int B,int C){ node cur; pot[0].a=0; pot[0].b=0; pot[0].parent=-1; pot[0].level=0; pot[0].op=-1; int front=0; int rear=1; visit[0][0]=1; int locate=-1,ans; while(front<rear) { cur=pot[front++]; if(cur.a==C||cur.b==C) { ans=cur.level; locate=front-1; break ; } int ta,tb,tp; for(int i=0; i<6; i++) { if(i==0) { ta=0; tb=cur.b; tp=i; } if(i==1) { ta=A; tb=cur.b; tp=i; } if(i==2) { tb=0; ta=cur.a; tp=i; } if(i==3) { tb=B; ta=cur.a; tp=i; } if(i==4) { if((B-cur.b)>=cur.a) { ta=0; tb=cur.a+cur.b; tp=i; } else { ta=cur.a-(B-cur.b); tb=B; tp=i; } } if(i==5) { if((A-cur.a)>=cur.b) { tb=0; ta=cur.a+cur.b; tp=i; } else { ta=A; tb=cur.b-(A-cur.a); tp=i; } } if(visit[ta][tb]==0) { visit[ta][tb]=1; node change; change.a=ta; change.b=tb; change.parent=front-1; change.level=cur.level+1; change.op=tp; pot[rear++]=change; } } } if(locate==-1) { printf("impossible\n"); } else { printf("%d\n",ans); output(locate); }}int main(){ int A,B,C; scanf("%d%d%d",&A,&B,&C); memset(visit,0,sizeof(visit)); bfs(A,B,C); return 0;}
- poj 3414 搜索BFS
- POJ 3414-Pots 简单搜索 BFS
- POJ 3414 Pots (BFS搜索题)
- POJ 3414 基础搜索 BFS 六
- poj 2243 BFS搜索
- poj 3083 搜索 DFS,BFS
- poj 2251 简单搜索bfs
- poj 3278 简单搜索bfs
- POJ 3126广度搜索BFS
- POJ - 3414 Pots(15.10.10 搜索专题)bfs
- POJ 3414 Pots——bfs搜索+dfs输出
- POJ 3126-Prime Path 简单搜索 BFS
- POJ 3278 用队列实现BFS搜索
- POJ 2631 最长路搜索,BFS
- poj 1562 简单的BFS搜索
- POJ 3278(BFS-搜索范围)
- POJ 1475 Pushing Boxes 搜索- 两重BFS
- poj 3126 Prime Path(bfs搜索)
- ArcGIS API for Silverlight 扩展测距功能-保留测距结果
- ORA-28547:连接服务器失败,可能是Oracle Net管理错误
- poj线段树专题
- 现实终究会历练我们,是现实太无奈~
- Oracle使用触发器实现ID自增的问题
- poj 3414 搜索BFS
- 利用Excel批量快速发送电子邮件
- 一些名词的解释
- 自己写游戏引擎——Get your hands dirty!!! (02)
- Java_乔晓松_使用cookie显示曾经访问过的商品
- linux下删除乱码文件
- Gradient Boost 算法流程分析
- java多线程模拟2个窗口同时卖车票100张
- C语言中对文件操作的小结