状态搜索。类似八数码问题

AC代码

#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200+5;
int vis[maxn][maxn];//是否访问过该节点
struct node
{
    int fill[3]; //当前状态的水的分布
    int dist;    //当前状态倒水量
    bool operator < (const node &p) const //排序
    {
        return dist>p.dist;
    }
};

int ans[maxn],cup[3];  

void update_ans(node &u) //细节处理 难点
{
    for(int i=0;i<3;++i)
    {
        int d=u.fill[i];
        if(ans[d]<0||u.dist<ans[d]) ans[d]=u.dist;
    }
} 

void bfs(int a,int b,int c,int d)
{
    cup[0]=a,cup[1]=b,cup[2]=c;
    node start;
    start.fill[0]=0,start.fill[1]=0,start.fill[2]=c;
    start.dist=0;
    priority_queue<node>q;
    q.push(start);
    while(!q.empty())
    {
        node u=q.top();  q.pop();
        update_ans(u);
        if(ans[d]>=0) break;

        //尝试从第i个杯子中向第j个倒水
        for(int i=0;i<3;++i)
        for(int j=0;j<3;++j)
        {
            if(i==j) continue;
            if(u.fill[i]==0||u.fill[j]==cup[j]) continue;
            int pour=min(u.fill[i],cup[j]-u.fill[j]);
            node temp;
            memcpy(&temp,&u,sizeof(u));
            temp.dist=u.dist+pour;
            temp.fill[i]-=pour;
            temp.fill[j]+=pour;
            if(!vis[temp.fill[0]][temp.fill[1]])
            {
            q.push(temp);
            vis[temp.fill[0]][temp.fill[1]]=1;
            }
        }
    }
    while(d>=0)
    {
        if(ans[d]>=0)
        {
            printf("%d %d\n",ans[d],d);
            return ;
        }
        --d;
    }
}
int main()
{
    int T,a,b,c,d;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d",&a,&b,&c,&d);
        memset(ans,-1,sizeof(ans));
        memset(vis,0,sizeof(vis));
        bfs(a,b,c,d);
    }
    return 0;
}

如有不当之处欢迎指出!

uva10603 倒水问题的更多相关文章

  1. UVa10603 倒水 Fill-状态空间搜索

    https://vjudge.net/problem/UVA-10603 There are three jugs with a volume of a, b and c liters. (a, b, ...

  2. UVA10603 倒水问题 Fill

    伫倚危楼风细细,望极春愁,黯黯生天际.草色烟光残照里,无言谁会凭阑意. 拟把疏狂图一醉,对酒当歌,强乐还无味.衣带渐宽终不悔,为伊消得人憔悴.--柳永 题目:倒水问题 网址:https://onlin ...

  3. 关于BFS和dijkstra(2019.04.20)

    我的BFS板子 struct node{/*略*/};//表示一个状态 std::map<node,bool>vis;//判断每个状态是否已访问过 std::queue<node&g ...

  4. UVa 10603 倒水问题

    https://vjudge.net/problem/UVA-10603 题意:三个杯子,倒水问题.找出最少倒水量. 思路:路径寻找问题.不难,暴力枚举. #include<iostream&g ...

  5. 广度优先搜索 cdoevs 1226 倒水问题

    cdoevs 1226 倒水问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 有两个无刻度标志的水壶,分别可装 x 升 ...

  6. 倒水问题 (codevs 1226) 题解

    [问题描述] 有两个无刻度标志的水壶,分别可装x升和y升 ( x,y 为整数且均不大于100)的水.设另有一水缸,可用来向水壶灌水或接从水壶中倒出的水, 两水壶间,水也可以相互倾倒.已知x升壶为空壶, ...

  7. UVa10603 Fill

    解题思路:这是神奇的一题,一定要好好体会.见代码: #include<cstdio> #include<cstring> #include<algorithm> # ...

  8. 【UVA10603】Fill (构图+最短路)

    题目: Sample Input22 3 4 296 97 199 62Sample Output2 29859 62 题意: 有三个杯子它们的容量分别是a,b,c, 并且初始状态下第一个和第二个是空 ...

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

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

随机推荐

  1. maven多模块搭建

    此时你会发现父模块含有如下内容 这是因为创建的maven项目都带有样例,比如上图的这张图片 各种artifact都是做什么的呢,@参考文章中给出了答案 怎么创建不带这些呢? 那就创建simple pr ...

  2. lambda高级进阶--组合函数

    只有一个参数的函数可以将一个值转换成另一个值,如果你有2个这样的转换函数,那么在进行一个转换之后在进行第二个转换,其实也是一个中转换. 说到这里,先说一下我个人的一个习惯,我在写javaBean的se ...

  3. linkin大话java

    青春不灭,理想犹存,linkin--勿忘初心! 不知不觉已经和java并肩作战将近了2年,在这2年之中模模糊糊研究了java2ee领域的好多东西,但是都没有做系统的整理.以前写的笔记也是零零散散不成一 ...

  4. Composer - windows下安装方法

    在windows下安装的方法 方法一:使用安装程序 这是将 Composer 安装在你机器上的最简单的方法. 下载并且运行 Composer-Setup.exe,它将安装最新版本的 Composer ...

  5. 关于awk的范围模式功能问题

    关于awk的范围模式功能问题 man awk中这样写到 The pattern1, pattern2 form of an expression is called a range pattern. ...

  6. Eclipse使用EGit,commit之后仍显示NO HEAD的解决方法

    由于以前做的项目一直用的都是svn,想试一下git尝尝鲜,遇见点问题.记录下来防止以后再出现这种情况,同时希望能帮助到和我一样的初学者. 问题描述: 右键项目-->team-->commi ...

  7. 关于spring通知中propagation的7种配置《转载》

    <转载>:http://nannan408.iteye.com/blog/1754882

  8. template.helper()方法

    上一篇文章我们已经讲到了helper()方法,但是上面的例子只是一个参数的写法,如果是多个参数,写法就另有区别了. <div id="user_info"></d ...

  9. 05_Python Format Operation

    Python格式化输出 print('name: %s,version: %s,code: %d' %('Python',3.6,3)) print('name: {name},version: {v ...

  10. Redis .Net 基本类型使用之南

    前言 最近需要使用redis,看了一些文档,也在博客园里面看了很多文章,这里就记录下Redis常用类型的操作. String string是redis基本类型,一般通过Get,Set 命令进行操作,这 ...