POJ 3414 Pots
题目链接
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
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
大致题意
给你a, b, c三个数,分别表示两个容器容量和你所需要最后的容量结果,两个容器可以互相倒水,问最少几次可以得到c,并输出过程
具体思路
其实两个罐子,无非就六种操作,AB罐子分别倒水,AB罐子充满水,A往B倒,B往A倒,无非就是BFS广搜最短路径,途中记录路径(并查集)原理其实和迷宫路线一样
#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
#include<queue>
using namespace std;
typedef long long int LLD;
LLD times[105][105],a,b,c,flag=0;;
struct S
{LLD nowx,nowy,perx,pery;
}way[105][105];
void join(LLD nowx,LLD nowy,LLD perx,LLD pery)
{way[nowx][nowy].nowx=nowx;way[nowx][nowy].nowy=nowy;way[nowx][nowy].perx=perx;way[nowx][nowy].pery=pery;return ;
}
void inputans(LLD x,LLD y)
{LLD len=times[x][y];LLD ansx[times[x][y]+5],ansy[times[x][y]+5];while ((way[x][y].nowx!=way[x][y].perx)||(way[x][y].nowy!=way[x][y].pery)){ansx[times[x][y]]=x;ansy[times[x][y]]=y;LLD xx=x;LLD yy=y;x=way[xx][yy].perx;y=way[xx][yy].pery;}ansx[0]=ansy[0]=0;for (LLD i=0;i<len;i++){if (ansx[i+1]==ansx[i]){if (ansy[i+1]==b){printf("FILL(2)\n");}else if (ansy[i+1]==0){printf("DROP(2)\n");}}else if (ansy[i+1]==ansy[i]){if (ansx[i+1]==a){printf("FILL(1)\n");}else if (ansx[i+1]==0){printf("DROP(1)\n");}}else if (ansx[i+1]>ansx[i]&&ansy[i+1]<ansy[i]){printf("POUR(2,1)\n");}else if (ansx[i+1]<ansx[i]&&ansy[i+1]>ansy[i]){printf("POUR(1,2)\n");}}return ;
}
int main()
{fill(times[0],times[0]+105*105,105105);scanf("%lld %lld %lld",&a,&b,&c);times[0][0]=0;way[0][0].nowx=0;way[0][0].nowy=0;way[0][0].perx=0;way[0][0].pery=0;queue<LLD>duia;queue<LLD>duib;duia.push(0);duib.push(0);while (!duia.empty()){LLD A,B;A=duia.front();B=duib.front();duia.pop();duib.pop();if (A==c||B==c){flag=1;printf("%lld\n",times[A][B]);inputans(A,B);break ;}if (times[0][B]>times[A][B]+1){times[0][B]=times[A][B]+1;duia.push(0);duib.push(B);join(0,B,A,B);}if (times[A][0]>times[A][B]+1){times[A][0]=times[A][B]+1;duia.push(A);duib.push(0);join(A,0,A,B);}if (times[A][b]>times[A][B]+1){times[A][b]=times[A][B]+1;duia.push(A);duib.push(b);join(A,b,A,B);}if (times[a][B]>times[A][B]+1){times[a][B]=times[A][B]+1;duia.push(a);duib.push(B);join(a,B,A,B);}LLD acha=a-A;if (acha>0&&B>0){if (B<=acha&×[A+B][0]>times[A][B]+1){times[A+B][0]=times[A][B]+1;duia.push(A+B);duib.push(0);join(A+B,0,A,B);}if (B>acha&×[a][B-acha]>times[A][B]+1){times[a][B-acha]=times[A][B]+1;duia.push(a);duib.push(B-acha);join(a,B-acha,A,B);}}LLD bcha=b-B;if (bcha>0&&A>0){if (A<=bcha&×[0][A+B]>times[A][B]+1){times[0][A+B]=times[A][B]+1;duia.push(0);duib.push(A+B);join(0,A+B,A,B);}if (A>bcha&×[A-bcha][b]>times[A][B]+1){times[A-bcha][b]=times[A][B]+1;duia.push(A-bcha);duib.push(b);join(A-bcha,b,A,B);}}}if (flag==0){printf("impossible\n");}return 0;
}
POJ 3414 Pots相关推荐
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- POJ 3414 Pots【BFS】+ Python
原题链接: 3414 -- Pots 参考资料:POJ 3414 - Pots | 眈眈探求 POJ 3414 Pots[BFS][图搜] - it610.com 一 特别注意: 1. 每一种操作对应 ...
- POJ 3414 Pots(深搜并打印路径)
POJ 3414 Pots(深搜并打印路径) You are given two pots, having the volume of A and B liters respectively. The ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj ...
- POJ 3414 Pots【广搜】
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- poj 3414 Pots BFS
参考文章:思路公式 代码思路 原来的代码有点瑕疵,这是最新的AC代码 #pragma warning(disable:4996) #include<iostream> #include&l ...
- H - Pots POJ - 3414(两个锅互相倒水)
H - Pots POJ - 3414 题意: (两个锅互相倒水)希望能通过题目中的几种操作使一个锅中的水量为目标水量 bfs 搜索每一步的每种操作,直到所有操作用完,则impossible,否则输出 ...
- Pots POJ - 3414
You are given two pots, having the volume of A and B liters respectively. The following operations c ...
- Pots POJ - 3414(bfs)
You are given two pots, having the volume of A and B liters respectively. The following operations c ...
最新文章
- 删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录...
- ASP.NET生成静态页面的方法
- 【BZOJ 4057 Kingdoms】
- CTO说了,delete后不加limit,直接滚蛋!
- 【C++】【TinyXml】xml文件的读写功能使用——写xml文件
- Spring2.5整合JPA
- 关于Swift中Struct,Class和Enum的哪些事儿
- Fedora15安装NVIDIA显卡驱动全过程
- 语言与golang语言运行速度_Golang语言情怀第13期 Go 语言设计模式 介绍
- C#如何[添加][删除][修改]XML中的记录
- 技术人员的明天:35岁后我们做什么
- 如何在今日头条上持续生产优质内容
- memcpy函数的使用方法
- 学成在线首页——静态页面(html+css)素材链接放在文章结尾了
- java毕业设计药品管理系统Mybatis+系统+数据库+调试部署
- 我的新书《C++服务器开发精髓》终于出版啦
- Protecting Against DNN Model Stealing Attacks 论文阅读心得
- Apache FOP 将Java对象转换为pdf文件
- php win8环境搭建
- anroid获取ping值