题目:

给你两个容器,分别能装下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. SQL Server对数据进行添加

    下面介绍一下SQL Server如何往数据库中加入数据. <!-- 添加主人信息 --> <form action="add_Admin.jsp" method= ...

  2. 实验0 安装GLUT包及工程的创建与运行

    下面将对Windows下在MicroSoft Visual C++2010(简称MSVC)环境下的OpenGL编程进行简单介绍. 1.安装GLUT工具包 GLUT不是OpenGL所必须的,但它会给我们 ...

  3. Lazarus开发环境编译选项配置

    Lazarus的环境配置让人有点犯晕,对于刚从delphi转到lazarus上的我来说,每次新建工程都会遇到一堆Can't find unit xxxx used by xxxx的问题,问题虽然不大, ...

  4. Type inference

    Type inference refers to the automatic detection of the data type of an expression in a programming ...

  5. google浏览器 打印A4 最大宽度和高度px

    width: 1563px;(max) + = 分页了 + = 分页了 + = 没有分页 / ViewBag.results[].Count)); <td width="15%&quo ...

  6. jq 禁用复选框 和输入框

    $('input').attr("readonly", ""); $('input').attr("disabled", "fal ...

  7. <转>python 发送邮件实例

    文件形式的邮件 #!/usr/bin/env python3 #coding: utf-8 import smtplib from email.mime.text import MIMEText fr ...

  8. openstack--memecache

    一.缓存系统 静态web页面: 1.工作流程: 在静态Web程序中,客户端使用Web浏览器(IE.FireFox等)经过网络(Network)连接到服务器上,使用HTTP协议发起一个请求(Reques ...

  9. promise原理及使用方法

    Promise 的含义 所谓Promise ,简单说就是一个容器,里面保存着某个未来才回结束的事件(通常是一个异步操作)的结果.从语法上说,Promise是一个对象,从它可以获取异步操作的消息. re ...

  10. Object.assign和序列/反序列

    Object.assign let testObj = { a:[1,2,4], b:{ name:'ls', school:['huf','yelu'], parent:{ father:'lili ...