【BFS】CODE[VS] 1226 倒水问题 (BFS+模拟)
来源:互联网 发布:优雅的女生知乎 编辑:程序博客网 时间:2024/06/03 09:27
点击被抓去倒水
这道题主要考察代码能力
模拟倒水的过程,结果因为码力太弱,漏了两种情况(x和y全倒入一个(x或y)不满)
然后就没有然后了
最近超喜欢压代码怎么破???
代码如下:
#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#include <queue>const int maxn = 333;using namespace std;int x,y,z;bool vis[maxn][maxn];struct state{ int x,y,step;};state f;queue<state>q;int bfs(){ q.push(f); vis[f.x][f.y] = 1; while(q.size()) { f = q.front(); q.pop(); if(f.x == z|| f.y == z) return f.step;//最近超喜欢压代码! if(f.x < x && vis[x][f.y] == 0) { q.push((state){x,f.y,f.step+1}); vis[x][f.y] = 1;}//倒满x if(f.y < y && vis[f.x][y] == 0) { q.push((state){f.x,y,f.step+1}); vis[f.x][y] = 1;}//倒满y if(f.x && vis[0][f.y] == 0) { q.push((state){0,f.y,f.step+1}); vis[0][f.y] = 1;}//倒空x if(f.y && vis[f.x][0] == 0) { q.push((state){f.x,0,f.step+1}); vis[f.x][0] = 1;}//倒空y if(f.x >= y-f.y && vis[f.x-(y-f.y)][y] == 0){ q.push((state) {f.x-(y-f.y),y,f.step+1}); vis[f.x-(y-f.y)][y] = 1;}//从x倒入y中填满y if(f.y >= x-f.x && vis[x][f.y-(x-f.x)] == 0){ q.push((state) {x,f.y-(x-f.x),f.step+1}); vis[x][f.y-(x-f.x)] = 1;}//从y倒入x中填满x if(f.x < y-f.y && vis[0][f.x+f.y] == 0) { q.push((state){0,f.x+f.y,f.step+1}); vis[0][f.x+f.y] = 1;}//全部x加入y if(f.y < x-f.x && vis[f.x+f.y][0] == 0) { q.push((state){f.x+f.y,0,f.step+1}); vis[f.x+f.y][0] = 1;}//全部y加入x } return -1; }int main(){ scanf("%d%d%d",&x,&y,&z); int ans = bfs(); if(ans != -1) { printf("%d\n",ans); return 0; } printf("impossible\n");return 0;}
THE END
By Peacefuldoge
http://blog.csdn.net/loi_peacefuldog
0 0
- 【BFS】CODE[VS] 1226 倒水问题 (BFS+模拟)
- POJ 3414 Pots【bfs模拟倒水问题】
- POJ 3414 Pots【bfs模拟倒水问题】
- POJ3414 模拟倒水问题 BFS记录路径
- POJ 3414-Pots(BFS-模拟倒水)
- 水杯倒水问题 bfs
- 20130421.4 倒水问题(BFS)
- UVA10603 倒水问题 BFS
- codevs1226 倒水问题 bfs
- pots 倒水问题(bfs)
- (转载)BFS 倒水问题 HDU 1495倒水问题
- uva 10603 Fill(倒水问题 BFS)
- Fill (Uva 10603 bfs 倒水问题)
- 7.5.2倒水问题(BFS)
- poj 3414 倒水问题 bfs
- 【codevs1226】倒水问题,BFS练习
- hdu1495 bfs解决倒水问题
- 算法学习-倒水问题(bfs)
- Java 中BufferedReader 和 InputStreamReader
- java多态 深入浅出
- tablayout:的配置
- ntp server
- 洛谷 P2327 [SCOI2005] 扫雷
- 【BFS】CODE[VS] 1226 倒水问题 (BFS+模拟)
- mysql全局唯一ID生成方案(二)
- WebService里的wsdl的详解以及使用注解修改wsdl的配置
- 玩儿转MVC之大屌牌儿MVC框架(一)
- onAttach(Context) 在Fragment中没有调用
- 获取系统相关信息
- Dubbo之旅--集群容错和负载均衡
- Git常用命令
- 转:DLL导出变量,函数,类