传送门

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. PHP简单post验证绕过

    if($_POST[user] && $_POST[pass]) { $conn = mysql_connect("*******", "****&quo ...

  2. windows组件

    Wcript.shell是windows自带的组件 因为他过于强大 所以经常被黑客加以利用 他可以调用系统内核运行的dos基本命令 与此对应的还有三个危险组件 他们分别是 FSO shell.Appl ...

  3. 09Mybatis_入门程序——删除用户以及更新用户

    删除用户: 还是前面的的案例,别的都不改,就修改两处地方.1.user.xml文件以及2.Mybatis_first.java文件 user.xml文件代码修改如下: <?xml version ...

  4. Windows Phone 简介

    中文官网 https://dev.windowsphone.com/zh-cn Windows Phone SDK 7.1 http://www.microsoft.com/zh-cn/downloa ...

  5. 泛型类型的协变(covariant)和逆变

    官网:http://msdn.microsoft.com/zh-cn/library/dd799517.aspx 原文链接:http://book.51cto.com/art/201112/30857 ...

  6. php基础27:文件写入

    <?php //1.打开一个文件 /* 第一个参数:打开的文件,第二个参数表明模式,w只写 如果打开的文件已经有了,那么删除这个文件,重新创建 如果没有,直接创建 fopen返回的是资源类型re ...

  7. sql语句or与union all的执行效率比较

    看到一篇文章是讲sql语句or与union all的执行效率比较的,以前没怎么注意这个问题,感觉文章写的不错,转来一看. 文章原链接:http://www.cunyoulu.com/zhuanti/q ...

  8. [CareerCup] 10.2 Data Structures for Large Social Network 大型社交网站的数据结构

    10.2 How would you design the data structures for a very large social network like Facebook or Linke ...

  9. 学习笔记——Maven实战(三)多模块项目的POM重构

    重复,还是重复 程序员应该有狗一般的嗅觉,要能嗅到重复这一最常见的坏味道,不管重复披着怎样的外衣,一旦发现,都应该毫不留情地彻底地将其干掉.不要因为POM不是产品代码而纵容重复在这里发酵,例如这样一段 ...

  10. MVC5 + EF6 + Bootstrap3 (12) 新建数据

    Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-create.html 系列教程:MVC5 + EF6 + ...