POJ 3414-Pots(BFS-模拟倒水)
来源:互联网 发布:中国神话知乎 编辑:程序博客网 时间:2024/05/09 04:41
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)
Source
题目意思:
解题思路:
#include<iostream>#include<cstdio>#include<iomanip>#include<cmath>#include<cstdlib>#include<cstring>#include<map>#include<algorithm>#include<vector>#include<queue>using namespace std;#define INF 0x3f3f3f3f#define MAXN 110struct Node{ int a,b;//分别表示两个瓶子 int step;//步数 int op;//操作 Node *former;//前一个操作} q[MAXN*MAXN];int A,B,C;//输入的原始值int vis[MAXN][MAXN];//vis[A][B]=true,表示a瓶装A升且b瓶装B升的状态是否访问过bool flag=false;//能否出现要求的水量void OutPut(Node *temp)//输出操作{ int s[MAXN*MAXN],cnt=0; while(temp->former!=NULL) { s[cnt++]=temp->op;//存储操作的序号 temp=temp->former;//指向下一个操作 } for(int i=cnt-1; i>=0; --i) { switch(s[i]) { case 1: cout<<"FILL(1)"<<endl; break; case 2: cout<<"FILL(2)"<<endl; break; case 3: cout<<"DROP(1)"<<endl; break; case 4: cout<<"DROP(2)"<<endl; break; case 5: cout<<"POUR(1,2)"<<endl; break; case 6: cout<<"POUR(2,1)"<<endl; break; } } return;}void BFS(){ memset(vis,false,sizeof(vis)); vis[0][0]=true; q[0].a=q[0].b=q[0].step=q[0].op=0; q[0].former = NULL; int head=0,tail=1; while(head<tail) { Node *temp =&q[head++]; int a=temp->a; int b=temp->b; int step=temp->step; if(a==C||b==C) { flag=true; cout<<step<<endl; OutPut(temp); return; } for(int i=0; i<6; ++i) { if(i==0)//FILL(1) { if(a<A&&!vis[A][b]) { vis[A][b]=true; q[tail].a=A; q[tail].b=b; q[tail].step=step+1; q[tail].former=temp; q[tail++].op=1; } } else if(i==1)//FILL(2) { if(b<B&&!vis[a][B]) { vis[a][B]=true; q[tail].a=a; q[tail].b=B; q[tail].step=step+1; q[tail].former=temp; q[tail++].op=2; } } else if(i==2)//DROP(1) { if(a>0&&!vis[0][b]) { vis[0][b]=true; q[tail].a=0; q[tail].b=b; q[tail].step=step+1; q[tail].former=temp; q[tail++].op=3; } } else if(i==3)//DROP(2) { if(b>0&&!vis[a][0]) { vis[a][B]=true; q[tail].a=a; q[tail].b=0; q[tail].step=step+1; q[tail].former=temp; q[tail++].op=4; } } else if(i==4)//POUR(1,2) { if(a>B-b&&b<B&&!vis[a-(B-b)][B])//the pot 2 is full (and there may be some water left in the pot 1) { vis[a-(B-b)][B]=true; q[tail].a=a-(B-b); q[tail].b=B; q[tail].step=step+1; q[tail].former=temp; q[tail++].op=5; } else if(a>0&&a+b<B&&!vis[0][a+b])//the pot 1 is empty (and all its contents have been moved to the pot 2) { vis[0][a+b]=true; q[tail].a=0; q[tail].b=a+b; q[tail].step=step+1; q[tail].former=temp; q[tail++].op=5; } } else if(i==5)//POUR(2,1) { if(b>A-a&&a<A&&!vis[A][b-(A-a)])//the pot 1 is full (and there may be some water left in the pot 2) { vis[A][b-(A-a)]=true; q[tail].a=A; q[tail].b=b-(A-a); q[tail].step=step+1; q[tail].former=temp; q[tail++].op=6; } else if(b>0&&a+b<A&&!vis[a+b][0])//the pot 2 is empty (and all its contents have been moved to the pot 1) { vis[a+b][0]=true; q[tail].a=a+b; q[tail].b=0; q[tail].step=step+1; q[tail].former=temp; q[tail++].op=6; } } } }}int main(){#ifdef ONLINE_JUDGE#else freopen("F:/cb/read.txt","r",stdin); //freopen("F:/cb/out.txt","w",stdout);#endif ios::sync_with_stdio(false); cin.tie(0); cin>>A>>B>>C; BFS(); if(!flag) puts("impossible"); return 0;}/*3 5 4*/
- POJ 3414-Pots(BFS-模拟倒水)
- POJ 3414 Pots【bfs模拟倒水问题】
- POJ 3414 Pots【bfs模拟倒水问题】
- POJ 3414 Pots(BFS 倒水)
- POJ 3414 Pots(模拟倒水)
- POJ 3414 Pots(BFS倒水问题)
- poj 3414 Pots [bfs - 倒水问题]
- Pots (模拟倒水)
- POJ-3414-Pots(BFS 模拟)
- POJ 3414 Pots(BFS)||SDUT(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)
- 分段双调排序实现
- 日志采集框架Flume的安装及使用
- caffe HDF5Data 层使用及数据生成
- VHDL库和包(Libraries and Packages)
- SSM框架——Spring+SpringMVC+Mybatis的搭建教程
- POJ 3414-Pots(BFS-模拟倒水)
- clock
- 读刘未鹏《我在南大的七年》有感
- java 垃圾回收机制(二)
- RabbitMQ入门教程 For Java【1】
- mysql数据库更改编码
- NSMutableSet 与NSMutableArray
- 生成hdf5文件用于多标签训练
- (一)redis学习------redis介绍