SPOJ Pouring Water
POUR1 - Pouring water
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
------------------------ Solution
BFS
写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}]=;
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-); return;}
if(x) enque(, y, d+);
if(y) enque(x, , d+);
if(x!=a){
enque(a, y, d+);
if(y){
int add=min(y, a-x);
enque(x+add, y-add, d+);
}
}
if(y!=b){
enque(x, b, d+);
if(x){
int add=min(x, b-y);
enque(x-add, y+add, d+);
}
}
}
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(""); continue;}
if(a==b){puts("-1"); continue;}
if(c%gcd(a,b)){puts("-1"); continue;}
bfs(, , a, b, c);
}
}
----------------------------------------
写这题时把main()里的几个continue全写成return了,竟然过了样例,果然样例没有不坑的。以后debug时要留心有没有continue写成return的地方。
SPOJ Pouring Water的更多相关文章
- What Is Mathematics?
What Is Mathematics? The National Council of Teachers of Mathematics (NCTM), the world's largest org ...
- halcon算子
halcon的算子列表 Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样 ...
- 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数013,shape模型
<zw版·Halcon-delphi系列原创教程> Halcon分类函数013,shape模型 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“pr ...
- 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数006, image,影像处理(像素图)
<zw版·Halcon-delphi系列原创教程> Halcon分类函数006, image,影像处理(像素图) 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“* ...
- halcon的算子列表
Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训 ...
- 《zw版·delphi与halcon系列原创教程》zw版_THOperatorSetX控件函数列表 v11中文增强版
<zw版·delphi与halcon系列原创教程>zw版_THOperatorSetX控件函数列表v11中文增强版 Halcon虽然庞大,光HALCONXLib_TLB.pas文件,源码就 ...
- Codeforces Round #218 (Div. 2) D. Vessels
D. Vessels time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...
- sicily 1027 MJ, Nowhere to Hide 字符串匹配与排序
1027. MJ, Nowhere to Hide Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description On BBS, th ...
- Meditation Guide
Meditation “Stop!!!” don’t we just scream[vi. 尖叫:呼啸:发出尖锐刺耳的声音:令人触目惊心 ] this in our minds when the da ...
随机推荐
- js原生捕鱼达人(三)--完结
先给分享下我写完的效果,github有点卡,我没有压缩代码,不过效果可以看到 https://jasonwang911.github.io/ 转载请注明'转载于Jason齐齐的博客http://www ...
- linux如何挂载windows下的共享文件
说明:windows下有一共享文件夹APP,windows本地ip是192.168.9.155现在需要在linux服务器上挂载这个APP文件夹,linux服务器ip是192.168.9.200 操作记 ...
- Web项目构建
Gradle为Web开发提供了两个插件,war和jetty apply plugin: 'war' apply plugin: 'jetty' war插件继承了java插件,jetty插件继承了war ...
- [Usaco2010 OPen]Triangle Counting 数三角形
[Usaco2010 OPen]Triangle Counting 数三角形 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 394 Solved: 1 ...
- 【原创】有关Silverlight DataGrid双击事件的分析 完整分析 代码与示例
公司项目用的silverlight,而且silverlight一些技术 资料比较少.所以分享出来 给大家参考参考. 有关Silverlight中DataGrid 双击事件 的代码 如下: 1. 前台x ...
- SSH公钥认证+优化
一 ssh公钥认证流程: sshclinet机器:产生公私钥(公钥相当于一把锁) sshclient:将公钥发给sshserver(抛出锁子) sshclinet去连sshserver不需要密钥 ...
- use AP_VENDOR_PUB_PKG.Update_Vendor_Site_Public to u ORA-01722: invalid number in Package AP_VENDOR_PUB_PKG Procedure Update_Vendor_Site_Public
ORA-01722: invalid number in Package AP_VENDOR_PUB_PKG Procedure Update_Vendor_Site_Public 发现此问题的经过: ...
- pandas 前后行操作
一.前后行满足条件 问题: 各位老师好,我有一个dataframe 产品 数据1 数据2 A 1 2 B 4 5 C 6 3 我想找出比如这一行数据1>数据2 AND 数据1的上一行3 AND ...
- GEOS库的学习之二:简单几何图形的创建
几何图形(Geometry)是geos里面基本的操作对象,因此Geometry类就是最重要的一个类 几何图形中主要有三个要素:点,线,面.横纵坐标构成点,多个点构成线,环线构成面,点线面混合构成几何集 ...
- livewriter写Blog 神秘失踪?
现在习惯用livewriter来总结/记录一些知识并发布为Blog 与同行交流,但是今天发生了一个怪事,上午我整理了两篇文档当时就用livewriter发送到了Blog上,但是晚上来看的时候之前发送的 ...