poj 3414 搜索
来源:互联网 发布:java外包项目 编辑:程序博客网 时间:2024/06/07 04:52
Description
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.
Input
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).
Output
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’.
Sample Input
3 5 4
Sample Output
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
这是一道广搜的题目,下面是代码:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <queue>using namespace std;const int maxn=101;int vis[maxn][maxn],q[maxn*maxn*maxn][5];int A,B,C,fa;int bfs(){ int front=0,rear=0,a,b,na,nb,nfront,ans; q[rear][0]=0,q[rear][1]=0,q[rear][2]=0,q[rear++][4]=0; vis[0][0]=1; while(rear>front){ a=q[front][0],b=q[front][1],ans=q[front][4]; nfront=front,front++; if(a==C||b==C){//到达目标 fa=nfront; return 1; } na=A,nb=b; if(!vis[na][nb]){//fill 1 vis[na][nb]=1; q[rear][0]=na,q[rear][1]=nb; q[rear][2]=1,q[rear][3]=nfront; q[rear++][4]=ans+1; } na=a,nb=B; if(!vis[na][nb]){//fill 2 vis[na][nb]=1; q[rear][0]=na,q[rear][1]=nb; q[rear][2]=2,q[rear][3]=nfront; q[rear++][4]=ans+1; } na=0,nb=b; if(!vis[na][nb]){//drop 1 vis[na][nb]=1; q[rear][0]=na,q[rear][1]=nb; q[rear][2]=3,q[rear][3]=nfront; q[rear++][4]=ans+1; } na=a,nb=0; if(!vis[na][nb]){//drop 2 vis[na][nb]=1; q[rear][0]=na,q[rear][1]=nb; q[rear][2]=4,q[rear][3]=nfront; q[rear++][4]=ans+1; } int full; full=(a+b)/B; full?(na=a+b-B,nb=B):(na=0,nb=a+b); if(!vis[na][nb]){//pour 1 to 2 vis[na][nb]=1; q[rear][0]=na,q[rear][1]=nb; q[rear][2]=5,q[rear][3]=nfront; q[rear++][4]=ans+1; } full=(a+b)/A; full?(na=A,nb=a+b-A):(na=a+b,nb=0); if(!vis[na][nb]){//pour 2 to 1 vis[na][nb]=1; q[rear][0]=na,q[rear][1]=nb; q[rear][2]=6,q[rear][3]=nfront; q[rear++][4]=ans+1; } } return 0;}void print(int fa){//打印路径 if(fa==0) return; print(q[fa][3]); if(q[fa][2]<=2) printf("FILL(%d)\n",q[fa][2]); else if(q[fa][2]<=4)printf("DROP(%d)\n",q[fa][2]/2); else { if(q[fa][2]==5)printf("POUR(1,2)\n"); else printf("POUR(2,1)\n"); } return;}int main(){ int ok; while(scanf("%d%d%d",&A,&B,&C)!=EOF){ memset(vis,0,sizeof(vis)); ok=bfs(); if(ok){ printf("%d\n",q[fa][4]); print(fa); } else printf("impossible\n"); } return 0 ;}
- poj 3414( 搜索 )
- poj 3414 搜索BFS
- poj 3414 搜索
- poj 3414 Pots 搜索
- poj 3414(搜索)
- 20124330102 【 搜索 -- 深度优先搜索 】 POJ 3414 Pots
- POJ 3414-Pots 简单搜索 BFS
- POJ 3414 Pots (BFS搜索题)
- POJ 3414 Pots 解题报告(搜索)
- poj 3414 Pots 广度优先搜索
- poj 3414 dfs 广度优先搜索
- POJ 3414 基础搜索 BFS 六
- POJ搜索题目总结
- POJ 2976 参数搜索
- 简单搜索1979@POJ
- 广度搜索-POJ 1753
- POJ搜索题目
- POJ - 1011 搜索剪枝
- CodeForces 178B1 - Greedy Merchants tarjan求双联通分量+tarjan离线求最近公共祖先
- ibasefilter 转
- mysql的安装目录下找不到data目录的解决方法
- Apache Pig的一些基础概念及用法总结(1)
- mssql查询特定表&列是否存在
- poj 3414 搜索
- fun((exp1,exp2),(exp3,exp4,exp5))有几个实参(关于逗号表达式)?
- 第一篇博客。。
- WPF Tips: Data Binding Converter一例:bool与Visibility之间的转换
- Apache Pig的一些基础概念及用法总结(2)
- FTP:windows和远程linux服务器传送文件的方法
- linux之chdir函数解析
- Android之开启闪光灯
- WPF Tips: Data Binding Converter一例:bool与IsChecked属性(bool?)之间的转换