题意:有两个固定大小的容器,通过对容器内水的操作,使得至少一个容器内的液体量达到规定的量,求出操作数最少的操作步骤

我写的比较繁琐,将各种操作封装在了结构体里,然后BFS

代码:

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#define ll long long
#define mod 10000000007
#define mem(a) memset(a,0,sizeof(a))using namespace std;typedef pair <int,int> pii;
const int maxn = 10000+5 , inf = 0x3f3f3f3f;
int C;
int a[3];
bool vis[maxn][maxn];
struct Node{int i,j,id,f,flag;int A,B;Node(int ii=0,int jj=0,int ff=0,int idd=0,int flagg=0,int AA=0,int BB=0):i(ii),j(jj),f(ff),id(idd),flag(flagg),A(AA),B(BB){};void FILL(){if(i==1) A=a[1];else B=a[2];}void DROP(){if(i==1) A=0;else B=0;}void POUR(){if(i==1&&j==2){if(A>a[2]-B){A-=(a[2]-B);B=a[2];}else{B+=A;A=0;}}if(i==2&&j==1){if(B>a[1]-A){B-=(a[1]-A);A=a[1];}else{A+=B;B=0;}}}void print(){if(j==0){if(flag==1){if(i==1) cout<<"FILL(1)"<<endl;if(i==2) cout<<"FILL(2)"<<endl;}if(flag==3){if(i==1) cout<<"DROP(1)"<<endl;if(i==2) cout<<"DROP(2)"<<endl;}}else{if(i==1&&j==2) cout<<"POUR(1,2)"<<endl;if(i==2&&j==1) cout<<"POUR(2,1)"<<endl;}}
}oper[maxn];bool judge(Node now){if(now.A==C||now.B==C) return true;return false;
}
vector<Node>path;
int find_path(Node u){while(u.f!=u.id){path.push_back(u);u = oper[u.f];}path.push_back(u);cout<<path.size()<<endl;for(int i=path.size()-1;i>=0;i--)path[i].print();
}void bfs(){vis[0][0] = true;Node tmp1(1,0,0,0,1,0,0);oper[0]=tmp1;Node tmp2(2,0,1,1,1,0,0);oper[1]=tmp2;Node tmp3(1,2,2,2,2,0,0);oper[2]=tmp3;Node tmp4(2,1,3,3,2,0,0);oper[3]=tmp4;Node tmp5(1,0,4,4,3,0,0);oper[4]=tmp5;Node tmp6(2,0,5,5,3,0,0);oper[5]=tmp6;queue<Node>q;q.push(tmp1);q.push(tmp2);q.push(tmp3);q.push(tmp4);q.push(tmp5);q.push(tmp6);int cnt = 6;while(!q.empty()){Node now = q.front();q.pop();if(now.j==0){if(now.flag==1) now.FILL();if(now.flag==3) now.DROP();}else now.POUR();if(judge(now)){find_path(now);return ;}if(!vis[now.A][now.B]){vis[now.A][now.B] = true;Node tmp1(1,0,now.id,cnt,1,now.A,now.B);oper[cnt++]=tmp1;q.push(tmp1);Node tmp2(2,0,now.id,cnt,1,now.A,now.B);oper[cnt++]=tmp2;q.push(tmp2);Node tmp3(1,2,now.id,cnt,2,now.A,now.B);oper[cnt++]=tmp3;q.push(tmp3);Node tmp4(2,1,now.id,cnt,2,now.A,now.B);oper[cnt++]=tmp4;q.push(tmp4);Node tmp5(1,0,now.id,cnt,3,now.A,now.B);oper[cnt++]=tmp5;q.push(tmp5);Node tmp6(2,0,now.id,cnt,3,now.A,now.B);oper[cnt++]=tmp6;q.push(tmp6);}}cout<<"impossible"<<endl;
}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);scanf("%d%d%d",&a[1],&a[2],&C);bfs();
}

转载于:https://www.cnblogs.com/seven7777777/p/10278709.html

