题目:

给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作
FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2);
DROP(i)        将第i个容器抽干
POUR(i,j)      将第i个容器里的水倒入第j个容器(这次操作结束后产生两种结果,一是第j个容器倒满并且第i个容器依旧有剩余,二是第i个容器里的水全部倒入j中,第i个容器为空)
现在要求你写一个程序,来找出能使其中任何一个容器里的水恰好有C升,找出最少操作数并给出操作过程

输入:

有且只有一行,包含3个数A,B,C(1<=A,B<=100,C<=max(A,B))

输出:

第一行包含一个数表示最小操作数K
随后K行每行给出一次具体操作,如果有多种答案符合最小操作数,输出他们中的任意一种操作过程,如果你不能使两个容器中的任意一个满足恰好C升的话,输出“impossible”

样例:

分析:简单的BFS,难点在于回溯,给每个状态用数组记录路径

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<numeric>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<cctype>
#define PI acos(-1.0)
const int INF = 0x3f3f3f3f;
const int NINF = -INF - ;
typedef long long ll;
using namespace std;
int a, b, c;
int used[][];
struct node
{
int x, y;
int flag;
int path[];//数组中0-5分别表示6种不同操作
}st;
string print[] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};
void bfs()
{
queue<node> q;
for (int i = ; i <= a; ++i)
{
for (int j = ; j <= b; ++j)
used[i][j] = INF;
}
memset(used, , sizeof(used));
st.x = , st.y = ;
st.flag = ;
memset(st.path, -, sizeof(st.path));
q.push(st);
used[st.x][st.y] = ;
while (q.size())
{
node temp = q.front();
q.pop();
if (temp.x == c || temp.y == c)
{
cout << temp.flag << endl;
for (int i = ; i < temp.flag; ++i)
cout << print[temp.path[i]] << endl;
return;
}
for (int i = ; i < ; ++i)
{
node now = temp;
now.flag++;
if (i == && now.x != a)
{
now.x = a;
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
else if (i == && now.y != b)
{
now.y = b;
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
else if (i == && now.x != )
{
now.x = ;
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
else if (i == && now.y != )
{
now.y = ;
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
else if (i == )
{
if (now.x + now.y > b)
{
now.x -= b - now.y;
now.y = b;
}
else
{
now.y += now.x;
now.x = ;
}
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
else if (i == )
{
if (now.x + now.y > a)
{
now.y -= a - now.x;
now.x = a;
}
else
{
now.x += now.y;
now.y = ;
}
if (!used[now.x][now.y])
{
used[now.x][now.y] = ;
now.path[temp.flag] = ;
q.push(now);
}
}
}
}
cout << "impossible" << endl;
}
int main()
{
cin >> a >> b >> c;
bfs();
return ;
}

POJ3414 Pots的更多相关文章

  1. POJ3414—Pots(bfs加回溯)

    http://poj.org/problem?id=3414                                       Pots Time Limit: 1000MS   Memor ...

  2. POJ-3414 Pots (BFS)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  3. 快速切题 poj3414 Pots

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10042   Accepted: 4221   Special J ...

  4. poj3414 Pots (BFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12198   Accepted: 5147   Special J ...

  5. POJ3414 Pots —— BFS + 模拟

    题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  6. POJ3414 Pots BFS搜素

    题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...

  7. POJ-3414.Pots.(BFS + 路径打印)

    这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...

  8. poj3414 Pots(BFS)

    题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...

  9. 【BFS】Pots

    [poj3414]Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16925   Accepted: 7168   ...

随机推荐

  1. 利用set特性判断list是否存在重复的值

    List<String>  list2=new ArrayList();//存放很多值的list //根据set不能存储相同的值该特性来判断list2中的值是否重复 HashSet set ...

  2. 【Oracle】表连接三种方式

    表连接的方式有三种分别是:排序合并连接(Sort Merge Join).嵌套循环连接(Nested Loops Join).哈希连接(Hash Join). 1. 排序合并连接(Sort Merge ...

  3. 复习java基础第六天(IO)

    一:File 类 • 输入:读取外部数据(磁盘.光盘等存储设备的数据)到程序(内存)中.  • 输出:将程序(内存)数据输出到磁盘.光盘等存储设备中 • Java 的 IO 流主要包括输入.输出两种 ...

  4. AI不与人为敌

    业界广为流传着一句话:有多少人工就有多少智能. 其实还应该有一句话:有多少付出就因该有多少回报.公正是世界永恒的话题. 一.人工智能还是人工愚蠢 科技从来没有善恶,也不会杀人,愚蠢的人比聪明的人做的错 ...

  5. js 习题

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  6. CF319E Ping-Pong 线段树 + vector + 思维

    Code: #include<bits/stdc++.h> #define N 3000009 #define maxn 3000009 #define ll long long #def ...

  7. HTML 1.1页面js修改文字颜色

    昨天的报告页面,想要实现根据不同文字内容改变字体颜色,效果图: 调试了半天出不来效果,最后请教了前端,上代码: <!DOCTYPE html> <html lang="en ...

  8. js声明变量作用域会提前

    var s = 1; function test() { console.info(s); var s = 2; console.info(s); } test(); >>>unde ...

  9. ldap 用户组和用户(4)

    Posixgroup用户组属性 默认情况下openldap的用户组属性是Posixgroup,Posixgroup用户组属性和用户没有实际的对应关系.如果我们一定要把Posixgroup和user对应 ...

  10. cxgrid的过滤%x%问题【备查】

    把这个文件复制到你的程序目录\DevExpress VCL\ExpressDataController\Sources\cxLike.pas function LikeStr(const AStr,  ...