传送门

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. [原创]CI持续集成系统环境---部署Gitlab环境完整记录

    Gitlab是一个代码托管平台,在实际工作中,对代码管理十分有用. 废话不多说,下面是对我自己搭建的Gitlab环境做一记录: (1)安装 ------------------------------ ...

  2. CSS 布局调试工具

    说是工具其实只是一段 Javascript 代码,但非常实用,它会给页面里所有的 DOM 元素添加一个 1px 的描边(outline),方便我们在调试 CSS 过程中分析.排查问题. 先来看看代码, ...

  3. .NET 知识

    1.读懂IL代码就这么简单 IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的 ...

  4. Windows Phone:如何检查WMAppManifest中的Capability属性

    在Windows Phone应用中有一个应用程序清单(WMAppManifest.xml),其中对于不同的应用可以设定Capability来告知需要哪些特性或功能,详细内容可以参考官方文档: http ...

  5. 补鞋匠---Cobbler 服务器自动搭建

    Cobbler 服务器自动搭建http://tshare365.com/archives/439.html

  6. U3D assetbundle加载

    using UnityEngine; using System.Collections; public class testLoadFromAB : MonoBehaviour { IEnumerat ...

  7. Go视频教程整理

    [Go Web基础]01博客项目设计 |Go视频教程|Go语言基础 http://www.tudou.com/programs/view/gXZb9tGNsGU/ [Go Web基础]02初窥 Web ...

  8. JS replace()方法-字符串首字母大写

    replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. replace()方法有两个参数,第一个参数是正则表达式,正则表达式如果带全局标志/g,则是代表替换 ...

  9. JDSideMenu实现(整块)侧滑功能,主视图会和状态栏(StatusBar)会一起滑动。

    JDSideMenu 实现侧边菜单功能,支持手势滑动.跟一般的侧边菜单不一样的是,滑动主视图,主视图会和状态栏(StatusBar)会一起滑动. demo 自行下载

  10. Android Studio之gradle的配置与介绍

    1.gradle的简单介绍 Gradle是可以用于Android开发的新一代的Build System,也是Android Studio默认的build工具.其实Gradle脚本是基于一种JVM语言- ...