题目大意:
有一个瓶子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. asp.net 实现对xml文件的 读取,添加,删除,修改

    用于修改站内xml文件 已知有一个XML文件(bookstore.xml)如下:<?xml version="1.0" encoding="gb2312" ...

  2. sql yog注册码

    Name: AnyRegistration Code: 26f359fc-e3f6-4727-8af1-72a1a4a0819d

  3. JS计算指定日期是距今的第几周,星期几

    无意中在百度知道上发现这样一个问题,就抽时间见写了一个函数. 首先我们需要明确,既然是指定日期距今的第几周,那么就要知道指定的日期是什么,而且是不能确定的,会根据使用者不同而得到不同的日期,所以我们需 ...

  4. 解决UITableViewCell左侧分割线有空白的问题

    ios7中,UITableViewCell左侧会有默认15像素的空白.设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉. ios8中,setSeparatorIn ...

  5. 最优雅的C++跟lua交互.

    我先来吐槽一下我们这个项目. 我是做手机游戏的, cocos2dx引擎, lua编码. 这本来是一件很欢快的事情, 因为不用接触C++. C++写久了的人写lua, 就会感觉任督二脉被打通了, 代码写 ...

  6. directsound 应用实例

    sdk里边有个文件夹Samples\C++\XInput\AudioController这个就是

  7. linxu 挂载分区

    1. 添加新硬盘 设置 -> Storage -> SATA控制器->右击,选择“添加虚拟硬盘” 然后,根据需求创建合适的硬盘 2. 重启虚拟机 查看现有系统的磁盘空间 sudo f ...

  8. How do I solve the error: An error was encountered while running (Domain = LaunchServicesError, Code = 0) ?

    How do I solve the error: An error was encountered while running (Domain = LaunchServicesError, Code ...

  9. Solr4.8.0源码分析(26)之Recovery失败造成的宕机原因分析

    最近在公司做SolrCloud的容灾测试,刚好碰到了一个比较蛋疼的问题,跟SolrCloud的Recovery和leader选举有关,正好拿出来分析下. 现象是这样的:比如我有一台3个shard的So ...

  10. 转:LESS CSS 框架简介

    原文来自于:http://www.ibm.com/developerworks/cn/web/1207_zhaoch_lesscss/ 简介 CSS(层叠样式表)是一门历史悠久的标记性语言,同 HTM ...