POJ 3414 Pots
来源:互联网 发布:360商城抢购软件 编辑:程序博客网 时间:2024/05/14 22:51
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)
题意:给出容积分别为A升和B升的两只罐子,问是否可以得到C升水
直接用BFS枚举6种情况即可
#include <iostream>#include <stdio.h>#include <math.h>#include <string.h>#include <queue>using namespace std;int vis[110][110];//标记是否出现过这种情况struct node{ int a; int b; int step;//操作的次数 string str;//记录操作};queue<node>q;int bfs(int A,int B,int C){ node t; while(!q.empty()) { node s; s=q.front(); q.pop(); if(s.a==C||s.b==C)//第一罐或第二罐满足条件,输出结果 { printf("%d\n",s.step); for(int i=0; i<s.step; i++) { if(s.str[i]=='1') printf("FILL(1)\n"); else if(s.str[i]=='2') printf("FILL(2)\n"); else if(s.str[i]=='3') printf("DROP(1)\n"); else if(s.str[i]=='4') printf("DROP(2)\n"); else if(s.str[i]=='5') printf("POUR(1,2)\n"); else if(s.str[i]=='6') printf("POUR(2,1)\n"); } return 1; } for(int i=1; i<=6; i++)//遍历六种操作并记录 { if(i==1) { t.a=A; t.b=s.b; t.step=s.step+1; t.str=s.str+'1'; if(!vis[t.a][t.b])//未出现过的情况 { q.push(t); vis[t.a][t.b]=1;//标记为已出现过 } } else if(i==2) { t.a=s.a; t.b=B; t.step=s.step+1; t.str=s.str+'2'; if(!vis[t.a][t.b]) { q.push(t); vis[t.a][t.b]=1; } } else if(i==3) { t.a=0; t.b=s.b; t.step=s.step+1; t.str=s.str+'3'; if(!vis[t.a][t.b]) { q.push(t); vis[t.a][t.b]=1; } } else if(i==4) { t.a=s.a; t.b=0; t.step=s.step+1; t.str=s.str+'4'; if(!vis[t.a][t.b]) { q.push(t); vis[t.a][t.b]=1; } } else if(i==5) { if(s.a+s.b>B) { t.a=s.a+s.b-B; t.b=B; } else { t.b=s.b+s.a; t.a=0; } t.step=s.step+1; t.str=s.str+'5'; if(!vis[t.a][t.b]) { q.push(t); vis[t.a][t.b]=1; } } else if(i==6) { if(s.a+s.b>A) { t.b=s.b+s.a-A; t.a=A; } else { t.a=s.a+s.b; t.b=0; } t.step=s.step+1; t.str=s.str+'6'; if(!vis[t.a][t.b]) { q.push(t); vis[t.a][t.b]=1; } } } } return 0;}int main(){ int A,B,C; node p; scanf("%d%d%d",&A,&B,&C); memset(vis,0,sizeof(vis)); while(!q.empty()) { q.pop(); } p.a=0; p.b=0; p.step=0; p.str=""; vis[p.a][p.b]=1; q.push(p); int x=bfs(A,B,C); if(!x)//x为0说明无法得到满足条件的情况 { printf("impossible\n"); } return 0;}
- POJ 3414 Pots
- poj 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots BFS
- POJ 3414 Pots
- poj 3414 Pots
- poj 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- POJ 3414(Pots)
- POJ 3414 Pots
- POJ 3414 Pots
- POJ-3414-Pots
- POJ 3414 pots
- POJ 3414 Pots
- POJ 3414 Pots
- POJ 3414 Pots
- poj 3414 Pots
- 一道有趣的GOOGLE面试题
- java中的静态
- 14.ios之数据存取
- 快速矩阵幂POJ3070
- Java Web中的servlet、jsp、session等的用法。
- POJ 3414 Pots
- java学习笔记,复习+总结(1)
- 深圳蛇口游艇租赁,深圳南山游艇租赁,深圳蛇口游艇出租,深圳湾游艇会游艇租赁,深圳蛇口帆船出租
- 模拟退火求解TSP问题<2变换法产生路径>
- Android中Intent笔记
- 摘抄
- Xcode6为什么干掉pch(Precompile Prefix Header)&如何添加pch文件
- struts2的国际化i18n
- Activity之间通信