博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1495 bfs搜索、模拟
阅读量:4563 次
发布时间:2019-06-08

本文共 2779 字,大约阅读时间需要 9 分钟。

大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。

Input三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。Output如果能平分的话请输出最少要倒的次数,否则输出"NO"。Sample Input

7 4 34 1 30 0 0

Sample Output

NO3
#include 
#include
#include
#include
#include
#include
using namespace std;typedef long long LL;#define Mem0(x) memset(x, 0, sizeof(x))#define MemI(x) memset(x, -1, sizeof(x))#define MemM(x) memset(x, 0x3f, sizeof(x))const int MAXN = 10005;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;//vis -> all of case of n and mint vis[150][150], s, n, m;struct Node{ int s, n, m, cnt;};queue
q;int bfs(){ while(!q.empty()) q.pop(); Mem0(vis); Node now, next; now.s = s, now.n = 0, now.m = 0, now.cnt = 0; q.push(now); vis[now.n][now.m] = 1; while(!q.empty()) { now = q.front(); q.pop();// cout << now.s << " " << now.n << " " << now.m << " " << now.cnt << endl; int cnt = 0; if(2 * now.s == s) cnt++; if(2 * now.n == s) cnt++; if(2 * now.m == s) cnt++; if(cnt == 2) return now.cnt; // s -> n next.cnt = now.cnt + 1; if(now.s && now.n != n) { int d = n - now.n; next.s = max(0, now.s - d); next.n = min(n, now.n + now.s); next.m = now.m; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } // s -> m if(now.s && now.m != m) { int d = m - now.m; next.s = max(0, now.s - d); next.m = min(m, now.m + now.s); next.n = now.n; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } // n -> s if(now.n && now.s != s) { int d = s - now.s; next.n = max(0, now.n - d); next.s = min(s, now.s + now.n); next.m = now.m; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } // n -> m if(now.n && now.m != m) { int d = m - now.m; next.n = max(0, now.n - d); next.m = min(m, now.m + now.n); next.s = now.s; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } //m -> s if(now.m && now.s != s) { int d = s - now.s; next.m = max(0, now.m - d); next.s = min(s, now.s + now.m); next.n = now.n; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } // m -> n if(now.m && now.n != n) { int d = n - now.n; next.m = max(0, now.m - d); next.n = min(n, now.n + now.m); next.s = now.s; if(!vis[next.n][next.m]) { vis[next.n][next.m] = 1; q.push(next); } } } return 0;}int main(){ while(cin >> s >> n >> m) { if(!s && !n && !m) break; if(s % 2) { cout << "NO" << endl; continue; } else { int ans = bfs(); if(ans) cout << ans << endl; else cout << "NO" << endl; } } return 0;}

 

 

转载于:https://www.cnblogs.com/shuizhidao/p/9692467.html

你可能感兴趣的文章
Linux Centos7 解决数据库5.7版本乱码问题
查看>>
16个Linux服务器监控命令
查看>>
在windows上安装nginx并注册
查看>>
Item 17: Consider using lazy evaluation.(More Effective C++)
查看>>
深入理解Ajax原理
查看>>
Codeforces 523B - Mean Requests 英语阅读题
查看>>
Oracle B-Tree Index 原理
查看>>
Oracle Buffer Cache 原理
查看>>
asp.net错误处理封装
查看>>
Android - HelloWorld的Layout内容
查看>>
HDU 1143 Tri Tiling(递归)
查看>>
Vhost Architecture
查看>>
RTP协议分析
查看>>
怎么洗掉衣服上的水粉颜料、丙烯颜料、水彩颜料、油画颜料
查看>>
linux常用命令总结
查看>>
数值计算中的上溢和下溢
查看>>
Jenkins+SVN+Maven+shell 自动化部署实践
查看>>
看见一个程序员敲键盘的速度不快
查看>>
如何轻松培养孩子流利说英语
查看>>
Matlab 重命名
查看>>