博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
USACO Mother's Milk(bfs)
阅读量:2231 次
发布时间:2019-05-09

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

题解:
水杯倒水的问题非常经典,套路也是一样的,bfs找出全部状态。

这道题的关键在于每次都应该进行六次的倒水尝试,细心一点。PS:三维数组表示状态真的非常方便。

代码实现:

/*ID: eashionLANG: C++TASK: milk3*/#include 
#include
#include
#include
#include
#include
#define MAX 22using namespace std;struct state{ int a,b,c;};int A,B,C;int save[MAX];queue
Q;int show[MAX][MAX][MAX];int main(){ freopen("milk3.in","r",stdin); freopen("milk3.out","w",stdout); while( scanf("%d%d%d",&A,&B,&C) != EOF ){ int pos = 0; memset(save,0,sizeof(save)); memset(show,0,sizeof(show)); show[0][0][C] = 1; state tmp; tmp.a = 0; tmp.b = 0; tmp.c = C; Q.push(tmp); while( !Q.empty() ){ state cur; cur = Q.front(); Q.pop(); if( cur.a == 0 ){ save[pos] = cur.c; pos++; } state nstate; int ta,tb,tc; if( cur.a != 0 ){ ta = cur.a; tb = cur.b; tc = cur.c; if( ta+tb <= B ){ tb += ta; ta = 0; tc = cur.c; } else{ ta = ta+tb-B; tb = B; tc = cur.c; } if( show[ta][tb][tc] != 1 ){ nstate.a = ta; nstate.b = tb; nstate.c = tc; Q.push(nstate); show[ta][tb][tc] = 1; } ta = cur.a; tb = cur.b; tc = cur.c; if( ta+tc <= C ){ tc += ta; ta = 0; tb = cur.b; } else{ ta = ta+tc-C; tc = C; tb = cur.b; } if( show[ta][tb][tc] != 1 ){ nstate.a = ta; nstate.b = tb; nstate.c = tc; Q.push(nstate); show[ta][tb][tc] = 1; } } if( cur.b != 0 ){ ta = cur.a; tb = cur.b; tc = cur.c; if( ta+tb <= A ){ ta += tb; tb = 0; tc = cur.c; } else{ tb = ta+tb-A; ta = A; tc = cur.c; } if( show[ta][tb][tc] != 1 ){ nstate.a = ta; nstate.b = tb; nstate.c = tc; Q.push(nstate); show[ta][tb][tc] = 1; } ta = cur.a; tb = cur.b; tc = cur.c; if( tb+tc <= C ){ tc += tb; tb = 0; ta = cur.a; } else{ tb = tb+tc-C; tc = C; ta = cur.a; } if( show[ta][tb][tc] != 1 ){ nstate.a = ta; nstate.b = tb; nstate.c = tc; Q.push(nstate); show[ta][tb][tc] = 1; } } if( cur.c != 0 ){ ta = cur.a; tb = cur.b; tc = cur.c; if( tc+tb <= B ){ tb += tc; tc = 0; ta = cur.a; } else{ tc = tc+tb-B; tb = B; ta = cur.a; } if( show[ta][tb][tc] != 1 ){ nstate.a = ta; nstate.b = tb; nstate.c = tc; Q.push(nstate); show[ta][tb][tc] = 1; } ta = cur.a; tb = cur.b; tc = cur.c; if( ta+tc <= A ){ ta += tc; tc = 0; tb = cur.b; } else{ tc = ta+tc-A; ta = A; tb = cur.b; } if( show[ta][tb][tc] != 1 ){ nstate.a = ta; nstate.b = tb; nstate.c = tc; Q.push(nstate); show[ta][tb][tc] = 1; } } } sort(save,save+pos); for( int i = 0; i < pos; i++ ){ if( i != pos-1 ){ printf("%d ",save[i]); } else{ printf("%d\n",save[i]); } } } return 0;}

转载于:https://www.cnblogs.com/ljbguanli/p/7304902.html

你可能感兴趣的文章
【操作系统】大小端问题
查看>>
Git上传代码时碰到的问题及解决方法
查看>>
【Linux】vim的简单配置
查看>>
【C++】智能指针
查看>>
【C++】const修饰的成员函数
查看>>
【C++】面向对象的三大特性
查看>>
【C++】智能指针(后续)
查看>>
【C】堆区和栈区的区别
查看>>
【linux】send和recv函数解析
查看>>
【Linux】线程安全的单例模式以及计算密集型线程和IO密集型线程
查看>>
一次完整的HTTP请求是怎样的??
查看>>
【C++】常见的内存泄漏及解决方法
查看>>
【C++】const 指针与指向const的指针
查看>>
【Linux】多线程和多进程 及其应用场景
查看>>
【C++】构造函数中必须通过初始化列表来进行初始化情况
查看>>
【算法】对于大数的操作
查看>>
【操作系统】系统调用的概念
查看>>
【计算机网络】cookie和session的区别
查看>>
【C++】构造函数、析构函数抛出异常的问题
查看>>
【C++】关于vector<bool>
查看>>