三个水杯 (bfs)
Input
接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3>0)表示三个水杯的体积。
第二行给出三个整数E1 E2 E3 (体积小于等于相应水杯体积)表示我们需要的最终状态
Output
Sample Input
2
6 3 1
4 1 1
9 3 2
7 1 1
Sample Output
3
-1
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std; int s1,s2,s3;
bool vis[][][]; struct node
{
int a,b,c;
int bu;
}x,y; int bfs()
{
queue<node> q;
while(!q.empty())
q.pop();
q.push(x);
while(!q.empty()){
node r=q.front();
q.pop();
vis[r.a][r.b][r.c]=true;
if(r.a==y.a&&r.b==y.b&&r.c==y.c) return r.bu; if(<r.a && r.b<s2){
node t=r;
int tt=min(t.a,s2-t.b);
t.a-=tt;
t.b+=tt;
if(vis[t.a][t.b][t.c]==false){
vis[t.a][t.b][t.c]=true;
t.bu++;
q.push(t);
}
} if(<r.a && r.c<s3){
node t=r;
int tt=min(t.a,s3-t.c);
t.a-=tt;
t.c+=tt;
if(vis[t.a][t.b][t.c]==false){
vis[t.a][t.b][t.c]=true;
t.bu++;
q.push(t);
}
} if(<r.b && r.a<s1){
node t=r;
int tt=min(t.b,s1-t.a);
t.b-=tt;
t.a+=tt;
if(vis[t.a][t.b][t.c]==false){
vis[t.a][t.b][t.c]=true;
t.bu++;
q.push(t);
}
} if(<r.b && r.c<s3){
node t=r;
int tt=min(t.b,s3-t.c);
t.b-=tt;
t.c+=tt;
if(vis[t.a][t.b][t.c]==false){
vis[t.a][t.b][t.c]=true;
t.bu++;
q.push(t);
}
} if(<r.c && r.a<s1){
node t=r;
int tt=min(t.c,s1-t.a);
t.c-=tt;
t.a+=tt;
if(vis[t.a][t.b][t.c]==false){
vis[t.a][t.b][t.c]=true;
t.bu++;
q.push(t);
}
} if(<r.c && r.b<s2){
node t=r;
int tt=min(t.c,s2-t.b);
t.c-=tt;
t.b+=tt;
if(vis[t.a][t.b][t.c]==false){
vis[t.a][t.b][t.c]=true;
t.bu++;
q.push(t);
}
}
}
return -;
} int main()
{
int n;
scanf("%d",&n);
while(n--){
memset(vis,false,sizeof(vis));
scanf("%d %d %d",&s1,&s2,&s3);
x.a=s1,x.b=,x.c=,x.bu=;
scanf("%d %d %d",&y.a,&y.b,&y.c);
if(s1<y.a+y.b+y.c){
printf("-1\n");
continue;
}
else{
printf("%d\n",bfs());
}
}
}
三个水杯 (bfs)的更多相关文章
- nyoj三个水杯(bfs)
三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互 ...
- nyoj 21三个水杯(BFS + 栈)
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=21 思想: 看了一下搜索就来写了这题(BFS 找出最短路径 所以用此来进行搜索) 这题在 ...
- NYOJ 21.三个水杯-初始态到目标态的最少次数-经典BFS
题目传送门:biubiubiu~ 三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子. ...
- NYOJ #21 三个水杯(bfs)
描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算.现在要求你写出一个程序,使其输出使初始状态到达目标 ...
- 三个水杯(BFS)
三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 给出三个水杯.大小不一,而且仅仅有最大的水杯的水是装满的,其余两个为空杯子. 三个水杯之间相互倒水,而且水杯 ...
- NYOJ 21 三个水杯
三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有 ...
- 三个水杯——java,广度优先搜索
题目如下: 21-三个水杯 内存限制:64MB 时间限制:1000ms 特判: No通过数:51 提交数:137 难度:4 题目描述: 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个 ...
- nyoj 三个水杯
三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有标识,只 ...
- 26-三个水杯(bfs)
三个水杯 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有 ...
随机推荐
- window忘记密码怎么办
net命令 Net User 功能:添加或更改用户帐号或显示用户帐号信息. 格式:net user [username [password | *] [options]] [/domain] ne ...
- HDU 3836 Equivalent SetsTarjan+缩点)
Problem Description To prove two sets A and B are equivalent, we can first prove A is a subset of B, ...
- 【DataStructure】The description of Java Collections Framework
The Java Connections FrameWork is a group of class or method and interfacs in the java.util package. ...
- 浅谈http请求数据分析
前段时间,我一个朋友给我打了个电话.说是现在在搞网络销售,问我能不能帮他整个自动发帖机.说实在的,以前没有弄过这块,我就跟他讲我试试看吧,能不能成不能保证.毕竟是搞程序的嘛,自学的能力还是有滴.经过一 ...
- 学习通过Thread+Handler实现非UI线程更新UI组件(转)
[Android线程机制] 出于性能考虑,Android的UI操作并不是线程安全的,这就意味着如果有多个线程并发操作UI组件,可能导致线程安全问题.为了解决这个问题,Android制定了一条简单的规则 ...
- Mean Shift简介
Mean Shift,我们 翻译“平均漂移”. 其集群,图像平滑. 图像分割和跟踪已广泛应用.因为我现在认为追踪,因此推出Mean Shift该方法用于目标跟踪.从而MeanShift較全面的介绍. ...
- (五岁以下儿童)NS3样本演示:桥模块演示样品csma-bridge.cc凝视程序
(五岁以下儿童)NS3:桥模块演示样品csma-bridge.cc凝视程序 1.Ns3 bridge模csma-bridge.cc演示示例程序的目光 // Network topology // // ...
- SQL server 2005 PIVOT运算符的使用
原文:SQL server 2005 PIVOT运算符的使用 PIVOT,UNPIVOT运算符是SQL server 2005支持的新功能之一,主要用来实现行到列的转换.本文主要介绍PIVOT运算符的 ...
- Web API 数据流使用
ASP.NET Web API 应用教程(一) ——数据流使用 相信已经有很多文章来介绍ASP.Net Web API 技术,本系列文章主要介绍如何使用数据流,HTTPS,以及可扩展的Web AP ...
- php 无错误提示 的解决方法
问:我在win7安装了PHP,浏览器是IE9.我代码写错了,浏览器一点错误提示都没有,一片空白.如果写对了,就能正常运行显示出来.请问这是怎么回事,应该怎么弄?你们两个的方法都试过,但都没有提示(注: ...