OpenJ_Bailian 3151 Pots (BFS)相关推荐

  1. POJ3414 Pots —— BFS + 模拟

    题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  2. Pots --bfs

    You are given two pots, having the volume of A and B liters respectively. The following operations c ...

  3. POJ-3414 Pots BFS+记忆路径

    这题让我想起了杭电的那一道三个可乐罐的题目,那一题好像只要输出次数就可以了,而这里则需要将所有的路径全部输出出来,这样的话,单纯的BFS则会在状态的保留上会出现大量的空间冗余.于是这题在考虑到搜索空间 ...

  4. Pots (BFS ➕ 输出路径)

    题目链接:http://poj.org/problem?id=3414 思路: 因为有六种操作,所以六种操作中合法的都加入队列中BFS 如何去输出路径呢? 我们不妨设一个string数组,它的索引就和 ...

  5. poj 3414 Pots BFS

    参考文章:思路公式 代码思路 原来的代码有点瑕疵,这是最新的AC代码 #pragma warning(disable:4996) #include<iostream> #include&l ...

  6. kuangbin 专题一 简单搜索

    kuangbin 专题一 简单搜索 1.POJ1321棋盘问题[DFS] 代码 自己的想法 2.POJ2251Dungeon Master[三维空间BFS] 代码 自己的想法 3.POJ3278 Ca ...

  7. 搜索专题(不定期更新)

    1.POJ 2386  Lake Counting 题意:给出一块区域,询问有多少个湖泊? 思路:DFS,对于'W',深搜一次,并标记已访问.之后每次对未访问的'W'做一次深搜. 1 #include ...

  8. Pots(poj-3414)bfs+输出路径

    题目描述 You are given two pots, having the volume of A and B liters respectively. The following operati ...

  9. Pots POJ - 3414(bfs)

    You are given two pots, having the volume of A and B liters respectively. The following operations c ...

最新文章

  1. 用eclips连hadoop报Unknown protocol to job tracker: org.apache.hadoop.hdfs.protocol.ClientProtoco
  2. 玩了一把WOW,贴几张图上来,当然只是私服哈!
  3. 61-1 认识webpack
  4. 【Mac】sublime text3标题栏显示文件完整路径
  5. java 打包后 文件资源文件 jar,JAVA打包成JAR无法找到资源文件
  6. 工作随想---关于业务流程,落实责任制
  7. PyQt5 Pyinstaller时出现错误Cannot find PyQt5 plugin directories
  8. freemarker 模板填充乱码
  9. java shell排序_八大排序算法——希尔(shell)排序
  10. 2.5 HDFS体系架构
  11. WCF NetTcpBinding Transport安全模式(7) ClientCredentialType证书验证模式---- ChainTrust验证模式...
  12. bzoj4008: [HNOI2015]亚瑟王
  13. 《软件调试》第二版正式发售,看雪给你早鸟价
  14. ubuntu 中比较好用的一些工具
  15. impala 时间格式转换
  16. 在酒店怎么让你电脑的网速比别人快?
  17. hashmap中的key是有序的么_Java中的HashMap,为什么输出竟然是有序的
  18. ubuntu 安装 nvidia 显卡驱动
  19. 只需64MB优盘+90分钟nbsp;教你如何全面…
  20. SpringBoot自动跳转首页

热门文章

  1. wh计算公式_“笔记本”电池里的mAh和Wh有啥区别
  2. android自定义view设置高度,自定义View的宽高设定
  3. linux超过cpu负载重启脚本,linux下Web服务器CPU负载超过阀值自动重启脚本
  4. Courier:Dropbox 基于gRPC 的 RPC 框架开发过程
  5. 啤酒与尿布,咩叔原创基于图论简单到爆的实时关联性算法
  6. git基础管理--操作远程仓库
  7. Spring Java-based容器配置
  8. TheWorld不能访问Taobao的解决办法
  9. 十分钟完成的操作系统
  10. 差分进化算法原理及matlab代码实现