题目:

给你两个容器,分别能装下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. Android Studio连接夜神模拟器

    运行-cmd,进入夜神模拟器安装目录,进入目录下的bin 执行Nox_adb.exe connect 127.0.0.1:62001命令,connect左右都有空格. 执行命令后就可以连接到夜神模拟器 ...

  2. PL/SQL之高级篇

    原文地址:http://www.cnblogs.com/sin90lzc/archive/2012/08/30/2661117.html 参考文献:<Oracle完全学习手册> 1.概述 ...

  3. 【Oracle】客户端监听配置

    首先找到oracle软件安装的目录,找到\product\11.2.0\client_1\network\admin,打开tnsnames.ora文件: 粘贴一下内容: LISTENER= (DESC ...

  4. EF CodeFirst 基础命令

    PM> enable-migrations 已在项目"EasyWeChat.Data"中启用迁移.若要覆盖现有迁移配置,请使用 -Force 参数. PM> add-m ...

  5. Linux 之secureCRT连接SSH

    1.登陆linux系统,打开终端命令.输入 rpm -qa |grep ssh 查找当前系统是否已经安装. 2.如果没有安装SSH软件包,可以通过yum  或rpm安装包进行安装. .3.安装好了之后 ...

  6. Python3中替代Python2中cmp()函数的新函数(gt,ge,eq,le,lt)

    原文出处:http://blog.csdn.net/Artprog/article/details/52197779 Python3中已经不能使用cmp()函数了,被如下五个函数替代: import ...

  7. Java基础学习笔记: 多线程,线程池,同步锁(Lock,synchronized )(Thread类,ExecutorService ,Future类)(卖火车票案例)

    多线程介绍 学习多线程之前,我们先要了解几个关于多线程有关的概念.进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. 线 ...

  8. res对象json,redirect

    1.res.json() var express=require('express'); var app=express(); app.get('/',function(req,res){ //返回j ...

  9. vue组件的拆分

    vue组件的拆分 <div class="div"> <!-- 拆分出来的组件 自定义名字moban --> <moban></moban ...

  10. phpMyAdmin使用教程

    ---恢复内容开始--- wamp中自带了管理MySQL的phpMyAdmin,可用来本机测试,服务器维护,虚拟主机用户管理MySQL. 登录需记住servername,username,passwo ...