[BFS] HDU 1495
来源:互联网 发布:淘宝法律咨询 编辑:程序博客网 时间:2024/06/11 02:32
和上面那个倒来倒去的差不多啦hhh
只不过这次是三个杯子互相倒,有三个记录的点
#include <cstdlib>#include <cstring>#include <iostream>#include <queue>#define N 1000using namespace std;struct Node { int a, b, s; int cnt;} cur, nex;int A, B, S;bool vis[ N ][ N ];//六种操作,要传指针void operate ( Node *c, Node *n, int i ) { switch ( i ) { // Pour b to a case 0: { n->a = min ( A, c->a + c->b ); n->b = c->b - ( n->a - c->a ); break; } // Pour a to b case 1: { n->b = min ( B, c->b + c->a ); n->a = c->a - ( n->b - c->b ); break; } // Pour s to a case 2: { n->a = min ( A, c->a + c->s ); n->s = c->s - ( n->a - c->a ); break; } // Pour s to b case 3: { n->b = min ( B, c->b + c->s ); n->s = c->s - ( n->b - c->b ); break; } // Pour a to s case 4: { n->s = min ( S, c->s + c->a ); n->a = c->a - ( n->s - c->s ); break; } // Pour b to s case 5: { n->s = min ( S, c->s + c->b ); n->b = c->b - ( n->s - c->s ); break; } }}int bfs () { memset ( vis, false, sizeof ( vis ) ); cur.a = 0, cur.b = 0, cur.s = S; cur.cnt = 0; vis[ cur.a ][ cur.b ] = true; queue<Node> q; q.push ( cur ); while ( !q.empty () ) { cur = q.front (); q.pop (); if ( cur.s == S / 2 && ( cur.a == S / 2 || cur.b == S / 2 ) ) return cur.cnt; for ( int i = 0; i < 6; ++i ) { nex.a = cur.a, nex.b = cur.b, nex.s = cur.s; operate ( &cur, &nex, i ); nex.cnt = cur.cnt + 1; if ( !vis[ nex.a ][ nex.b ] ) { vis[ nex.a ][ nex.b ] = true; q.push ( nex ); } } } return -1;}int main () { while ( cin >> S >> A >> B ) { if ( !( S || A || B ) ) break; if ( S % 2 ) { cout << "NO" << endl; continue; } int sol = bfs (); if ( sol == -1 ) cout << "NO" << endl; else cout << sol << endl; } return 0;}
阅读全文
0 0
- HDU-1495 BFS
- hdu 1495(bfs)
- 图论 BFS HDU 1495
- 【BFS】HDU 1495
- HDU 1495 BFS+模拟
- [BFS] HDU 1495
- hdu 1495 bfs 非常可乐
- hdu 1495 非常可乐 BFS
- hdu 1495 非常可乐 bfs
- hdu 1495 非常可乐 (bfs)
- 非常可乐 hdu 1495 BFS
- hdu 1495非常可乐 bfs
- HDU 1495 非常可乐 BFS
- hdu 1495 非常可乐 (bfs)
- [bfs] HDU 1495 非常可乐
- BFS-HDU-1495-非常可乐
- hdu 1495 非常可乐(BFS)
- HDU 1495 非常可乐 (BFS)
- 2016年系统架构师考试题详解
- 《经济学的思维方式》
- 关于异常的整理
- HttpURLConnection请求响应实例
- 条件编译
- [BFS] HDU 1495
- spark submit 多个配置文件
- 基于Java数据库应用中如何根据Jtable的model值变化引起table内容自动
- node.js 搭建本地服务
- ffmpeg库解码海思G726库编码音频数据
- (转)谁是全球资管规模之冠?
- 彻底理解ThreadLocal
- 运行测试类学习Java中Time的API
- 自定义view