题目大意:
有一个瓶子A和一个瓶子B,可以有三种操作倒满,倒空,或者把瓶子A倒向瓶子B(或者把瓶子B倒向瓶子A),可以扩展出6种操作,没什么简单的写法,只能一种一种的写.....
当然是使用广搜.......................直到一个瓶子里面有C升水,或者倒不出来这种结果,还需要输出到得步骤, 可以递归输出路径......
///////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std; #define maxn 110 int A, B, C;
char a[][] ={ "FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"}; struct node
{
    int fromX, fromY, step, op;
}v[maxn][maxn];
struct point
{
    int x, y;
};//当前瓶子A,B的水量 //输出路径
void DFS(int x, int y)
{
    if(x ==  && y == )
        return ;     DFS(v[x][y].fromX, v[x][y].fromY);     printf("%s\n", a[ v[x][y].op ]); }
void BFS()
{
    queue<point> Q;
    point s, q;
    s.x = s.y = ;
    v[][].step = ;     Q.push(s);     while(Q.size())
    {
        s = Q.front();Q.pop();         if(s.x == C || s.y == C)
        {
            printf("%d\n", v[s.x][s.y].step-);
            DFS(s.x, s.y);             return ;
        }         for(int i=; i<; i++)
        {
            q = s;             if(i == )
                q.x = A;
            else if(i == )
                q.y = B;
            else if(i == )
                q.x = ;
            else if(i == )
                q.y = ;
            else if(i == )
            {
                if(q.x+q.y <= B)
                    q.y += q.x, q.x = ;//把A里面的水全倒B里面
                else
                    q.x -= (B-q.y), q.y = B;//把B倒满
            }
            else
            {
                if(q.x+q.y <= A)
                    q.x += q.y, q.y = ;
                else
                    q.y -= (A-q.x), q.x = A;
            }             if(v[q.x][q.y].step == )
            {
                v[q.x][q.y].step = v[s.x][s.y].step+;
                v[q.x][q.y].fromX = s.x;
                v[q.x][q.y].fromY = s.y;
                v[q.x][q.y].op = i;                 Q.push(q);
            }
        }
    }     printf("impossible\n");
} int main()
{
    while(scanf("%d%d%d", &A, &B, &C) != EOF)
    {
        memset(v, , sizeof(v));         BFS();
    }     return ;

}

H - Pots的更多相关文章

  1. (poj)3414 Pots (输出路径的广搜)

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

  2. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  3. Pots(bfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8266   Accepted: 3507   Special Judge D ...

  4. poj 3414 Pots (bfs+线索)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special J ...

  5. M - Pots

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

  6. POJ3414—Pots(bfs加回溯)

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

  7. POJ 3414 Pots(BFS+回溯)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11705   Accepted: 4956   Special J ...

  8. poj 3414 Pots【bfs+回溯路径 正向输出】

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

  9. poj3414 Pots (BFS)

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

随机推荐

  1. [转帖]MATLAB曲线绘制及颜色类型

    信号源产生的方法 来源:http://www.2cto.com/kf/201401/270494.html  matlab的checkerboard说明,GOOD! 来源:http://www.chi ...

  2. Index Full Scan vs Index Fast Full Scan-1103

    [Oracle] Index Full Scan vs Index Fast Full Scan作者:汪海 (Wanghai) 日期:14-Aug-2005 出处:http://spaces.msn. ...

  3. kill session真的能杀掉进程吗

    session1 确认sidSYS @ prod > select userenv('sid') from dual; USERENV('SID')-------------- 144 sess ...

  4. 最简单的基于FFmpeg的移动端例子:IOS 视频转码器

    ===================================================== 最简单的基于FFmpeg的移动端例子系列文章列表: 最简单的基于FFmpeg的移动端例子:A ...

  5. os项目icon和default 等相关图标命名规则和大小设置

    最新的参考apple官网地址:https://developer.apple.com/library/ios/qa/qa1686/_index.html,网页下面有详细的使用方法(ios7以后的) 转 ...

  6. MYSQL命令行连接数据库

    连接数据库 mysql -uroot -proot -P3306 -Dmydemo # 参数详解 -uuser 用户user -ppwd 密码 -P3306 端口 -Dmysql 数据库 #显示所有数 ...

  7. c++异常机制实现原理

    今天突然看到一篇文章,讲异常机制的实现,所以分享一下:http://baiy.cn/doc/cpp/inside_exception.htm 内容讲的很深,但是编译器的实现是不是真是这样就不知道了,我 ...

  8. Oauth认证简介

    Oauth是什么: 1.Oauth是一种安全认证的协议: 2.Oauth为用户资源的授权提供了一个安全的.开放而又简易的标准: 3.Oauth的授权不会使第三方触及到用户的账号信息(用户名和密码). ...

  9. java设计模式——单例(Singleton)模式

    在某些场景,你需要找到一个承担职责的对象,并且这个对象是他所属类的唯一实例.此时可以使用单例模式. 单例模式的意图是为了确保一个类有且仅有一个实例,并为他提供一个全局的访问点.创建一个担当独一无二角色 ...

  10. QQ弹窗代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...