hdu1415和poj 1415 Jugs
来源:互联网 发布:高性能网络编程 陶辉 编辑:程序博客网 时间:2024/04/30 13:15
http://acm.hdu.edu.cn/showproblem.php?pid=1415
这个hdu的题目有问题,应该是把b的水变为t,第一次wr了,看第二个例子就可以看出是把b的水变为t。浙江大学oj是正确的。
用bfs遍历,用一个二维数组保存前面的状态和倒水的方式就行,1-6分别代表6种不同的倒水方式。就可以解决,但是还是用了1个小时而且代码比较冗长。。。。
#include<iostream>#include<queue>using namespace std;int a, b, t;struct node{ int a,b,p;}s[1002][1002];void show(int x,int y){ if (s[x][y].a >= 0 && s[x][y].b>=0) { show(s[x][y].a, s[x][y].b); switch (s[x][y].p){ case 1:cout << "fill A" << endl; break; case 2:cout << "fill B" << endl; break; case 3:cout << "empty A" << endl; break; case 4:cout << "empty B" << endl; break; case 5:cout << "pour A B" << endl; break; case 6:cout << "pour B A" << endl; break; } }}void bfs(){ queue<node> qu; node now = { 0,0,0}; qu.push(now); s[0][0].a = s[0][0].b = -1; s[0][0].p = 0; while (qu.size()>0) { node temp; now = qu.front(); qu.pop(); if (now.b == t){ show(now.a, now.b); return; } if (now.a != a){ temp.a = a; temp.b = now.b; if (s[temp.a][temp.b].p== -1){ s[temp.a][temp.b].p = 1; s[temp.a][temp.b].a = now.a; s[temp.a][temp.b].b = now.b; qu.push(temp); } } if (now.b != b){ temp.a = now.a; temp.b = b; if (s[temp.a][temp.b].p == -1){ s[temp.a][temp.b].p = 2; s[temp.a][temp.b].a = now.a; s[temp.a][temp.b].b = now.b; qu.push(temp); } } if (now.a != 0){ temp.a = 0; temp.b = now.b; if (s[temp.a][temp.b].p == -1){ s[temp.a][temp.b].p = 3; s[temp.a][temp.b].a = now.a; s[temp.a][temp.b].b = now.b; qu.push(temp); } } if (now.b != 0){ temp.a = now.a; temp.b = 0; if (s[temp.a][temp.b].p == -1){ s[temp.a][temp.b].p = 4; s[temp.a][temp.b].a = now.a; s[temp.a][temp.b].b = now.b; qu.push(temp); } } if (now.a != 0 && now.b != b){ if (now.a >= b - now.b){ temp.b = b; temp.a = now.a - (b - now.b); } else { temp.a = 0; temp.b = now.a + now.b; } if (s[temp.a][temp.b].p == -1){ s[temp.a][temp.b].p = 5; s[temp.a][temp.b].a = now.a; s[temp.a][temp.b].b = now.b; qu.push(temp); } } if (now.a != a && now.b != 0){ if (now.b >= a - now.a){ temp.a = a; temp.b = now.b - (a - now.a); } else { temp.b = 0; temp.a = now.a + now.b; } if (s[temp.a][temp.b].p == -1){ s[temp.a][temp.b].p = 6; s[temp.a][temp.b].a = now.a; s[temp.a][temp.b].b = now.b; qu.push(temp); } } }}int main(){ while (cin >> a >> b >> t) { for (int i = 0; i <= a; i++){ for (int j = 0; j <= b; j++){ s[i][j].p = -1; } } bfs(); cout << "success" << endl; } return 0;}
0 0
- hdu1415和poj 1415 Jugs
- hdu1415-Jugs
- poj 1606 || hdu 1415 Jugs
- POJ 1606 && HDU 1415 Jugs(机智)
- poj 1606 Jugs
- poj 1606 Jugs (BFS)
- POJ 1606 Jugs
- poj 1606 Jugs
- POJ 1606 Jugs
- poj 1606 Jugs
- POJ-1606 Jugs
- POJ 1606 Jugs 倒水问题
- hdu 1415 Jugs
- Jugs
- Jugs
- Jugs
- Jugs
- ZOJ 1005 POJ 1606 Jugs /数论
- Java常用类库——String
- 蓝桥杯- 入门训练 Fibonacci数列
- SVN随笔
- Flash通过RTMP协议连接到RED5服务器发生不信任的本地SWF文件可能不能联系Internent
- Android 通过JNI实现守护进程,使得Service服务不被杀死
- hdu1415和poj 1415 Jugs
- 蓝桥杯 2015校内选拔试题 第二题:调和级数
- golang语法总结(六):常量声明与赋值
- json格式请求http
- BZOJ 1183 Croatian2008 Umnozak
- SwipeRefreshLayout完美添加及完善上拉加载功能
- java将集合保存到excel中
- 本科教育忽略的黄金C++<3> 泛型算法
- 【算法总结】Binary search 二分法