三个水杯

时间限制:1000 ms  |  内存限制:65535 KB
难度:4
 
描述
给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子。三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算。现在要求你写出一个程序,使其输出使初始状态到达目标状态的最少次数。
 
输入
第一行一个整数N(0<N<50)表示N组测试数据
接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3>0)表示三个水杯的体积。
第二行给出三个整数E1 E2 E3 (体积小于等于相应水杯体积)表示我们需要的最终状态
输出
每行输出相应测试数据最少的倒水次数。如果达不到目标状态输出-1
样例输入
2
6 3 1
4 1 1
9 3 2
7 1 1
样例输出
3
-1 虽然一看就知道用广度优先搜索,但是具体怎么做却还需要动一番脑筋。
代码如下
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct Bottle {
int v[];
int step;
Bottle() {};
Bottle(int u, int v, int w, int s) {
this->v[] = u; this->v[] = v; this->v[] = w;
step = s;
}
}; queue <Bottle> bot;
int visit[][][]; bool isEqual(Bottle p, int e[]) {
for(int i = ; i < ; i++) {
if(p.v[i] != e[i]) {
return false;
}
}
return true;
}
int main(int argc, char const *argv[])
{
int N;
scanf("%d",&N);
while(N--) {
int v[];
int e[];
scanf("%d %d %d",&v[],&v[],&v[]);
scanf("%d %d %d",&e[],&e[],&e[]); while(!bot.empty()) {
bot.pop();
}
memset(visit, , sizeof(visit));
bot.push(Bottle(v[],,,)); int ans = -;
while(!bot.empty()) {
Bottle p = bot.front();
bot.pop();
if(isEqual(p,e))
{
ans = p.step;
break;
}
else {
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
int k = (i+j)%;
//from i to k
if(p.v[i] > && p.v[k] < v[k]) {
int cha = v[k] - p.v[k];
int pour = min(p.v[i], cha);
Bottle t;
t.v[i] = p.v[i] - pour;
t.v[k] = p.v[k] + pour;
t.v[-i-k] = p.v[-i-k];
t.step = p.step+;
if(visit[t.v[]][t.v[]][t.v[]] != ) {
visit[t.v[]][t.v[]][t.v[]] = ;
bot.push(t);
}
}
}
}
}
}
printf("%d\n", ans); }
return ;
}

最新文章

  1. Android调用webservice的例子
  2. classes system in sencha touch
  3. C# Winform WindowsMediaPlayer控件
  4. Tsinghua dsa pa2
  5. 消除“Permission is only granted to system apps”错误
  6. Python之FTP多线程下载文件之分块多线程文件合并
  7. DotNet加密方式解析--非对称加密
  8. freemarker常见语法大全
  9. 【iOS】Swift GCD-下
  10. 2、SpringBoot接口Http协议开发实战8节课(7-8)
  11. js基础梳理-究竟什么是变量对象,什么是活动对象?
  12. [转]devm_gpiod_get_optional用法
  13. Windows8下安装ubuntu
  14. Codeforces 987 F - AND Graph
  15. 用光的微粒说和广义相对论来解释衍射现象 Explanation of Diffraction Phenomenon by Particle Theory of Light and General Relativity
  16. SQL 实践和技巧 &lt;2&gt;
  17. vue之Mutations 理解
  18. FreeRTOS 低功耗之睡眠模式
  19. React 组件间通信
  20. DBGRID控件里可以实现SHIFT复选吗?怎么设置?

热门文章

  1. mysql中locate和substring函数使用
  2. 继承FileInputFormat类来理解 FileInputFormat类
  3. kernighan lin算法
  4. ADO.NET 之断开连接层
  5. iOS之旅--隐藏(去除)导航栏底部横线
  6. springboot整合activiti报错[processes/]不存在解决方案
  7. Open closed principle
  8. ElasticSearch High Level REST API【7】聚合
  9. 连接MYSQL 错误代码2003
  10. RocketMQ源码分析之RocketMQ事务消息实现原理中篇----事务消息状态回查