uva 10603 倒水问题
来源:互联网 发布:淘宝网推广方法ppt 编辑:程序博客网 时间:2024/05/16 15:39
刘汝佳那本书上的代码有些问题,判断当前三个瓶子的水量状态是否被访问过后,还要判断当前状态的使用水量是否比之前的要少。如果少那么需要更新到达该状态的水量。
但是奇怪的是,缺少这一判断的代码依旧能够通过测试,目前不知道是由于缺少相应测试还是个人想法错了
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string>#include <string.h>#include <algorithm>#include <queue>using namespace std;int cap[3], d;struct Node{int val[3];int dist;Node(int a = 0, int b = 0, int c = 0){val[0] = a;val[1] = b;val[2] = c;}};bool operator < (const Node& a, const Node& b){return a.dist > b.dist;}const int maxn = 200 + 10;bool state[maxn][maxn];int res[maxn];void update_dist(const Node & node){for (int i = 0; i < 3; i++){if (res[node.val[i]] < 0 || res[node.val[i]] > node.dist)res[node.val[i]] = node.dist;}}void work(){memset(res, -1, sizeof(res));memset(state, false, sizeof(state));priority_queue<Node> que;Node start(0, 0, cap[2]);start.dist = 0;que.push(start);state[0][0] = true;while (!que.empty()){Node nt = que.top();que.pop();update_dist(nt);for (int i = 0; i < 3; i++){for (int j = 0; j < 3; j++){if (i == j || !nt.val[i] || nt.val[j] == cap[j])continue;int water_amount = min(cap[j], nt.val[i] + nt.val[j]) - nt.val[j];Node ssnt;memcpy(&ssnt, &nt, sizeof(Node));ssnt.val[i] -= water_amount;ssnt.val[j] += water_amount;ssnt.dist += water_amount;if (!state[ssnt.val[0]][ssnt.val[1]]){state[ssnt.val[0]][ssnt.val[1]] = true;que.push(ssnt);}elseupdate_dist(ssnt); // 这个更新存在与否都能够通过过测试, 目前没有想清楚}//for int j}//for int i}//while que.empty()while (d >= 0){if (res[d] >= 0){printf("%d %d\n", res[d], d);return;}d--;}}int main(){freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);int kasenum;scanf("%d", &kasenum);while (kasenum--){for (int i = 0; i < 3; i++)scanf("%d", &cap[i]);scanf("%d", &d);work();}//whilereturn 0;}
阅读全文
0 0
- uva 10603 倒水问题
- UVA 10603 Fill倒水问题
- 倒水问题(Fill,UVa 10603)
- 倒水问题(Fill,UVa 10603)
- uva 10603 Fill(倒水问题 BFS)
- Fill (Uva 10603 bfs 倒水问题)
- UVA 10603 Fill(倒水问题)
- Uva 倒水问题
- uva 10603 倒水 bfs
- UVa 10603 倒水
- 10603UVA倒水
- uva 571 - Jugs(倒水问题)
- UVa 10603 - Fill,经典倒水问题+隐式图搜索+dfs
- uva 10603倒水问题(搜索 隐式图的最短路 )
- 例题7-8 UVA 10603 Fill 倒水问题
- UVA 10603 Fill(状态空间搜索,倒水问题)
- UVA-10603 Fill 倒水问题 BFS+优先队列
- uva 10603 三个杯子倒水问题 BFS搜索
- 泛型
- sge 启动和重启命令
- jar文件
- NYOJ:76-超级台阶
- ROS使用过程中出现的问题:由于Anaconda的Python路径问题造成rqt_graph报错
- uva 10603 倒水问题
- 我学习python的常用工具或网站
- get和post
- 这个暑假的展望
- 2017/06/15 榕基面试题
- 见习报告
- Python常用软件安装
- 欢迎使用CSDN-markdown编辑器
- Android切换Fragment时视频依旧在播放,没有正常的停止。