题目链接:http://poj.org/problem?id=3414

思路:bfs简单应用,增对瓶A或者瓶B进行分析就可以了,一共6种状态。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; struct Node{
int a,b,step;
char str[][];
}; int A,B,C;
bool mark[][];
bool bfs()
{
memset(mark,false,sizeof(mark));
queue<Node>que;
Node p,q;
p.a=,p.b=,p.step=;
que.push(p);
mark[][]=true;
while(!que.empty()){
p=que.front();
que.pop();
if(p.a==C||p.b==C){
printf("%d\n",p.step);
for(int i=;i<=p.step;i++){
printf("%s\n",p.str[i]);
}
return true;
}
if(p.a==){
q=p;
q.a=A;
q.step++;
strcpy(q.str[q.step],"FILL(1)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}else if(p.a<=A){
q=p;
q.a=;
q.step++;
strcpy(q.str[q.step],"DROP(1)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
if(p.b<B){
q=p;
if(q.a+q.b<=B){
q.b+=q.a;
q.a=;
}else {
q.a=(q.b+q.a)-B;
q.b=B;
}
q.step++;
strcpy(q.str[q.step],"POUR(1,2)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}
}
if(p.b==){
q=p;
q.b=B;
q.step++;
strcpy(q.str[q.step],"FILL(2)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}else if(p.b<=B){
q=p;
q.b=;
q.step++;
strcpy(q.str[q.step],"DROP(2)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
if(p.a<A){
q=p;
if(q.b+q.a<=A){
q.a+=q.b;
q.b=;
}else {
q.b=(q.b+q.a)-A;
q.a=A;
}
q.step++;
strcpy(q.str[q.step],"POUR(2,1)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}
}
}
return false;
} int main()
{
while(~scanf("%d%d%d",&A,&B,&C)){
if(!bfs())puts("impossible");
}
return ;
}

最新文章

  1. 移动端事件对象touches的误区
  2. 【DP】HIHO 1078
  3. 什么是闭包(closure),为什么要用它?
  4. php生成随机密码(php自定义函数)转自先锋教程网
  5. Scrapy安装、爬虫入门教程、爬虫实例(豆瓣电影爬虫)
  6. html19-----视频,音乐的插入
  7. mysql - 查看Port
  8. button 垂直分布
  9. python package的概念
  10. eclipse提升注解提示速度
  11. LeetCode 101. Symmetric Tree (对称树)
  12. Spring MVC的handlermapping之BeanNameUrlHandlerMapping初始化
  13. tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例
  14. mac os x下的一些小技巧
  15. Spring Cloud构建微服务架构(二)服务消费者
  16. 读书笔记---HTML5实战 MARCO CASARIO(后六章)
  17. epoll+socket实现 socket并发 linux服务器
  18. CSRF 攻击(跨域攻击)
  19. C# 自定义配置文件
  20. Java之IO流总结

热门文章

  1. Android Exception 10(server)&#39; ~ Channel is unrecoverably broken and will be disposed!)
  2. linux下的QQ执行玩法:pidgin-lwqq
  3. hibernate list和iterate
  4. Drupal如何集中控制静态变量?
  5. 解决chrome和firefox flash不透明的方法
  6. stl之hash_multiset
  7. ssh理论知识
  8. Python爬去图片实例,python 爬取图片
  9. 查询清除SQL Server数据库备份还原历史记录
  10. atitit.js的&#160;字符串内容&#160;转义&#160;&#160;js处理html