三个水杯 (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 描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子.三个水杯之间相互倒水,并且水杯没有 ...
随机推荐
- Spring+Mybatis+SpringMVC后台与前台分页展示实例(附工程)(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文实现了一个后台由Spring+Mybatis+SpringMVC组成,分页采用Pag ...
- uva 1331 - Minimax Triangulation(dp)
option=com_onlinejudge&Itemid=8&page=show_problem&category=514&problem=4077&mosm ...
- Web Reference for a WCF Service has Extra “IdSpecified” Parameter ?
Question: I created a WCF service that exposed a method that has one paramater: public class Service ...
- mtk硬件项目开始关闭蓝牙功能:mtk 硬件ScanCode和keycode应用演示示例
项目要求:该项目因为没有使用android5.0,导致启动bluetooth的蓝牙audio slave功能必须使用第三方模组,该第三方模组,启动是通过android主板通过GPIO控制.UI界面是通 ...
- POJ 1798 Truck History
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- Source Insight 3.X 插件支持utf8,完美解决中国乱码,连接到美丽的轮廓
上次SI多标签插件之后,由于公司内部编码改为utf8编码,因此特意做了这个Source Insight 3.X utf8插件. 下载地址:http://pan.baidu.com/s/1mgyZous ...
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...
- 学习pthreads,创建和终止多线程
更CPU多线程编程,通过笔者的研究发现,,pthreads使用日趋广泛.它是螺纹POSIX标准,它定义了一组线程的创建和操作API. 配置环境见上博客文章.配置环境后,只需要加入#include &l ...
- php+sqlite 最佳web服务器
1 wampserver 支持mysql.每次都启动mysql,可以手动停止.但是运行时有时会很慢. 放弃 2 APS绿色版(Apache+PHP+SQLite) 组件环境:Apache2.2. ...
- SDUT 2933-人活着系列Streetlights(最小生成树Kruskal+和理查德设置来实现)
人活着系列之Streetlights Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 人活着假设是为了家庭,亲情----能够说 ...