这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome,创客晚上好吵呀,隔壁真的服...

  本题大意:给定两个杯子的容量,有六种操作,通过操作使得两个被子其中一个杯子的水等于待输入值C,并输出最少操作次数和操作名称。

  本题思路:BFS,遇到合适的直接输出,搜完都没有搜到就impossible。

  参考代码:

  将switch改为if能节省大部分篇幅,但是switch更美观明了一点。

  1. #include <string>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <queue>
  5. #include <stack>
  6. using namespace std;
  7.  
  8. typedef pair<int ,int > P;
  9. const int maxn = + , INF = 0x3f3f3f3f;
  10. int A, B, C, t;
  11.  
  12. struct {
  13. int x, y, cnt;//用来存他爹的坐标和倒水的方式
  14. } Ans[maxn][maxn];
  15. int vis[maxn][maxn];
  16. string ans[] = {
  17. "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)",
  18. };
  19.  
  20. void bfs() {
  21. queue <P> Q;
  22. vis[][] = ;
  23. Ans[][].x = -, Ans[][].y = -;
  24. Q.push(make_pair(, ));
  25. while(!Q.empty()) {
  26. P now = Q.front(), Next;
  27. stack <string> s1;
  28. Q.pop();
  29. if(now.first == C || now.second == C) {
  30. cout << vis[now.first][now.second] << endl;
  31. int X = now.first;
  32. int Y = now.second;
  33. while(X != -) {
  34. s1.push(ans[Ans[X][Y].cnt]);
  35. int tmp = X;
  36. X = Ans[X][Y].x;
  37. Y = Ans[tmp][Y].y;
  38. }
  39. s1.pop();
  40. while(!s1.empty()) {
  41. cout << s1.top() << endl;
  42. s1.pop();
  43. }
  44. return;
  45. }
  46. for(int i = ; i < ; i ++) {
  47. switch(i) {
  48. case ://将A加满
  49. Next.first = A;
  50. Next.second = now.second;
  51. break;
  52. case ://将B加满
  53. Next.second = B;
  54. Next.first = now.first;
  55. break;
  56. case ://将A倒掉
  57. Next.second = now.second;
  58. Next.first = ;
  59. break;
  60. case ://将B倒掉
  61. Next.first = now.first;
  62. Next.second = ;
  63. break;
  64. case ://将A倒入B中
  65. t = B - now.second;//B中还能倒入的量
  66. Next.first = now.first - t;
  67. if(Next.first < ) Next.first = ;
  68. Next.second = now.first + now.second;
  69. if(Next.second > B) Next.second = B;
  70. break;
  71. case ://将B倒入A中
  72. t = A - now.first;//A中还能倒入的量
  73. Next.second = now.second - t;
  74. if(Next.second < ) Next.second = ;
  75. Next.first = now.first + now.second;
  76. if(Next.first > A) Next.first = A;
  77. break;
  78. }
  79. if(vis[Next.first][Next.second] == INF) {
  80. vis[Next.first][Next.second] = vis[now.first][now.second] + ;
  81. Q.push(Next);
  82. Ans[Next.first][Next.second].x = now.first;
  83. Ans[Next.first][Next.second].y = now.second;
  84. Ans[Next.first][Next.second].cnt = i;
  85. }
  86. }
  87. }
  88. cout << "impossible" << endl;
  89. }
  90.  
  91. int main () {
  92. memset(vis, INF, sizeof(vis));
  93. cin >> A >> B >> C;
  94. bfs();
  95. return ;
  96. }

POJ-3414.Pots.(BFS + 路径打印)的更多相关文章

  1. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

  2. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

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

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

  4. POJ 3414 Pots 记录路径的广搜

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

  5. POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

  6. POJ - 3414 Pots BFS(著名倒水问题升级版)

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

  7. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

  8. poj 3414 Pots bfs+模拟

    #include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...

  9. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

随机推荐

  1. 完整验证码类(validityHelper)(代码+使用)

    using System; using System.Web; using System.Drawing; using System.Security.Cryptography; namespace ...

  2. 没有cv2.so文件

    最近发现opencv安装的有问题,发现少了cv2.so文件,这个文件是给python的调度包. 查来查去,发现cmake的时候有这个: -- Found PythonInterp: /usr/bin/ ...

  3. Duboo 与springboot整合

    https://github.com/apache/incubator-dubbo-spring-boot-project 当采用properties方式时,可以用下方的注解 1.pom <de ...

  4. VC中明明已经添加了头文件却还提示未定义的问题

    我在VS中编译程序遇到这个错误:error C3861: 'ReadDirectoryChangesW': identifier not found, even with argument-depen ...

  5. 机器学习进阶-光流估计 1.cv2.goodFeaturesToTrack(找出光流估计所需要的角点) 2.cv2.calcOpticalFlowPyrLK(获得光流检测后的角点位置) 3.cv2.add(进行像素点的加和)

    1.cv2.goodFeaturesToTrack(old_gray, mask=None, **feature_params)  用于获得光流估计所需要的角点参数说明:old_gray表示输入图片, ...

  6. jsfl完成通知air

    jsfl完成后生成一个文本A.txt, air开始jsfl执行后一直检测A.txt是否存在,存在就是完成了.那么就可以删除这个A.txt

  7. WDA-4-ALV按钮&ICON

    1.ICON图标 AccessControlledArea (14x14) Activate (14x14) Active (14x14) AdaptationTechnical (14x14) Ad ...

  8. HTTP 错误 403.6 - Forbidden 解决方案

    MSDN 的解决方案 原因 1 Ip 安全的 XML 元素的allowUnlisted属性的值为 false.此外,客户端计算机的 IP 地址不在ip 安全XML 元素之下 IP 地址的列表中.IIS ...

  9. homebrew, carthage以及redis的安装和启动

    homebrew的介绍以及redis的安装   brew install redis https://www.cnblogs.com/xd502djj/p/6923690.html redis的启动, ...

  10. VS2017断点调试UNITY2018.3 经常卡住的问题

    发现了VS下断点经常导致unity卡住的原因,是vs占用CPU太高导致的,这其中又是vs service hub 造成的,这个除了在代码中提示各函数引用之外好像没什么用,我定位到它的安装目录,删除了配 ...