nyoj21 三个水杯 (BFS)
2016-03-19 10:19
369 查看
题目21
题目信息
运行结果
本题排行
讨论区
难度:4
描述 给出三个水杯,大小不一,并且只有最大的水杯的水是装满的,其余两个为空杯子。三个水杯之间相互倒水,并且水杯没有标识,只能根据给出的水杯体积来计算。现在要求你写出一个程序,使其输出使初始状态到达目标状态的最少次数。
输入第一行一个整数N(0<N<50)表示N组测试数据
接下来每组测试数据有两行,第一行给出三个整数V1 V2 V3 (V1>V2>V3 V1<100 V3>0)表示三个水杯的体积。
第二行给出三个整数E1 E2 E3 (体积小于等于相应水杯体积)表示我们需要的最终状态输出每行输出相应测试数据最少的倒水次数。如果达不到目标状态输出-1样例输入
样例输出
来源经典题目上传者
hzyqazasdf
记得一年前看这道题 感觉说的是什么啊 完全看不懂 没有想法
前天有时间再看看 想用dfs做了 可是做的过程中发现了许多问题 以至于我没有做出来
看到评论区有人用bfs做 就用bfs了
题目信息
运行结果
本题排行
讨论区
三个水杯
时间限制: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
来源经典题目上传者
hzyqazasdf
记得一年前看这道题 感觉说的是什么啊 完全看不懂 没有想法
前天有时间再看看 想用dfs做了 可是做的过程中发现了许多问题 以至于我没有做出来
看到评论区有人用bfs做 就用bfs了
#include <stdio.h> #include <queue> #include <string.h> using namespace std; struct node { int step; int state[3]; friend bool operator<(node x,node y) { return x.step>y.step; } }; //当前三个水杯的状态 int curState[3]; //三个水杯目标状态 int endState[3]; //三个水杯的最大容量 int maxState[3]; bool vis[101][101][101]; //i向j倒水 void pourWater(int i,int j) { //如果j水杯是满的 或者 i水杯没水 返回 if(curState[j]==maxState[j]||curState[i]==0) return ; //如果i水杯当前的水容量大于j水杯空着的容量 if(curState[i]>=maxState[j]-curState[j]) { curState[i]=curState[i]-(maxState[j]-curState[j]); curState[j]=maxState[j]; } //如果i水杯当前的水容量小于水杯空着的容量 else { curState[j]+=curState[i]; curState[i]=0; } } int bfs() { priority_queue<node>s; while(!s.empty()) s.pop(); node temp,temp1; temp.state[0]=maxState[0]; temp.state[1]=temp.state[2]=temp.step=0; vis[maxState[0]][0][0]=true; s.push(temp); while(!s.empty()) { temp=temp1=s.top(); s.pop(); if(temp.state[0]==endState[0]&&temp.state[1]==endState[1]&&temp.state[2]==endState[2]) return temp.step; curState[0]=temp.state[0]; curState[1]=temp.state[1]; curState[2]=temp.state[2]; for(int i=0;i<3;i++) { for(int j=0;j<3;j++) { if(i!=j&&curState[i]!=0&&curState[j]!=maxState[j]) { pourWater(i,j); temp.state[0]=curState[0]; temp.state[1]=curState[1]; temp.state[2]=curState[2]; temp.step++; if(!vis[curState[0]][curState[1]][curState[2]]) { vis[curState[0]][curState[1]][curState[2]]=true; s.push(temp); } temp=temp1; curState[0]=temp.state[0]; curState[1]=temp.state[1]; curState[2]=temp.state[2]; } } } } return -1; } int main() { int ncase; scanf("%d",&ncase); while(ncase--) { memset(vis,false,sizeof(vis)); scanf("%d %d %d",&maxState[0],&maxState[1],&maxState[2]); scanf("%d %d %d",&endState[0],&endState[1],&endState[2]); printf("%d\n",bfs()); } }
相关文章推荐
- 怎么给电脑统一设置查看文件为列表形式?
- Android开发之WebService介绍
- cocos2dx和quick的区别
- 关闭win7窗口自动排列功能的方法
- CAShapeLayer
- 2016年:如何测试您的网站响应时间
- 结对开发-四则运算3
- 结对开发——四则运算(三)
- dfs回溯的注意事项
- mt7688 音频方案分析
- 动态规划入门之国王的金矿
- Java中构造函数重构+数组小练习
- 索尼PS VR游戏体验汇总 399美元起高端VR究竟值不值得买?
- java程序中比较常见的四种判断是否为空的性能优化比较
- 21.文件下载
- 【摘录+总结】Android动画之Tween动画 (渐变、缩放、位移、旋转)
- HttpClient使用详解
- 计算机网络常识
- 关于一些基础的Java问题的解答(五)
- easyUI接收后台的参数不正确导致报length no defined