UVA_10603 倒水问题 隐式图搜索
这道题目是刘汝佳白书上的例题,没有LRJ在白书上提到的划归为搜索问题,还真是一时难以想到好的解法。即三个瓶子,任意的一个状态都是一个节点,最后就划归为一个搜索问题。
由于题目数据量不大,三个杯子容量都不超过200,所以用个vis三维数组保存下访问过得状态以达到剪枝的目的,不过我在想数据量稍微大一点,这个数组就开不下了,同时搜索时间将大大增加。。。所以面对大数据的时候有没有更好的方法,我暂时还没想到。
代码写得超级挫,每个状态的转化都是手动敲的,好像可以用一个函数统一解决,当时写的时候就比较急,就直接手敲了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define INF 1<<31;
using namespace std;
int vis[][][];
int a,b,c,d;
struct node{
int a1,b1,c1;
int minw;
void init(int a,int b,int c)
{
a1=a;b1=b;c1=c;
}
};
node ans;
bool judge(node xq)
{
int q1,q2,q3;
q1=xq.a1-d;
q2=xq.b1-d;
q3=xq.c1-d; q1=q1>= ? q1 : -q1;
q2=q2>= ? q2 : -q2;
q3=q3>= ? q3 : -q3;
int ansq=min(q1,min(q2,q3));
int c1,c2,c3;
c1=ans.a1-d;
c2=ans.b1-d;
c3=ans.c1-d;
if (c1<) c1=-c1;
if (c2<) c2=-c2;
if (c3<) c3=-c3;
int ansc=min(c1,min(c2,c3)); if (ansq<ansc) return ;
if (ansq==ansc)
{
if (xq.minw<ans.minw) return ;
}
return ; }
void bfs(node xq)
{
queue<node>q;
q.push(xq);
while (!q.empty())
{
node x=q.front();
q.pop();
if (vis[x.a1][x.b1][x.c1]) continue;
vis[x.a1][x.b1][x.c1]=;
if (judge(x)) ans=x;
node t1;
int temp;
if (x.a1>){
t1=x;
if (x.b1<b){
temp=min(b-x.b1,x.a1);
t1.a1-=temp;
t1.b1+=temp;
t1.minw+=temp;
q.push(t1);
}
t1=x;
if (x.c1<c){
temp=min(c-x.c1,x.a1);
t1.a1-=temp;
t1.c1+=temp;
t1.minw+=temp;
q.push(t1);
}
}
if (x.b1>){
t1=x;
if (x.a1<a)
{
temp=min(a-x.a1,x.b1);
t1.b1-=temp;
t1.a1+=temp;
t1.minw+=temp;
q.push(t1);
}
t1=x;
if (x.c1<c){
temp=min(c-x.c1,x.b1);
t1.b1-=temp;
t1.c1+=temp;
t1.minw+=temp;
q.push(t1);
}
}
if (x.c1>){
t1=x;
if (x.a1<a){
temp=min(a-x.a1,x.c1);
t1.c1-=temp;
t1.a1+=temp;
t1.minw+=temp;
q.push(t1);
}
t1=x;
if (x.b1<b){
temp=min(b-x.b1,x.c1);
t1.c1-=temp;
t1.b1+=temp;
t1.minw+=temp;
q.push(t1);
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
memset(vis,,sizeof vis);
node first;
first.init(,,c);
first.minw=;
ans=first;
ans.minw=INF;
bfs(first);
int tq[];
tq[]=ans.a1;
tq[]=ans.b1;
tq[]=ans.c1;
sort(tq+,tq+);
int w;
for (w=;w>=;w--)
{
if (tq[w]<=d) break;
}
printf("%d %d\n",ans.minw,tq[w]); }
return ;
}
UVA_10603 倒水问题 隐式图搜索的更多相关文章
- 紫书 例题 11-6 UVa 658 (状态压缩+隐式图搜索+最短路)
这道题用到了很多知识点, 是一道好题目. 第一用了状态压缩, 因为这里最多只有20位, 所以可以用二进制来储存状态 (要对数据范围敏感), 然后 涉及到了一些位运算. 第二这里是隐式 ...
- uva 10274 Fans and Gems(隐式图搜索+模拟)
Fans and Gems Input: Standard Input Output: Standard Output Tomy's fond of a game called 'Fans and G ...
- [HNOI2006]最短母串问题 --- AC自动机 + 隐式图搜索
[HNOI2006]最短母串问题 题目描述: 给定n个字符串(S1,S2.....,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,......,Sn)都是T的子串. 输入格式: 第 ...
- UVa 658 - It's not a Bug, it's a Feature!(Dijkstra + 隐式图搜索)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 【uva 658】It's not a Bug, it's a Feature!(图论--Dijkstra或spfa算法+二进制表示+类“隐式图搜索”)
题意:有N个潜在的bug和m个补丁,每个补丁用长为N的字符串表示.首先输入bug数目以及补丁数目.然后就是对M个补丁的描述,共有M行.每行首先是一个整数,表明打该补丁所需要的时间.然后是两个字符串,第 ...
- 洛谷 P2622 关灯问题II【状压DP;隐式图搜索】
题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯--按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j]为1,那么当这盏灯开了的时 ...
- uva10603-倒水问题-暴力枚举-隐式图搜索
题意: 给你三个杯子,a,b,c,没有刻度,刚开始c杯是满的,倒水的要求,要么倒出水的杯子倒空,要么倒入杯子倒满. 结果: 要求某个杯子内有d水量,并且倒出的水量最少,如果倒不出d水量,那么倒出d1( ...
- UVA - 10603 Fill(隐式图搜索)
题目大意:经典的倒水问题. 给你三个瓶子,体积为a,b,c. 刚開始a.b是空的,c是满的,如今要求你到出体积为d的水.倒水的规则为,要么倒水方为空,要么接水方满 问倒到容量为d时,倒水的最小体积是多 ...
- uva 310 L--system(隐式图搜索+字符串处理)
L-system A D0L (Deterministic Lindenmayer system without interaction) system consists of a finite ...
随机推荐
- 002.让CI4框架CodeIgniter显示错误信息
01. 在public目录的index.php中,添加以下内容: //定义环境为开发模式,可以输出各种错误信息 define('ENVIRONMENT', 'development'); 02.我们在 ...
- 007、MySQL日期取当前时间,取昨天
#取今天文本格式 SELECT DATE_SUB( curdate( ), INTERVAL DAY ); #取昨天文本格式 SELECT DATE_SUB( curdate( ), INTERVAL ...
- centos7上安装mysql8
话不多说仍然是更换虚拟机的系列安装. 一.首先下载最先版的mysql.到官网下载https://dev.mysql.com/downloads/file/?id=477146 下载后上传linux到相 ...
- Java8集合框架——HashSet源码分析
本文的目录结构: 一.HashSet 的 Javadoc 文档注释和简要说明 二.HashSet 的内部实现:内部属性和构造函数 三.HashSet 的 add 操作和扩容 四.HashSet 的 r ...
- [LeetCode] 932. Beautiful Array 漂亮数组
For some fixed N, an array A is beautiful if it is a permutation of the integers 1, 2, ..., N, such ...
- TensorFlow中的L2正则化函数:tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()的用法与异同
tf.nn.l2_loss()与tf.contrib.layers.l2_regularizerd()都是TensorFlow中的L2正则化函数,tf.contrib.layers.l2_regula ...
- oracle基础知识小结
一.查看oracle用户.角色信息1.查看所有用户 select * from all_users; select * from user_users; 2.查看用户或角色系统权限 select * ...
- 【剑指Offer】面试题07. 重建二叉树
题目 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 ...
- mybatis今年笔记
1.读取配置文件:用的就是解析Xml文件的技术 2.mybatis是支持自己写dao层的,但是没有必要. mybatis做的事情: 第一个创建代理对象,第二个在代理对象中调用方法. 3.相同的注解如果 ...
- P 1021 个位数统计
转跳点: