题目大意:
有一个瓶子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. SSL VPN 详解

    SSL VPN是专栏VPN系列技术原理的最后一篇,SSL VPN作为远程接入型的VPN,已经具备非常广阔的前景,它的主要适应场景是取代L2TP Over IPSec,但功能要比L2TP Over IP ...

  2. idea配置tomcat.md

    [toc] 1.打开Edit Configurations,可以通过万能搜索快速进入!!! 2.添加服务器,在左上角找到Tomcat并添加 3.配置发布路径,Server标签页中填写完名称和路径,在D ...

  3. 中级Perl 第三章课后习题

    3. 10. 1. 练习1 [25 分钟] 读当前目录的文件列表并转换成全路径.不能用shell 命令或外部程序读当前目 录.Perl 的File::Spec 和Cwd 两个模块对这个程序有帮助.每个 ...

  4. 读终端输入数据BufferedReader

    public static void main(String[] args) {        BufferedReader br=new BufferedReader(new InputStream ...

  5. 百度地图实现车辆轨迹移动播放(baidu map api)

    开发技术:jquery,js baidu map api,json,ajax QQ1310651206

  6. SQL服务器名称的更改

    SQL服务器名称的更改   1.使用select @@ServerName可以看到当前数据库的服务器名称 2.从Sys.SysServers表中可以看到当前的所有服务器名称 3.使用 sp_drops ...

  7. js 去除字符串开头或者前几个字符。slice 也可以用于截取某一部分

    摘草自w3 slice() 方法可从已有的数组中返回选定的元素. 语法 arrayObject.slice(start,end) 参数 描述 start 必需.规定从何处开始选取.如果是负数,那么它规 ...

  8. vsftpd.conf 联机手册

    vsftpd.conf - vsftpd 的配置文件 描述vsftpd.conf 可以用于控制 vsftpd, 以实现各种各样的功能. vsftpd 缺省到 /etc/vsftpd.conf 处查找此 ...

  9. Title of live Writer

    Test From Windows Live Writer **markdown bold**

  10. cloudera目录位置

    http://www.aboutyun.com/thread-9189-1-1.html 这里来的嘿嘿. 1. 相关目录/var/log/cloudera-scm-installer : 安装日志目录 ...