博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SPOJ Pouring Water
阅读量:4326 次
发布时间:2019-06-06

本文共 2676 字,大约阅读时间需要 8 分钟。

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:

2523234

 

Sample output:

2-1 ------------------------ Solution BFS 写BFS最重要的是避免同一状态重复入队。 另外检查目标状态应该在每个状态出队时进行,因为状态的出口是“唯一”的,而入口一般有多种情况(即由队首状态一般可转移到多个新状态),注意代码中加粗的那行。 另外由于问题中由初始状态可转移到的状态并不多(也由于二维数组开不下),应当用map存每个状态到初始状态的距离(及所需的最少转移步数)。 还有一个技巧就是将enqueue写成一个函数,这样就避免了向多个状态转移带来的代码重复。
#include 
using namespace std;typedef pair
P;int gcd(int a, int b){
return b?gcd(b, a%b):a;}map
mp;queue

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

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_37、分布式缓存Redis介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_42、SpringBoot常用定时任务配置实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_39、SpringBoot2.x整合redis实战讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第14节 高级篇幅之SpringBoot多环境配置_59、SpringBoot多环境配置介绍和项目实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_41、SpringBoot定时任务schedule讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第10节 SpringBoot整合定时任务和异步任务处理_43、SpringBoot2.x异步任务实战(核心知识)...
查看>>
小D课堂 - 新版本微服务springcloud+Docker教程_1_01课程简介
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战...
查看>>