传送门

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的更多相关文章

  1. What Is Mathematics?

    What Is Mathematics? The National Council of Teachers of Mathematics (NCTM), the world's largest org ...

  2. halcon算子

    halcon的算子列表   Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样 ...

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

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

  4. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数006, image,影像处理(像素图)

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数006, image,影像处理(像素图) 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“* ...

  5. halcon的算子列表

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

  6. 《zw版·delphi与halcon系列原创教程》zw版_THOperatorSetX控件函数列表 v11中文增强版

    <zw版·delphi与halcon系列原创教程>zw版_THOperatorSetX控件函数列表v11中文增强版 Halcon虽然庞大,光HALCONXLib_TLB.pas文件,源码就 ...

  7. 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 ...

  8. sicily 1027 MJ, Nowhere to Hide 字符串匹配与排序

    1027. MJ, Nowhere to Hide Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description On BBS, th ...

  9. Meditation Guide

    Meditation “Stop!!!” don’t we just scream[vi. 尖叫:呼啸:发出尖锐刺耳的声音:令人触目惊心 ] this in our minds when the da ...

随机推荐

  1. CSS padding margin border属性

    主要定义四个区域:内容(content).内边距(padding).边框(border)和外边距(margin) margin:层的边框以外留的空白 background-color:背景颜色 bac ...

  2. 测试驱动开发实践 - Test-Driven Development(转)

    一.前言 不知道大家有没听过“测试先行的开发”这一说法,作为一种开发实践,在过去进行开发时,一般是先开发用户界面或者是类,然后再在此基础上编写测试. 但在TDD中,首先是进行测试用例的编写,然后再进行 ...

  3. MVC3学习:利用mvc3+ajax实现登录

    用到的工具或技术:vs2010,EF code first,JQuery ajax,mvc3. 第一步:准备数据库. 利用EF code first,先写实体类,然后根据实体类自动创建数据库:或者先创 ...

  4. IOS开发之—— 各种加密的使用(MD5,base64,DES,AES)

    基本的单向加密算法: BASE64 严格地说,属于编码格式,而非加密算法 MD5(Message Digest algorithm 5,信息摘要算法)SHA(Secure Hash Algorithm ...

  5. ios 解析html

    xml,json都有大量的库来解析,我们如何解析html呢? TFHpple是一个小型的封装,可以用来解析html,它是对libxml的封装,语法是xpath.今天我看到一个直接用libxml来解析h ...

  6. 千万不要在JS中使用连等赋值操作

    前言 文章标题这句话原本是在国外某JavaScript规范里看到的,当时并没有引起足够的重视,直到最近一次出现了bug发现JS里的连等赋值操作的特色(坑). 网上搜索一番发现一个非常好的连等赋值的(来 ...

  7. js中的运动

    缓慢隐藏与出现 效果: 鼠标移至分享上黄色区域自动向左隐藏. <!DOCTYPE html> <html> <head> <title></tit ...

  8. JavaScript事件---事件对象

    发文不易,若转载传播,请亲注明出处,谢谢!   内容提纲: 1.事件对象 2.鼠标事件 3.键盘事件 4.W3C与IE JavaScript事件的一个重要方面是它们拥有一些相对一致的特点,可以给你的开 ...

  9. Gson解析Json格式数据

    //数据定义:=========================================== class User{ String name; String password; String ...

  10. 使用gitlab+jenkins+saltstack+rsync自动部署Web应用

    转载:http://www.ithao123.cn/content-8128849.html