传送门

POUR1 - Pouring water

#gcd #recursion

Given two vessels, one of which can accommodate a litres of water and the other - b litres of water, determine the number of steps required to obtain exactly c litres of water in one of the vessels.

At the beginning both vessels are empty. The following operations are counted as 'steps':

  • emptying a vessel,
  • filling a vessel,
  • pouring water from one vessel to the other, without spilling, until one of the vessels is either full or empty.

Input

An integer t, 1<=t<=100, denoting the number of testcases, followed by t sets of input data, each consisting of three positive integers a, b, c, not larger than 40000, given in separate lines.

Output

For each set of input data, output the minimum number of steps required to obtain c litres, or -1 if this is impossible.

Example

Sample input:

2
5
2
3
2
3
4

Sample output:

2
-1------------------------

SolutionBFS写BFS最重要的是避免同一状态重复入队。另外检查目标状态应该在每个状态出队时进行,因为状态的出口是“唯一”的,而入口一般有多种情况(即由队首状态一般可转移到多个新状态),注意代码中加粗的那行。另外由于问题中由初始状态可转移到的状态并不多(也由于二维数组开不下),应当用map存每个状态到初始状态的距离(及所需的最少转移步数)。还有一个技巧就是将enqueue写成一个函数,这样就避免了向多个状态转移带来的代码重复。
#include <bits/stdc++.h>
using namespace std;typedef pair<int,int> P;
int gcd(int a, int b){return b?gcd(b, a%b):a;}
map<P,int> mp;
queue<P> que;
void enque(int a, int b, int d){int tmp=mp[{a, b}];if(!tmp||tmp>d){mp[{a, b}]=d;que.push({a, b});}
}
bool ok(int a, int b, int c){return a==c||b==c;}
// how BFS works?
void bfs(int x, int y, int a, int b, int c){mp.clear();while(!que.empty()) que.pop();mp[{x, y}]=1;que.push({x, y});while(!que.empty()){P top=que.front(); que.pop(); int d=mp[top];int x=top.first, y=top.second;if(x==c||y==c){printf("%d\n", d-1); return;}if(x) enque(0, y, d+1);if(y) enque(x, 0, d+1);if(x!=a){enque(a, y, d+1);if(y){int add=min(y, a-x);enque(x+add, y-add, d+1);}}if(y!=b){enque(x, b, d+1);if(x){int add=min(x, b-y);enque(x-add, y+add, d+1);}}}  puts("-1");
}
int main(){int T; scanf("%d", &T);for(int a, b, c; T--;){scanf("%d%d%d", &a, &b, &c);if(c>max(a, b)){puts("-1"); continue;}if(c==a||c==b){puts("1"); continue;}if(a==b){puts("-1"); continue;}if(c%gcd(a,b)){puts("-1"); continue;}bfs(0, 0, a, b, c);}
}

----------------------------------------

写这题时把main()里的几个continue全写成return了,竟然过了样例,果然样例没有不坑的。以后debug时要留心有没有continue写成return的地方。

转载于:https://www.cnblogs.com/Patt/p/4824875.html

SPOJ Pouring Water相关推荐

  1. Meditation Guide

    Meditation "Stop!!!" don't we just scream[vi. 尖叫:呼啸:发出尖锐刺耳的声音:令人触目惊心 ] this in our minds w ...

  2. 我的世界java刷怪数量_Minecraft我的世界Java版18w16a更新发布

    Minecraft我的世界Java版18w16a更新发布!Minecraft 1.13 仍未发布,18w16a为其第32个预览版.目前新版本已经基本完成了所有特性,现在更专注于漏洞修复和细节打磨了! ...

  3. 【CodeForces - 371D】Vessels(思维,元素合并,并查集)

    题干: There is a system of n vessels arranged one above the other as shown in the figure below. Assume ...

  4. halcon 相似度_Halcon分类函数,shape模型

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数013,shape模型 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化::: 用符号"**&quo ...

  5. Halcon 常用算子使用场合

    Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训 ...

  6. Halcon算子解释大全

                                             Halcon算子解释大全 Halcon/Visionpro视频教程和资料,请访问 重码网,网址: http://www ...

  7. Halcon一些功能算子

    Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训 ...

  8. halocn标定找旋转中心_HALOCN运算功能函数快查 | 学步园

    虽然绝大部分时间都是在VC中使用HALCON,HALCON的本身运算符很大都不会用到.但是这个资料还是很全.记录备查. Chapter 1 :Classification 1.1 Gaussian-M ...

  9. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数013,shape模型

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数013,shape模型 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号"**&qu ...

  10. 今天许多的家庭有计算机英语,用英语写我的家庭作文3篇

    用英语写我的家庭作文3篇 家不仅仅是一幢房子,它是漂泊者的避风港,是心灵的驿站,简而言之,它也是一种真正属于自己的生活方式,我的亲人,我的家.下面小编为大家带来有关我的家庭英语作文范文带翻译,欢迎大家 ...

最新文章

  1. 未安装在此服务器场中,无法添加到该范围
  2. python3 tensorflowprint错误_解决import tensorflow as tf 出错的原因
  3. QT的QTransform类的使用
  4. Python 基础函数
  5. java代码中获取classpath路径
  6. 并查集算法学习(转)
  7. php tr td,php-基于tr计数的Td / th的XPath
  8. IDEA添加项目启动配置
  9. grub rescue
  10. 微信消息记录导出到电脑
  11. vs2015 :“64位调试操作花费的时间比预期要长“,无法运行调试解决办法
  12. 农商银行计算机岗笔试题,广东农商银行金融科技岗笔试考什么?
  13. 计算机电源指示灯量一下就灭,开机硬盘指示灯闪了一下就不亮是怎么回事
  14. 在git bash中输入git init 提示错误信息: fatal: open /dev/null or dup failed: No such file or directory的解决办法
  15. 单、多通道图像反差处理
  16. 在职研究生读计算机专业,读计算机专业在职研究生让我择业自如高升有望
  17. try{}里有一个return语句,那么紧跟在这个try后的finally{}里的代码会不会被执行
  18. c# 向Excel文件写入数据(Workbook 和Worksheet )
  19. ET框架关于opCode的理解
  20. TestNG测试报告美化buid.xml配置

热门文章

  1. rhel linux 自动 fsck,red hat as 4 启动报错:checking filesystems fsck.ext3: bad magic number ......
  2. pycharm Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon run
  3. DeepFake技术--DeepFakes 概述(一)(二)
  4. python使用递归、尾递归、循环三种方式实现斐波那契数列
  5. hashmap相同的key会覆盖吗_【简单了解系列】从基础的使用来深挖HashMap
  6. C++ 单元测试框架 Boost Test BOOST_AUTO_TEST_CASE
  7. 【Django 2021年最新版教程30】django项目部署到华为云(nginx uWSGI mysql方式)
  8. 微信小程序云开发教程-JavaScript入门(5)-函数异步同步
  9. 微信小程序云开发教程-JavaScript入门(1)-学习环境、函数、调试
  10. php字符串怎么判断是否相等,php判断两个字符串是否相等