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

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

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

  参考代码:

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

 #include <string>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
using namespace std; typedef pair<int ,int > P;
const int maxn = + , INF = 0x3f3f3f3f;
int A, B, C, t; struct {
int x, y, cnt;//用来存他爹的坐标和倒水的方式
} Ans[maxn][maxn];
int vis[maxn][maxn];
string ans[] = {
"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)",
}; void bfs() {
queue <P> Q;
vis[][] = ;
Ans[][].x = -, Ans[][].y = -;
Q.push(make_pair(, ));
while(!Q.empty()) {
P now = Q.front(), Next;
stack <string> s1;
Q.pop();
if(now.first == C || now.second == C) {
cout << vis[now.first][now.second] << endl;
int X = now.first;
int Y = now.second;
while(X != -) {
s1.push(ans[Ans[X][Y].cnt]);
int tmp = X;
X = Ans[X][Y].x;
Y = Ans[tmp][Y].y;
}
s1.pop();
while(!s1.empty()) {
cout << s1.top() << endl;
s1.pop();
}
return;
}
for(int i = ; i < ; i ++) {
switch(i) {
case ://将A加满
Next.first = A;
Next.second = now.second;
break;
case ://将B加满
Next.second = B;
Next.first = now.first;
break;
case ://将A倒掉
Next.second = now.second;
Next.first = ;
break;
case ://将B倒掉
Next.first = now.first;
Next.second = ;
break;
case ://将A倒入B中
t = B - now.second;//B中还能倒入的量
Next.first = now.first - t;
if(Next.first < ) Next.first = ;
Next.second = now.first + now.second;
if(Next.second > B) Next.second = B;
break;
case ://将B倒入A中
t = A - now.first;//A中还能倒入的量
Next.second = now.second - t;
if(Next.second < ) Next.second = ;
Next.first = now.first + now.second;
if(Next.first > A) Next.first = A;
break;
}
if(vis[Next.first][Next.second] == INF) {
vis[Next.first][Next.second] = vis[now.first][now.second] + ;
Q.push(Next);
Ans[Next.first][Next.second].x = now.first;
Ans[Next.first][Next.second].y = now.second;
Ans[Next.first][Next.second].cnt = i;
}
}
}
cout << "impossible" << endl;
} int main () {
memset(vis, INF, sizeof(vis));
cin >> A >> B >> C;
bfs();
return ;
}

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. 06.linux文件目录操作命令

    文件目录操作命令: ›ls 显示文件和目录列表 -l 列出文件的详细信息 -a 列出当前目录所有文件,包含隐藏文件 ›mkdir 创建目录 -p 父目录不存在情况下先生成父目录 ›cd 切换目录 ›t ...

  2. leetcode993

    public class Node { public int CurNode; public int FatherNode; public int Layer; } public class Solu ...

  3. hive基础操作

    hive --version  查看hive的版本 hive -S -e "set" | grep auto  ##在shell下可以查找属性的状态.小技巧.

  4. linux下通过sed命令直接修改文件内容

    sed是实现对流的编辑.通常,我们使用sed可以实现内容的编辑后然后保存成另外的一个文件,如果正确的话,才写入到源文件.但是某些时候,我们需要直接修改文件,因为,保存文件到一个文件,然后再覆盖原文件的 ...

  5. js 编写一个神奇的四则运算

    写一个算法,有时候可以用简单的方法就可以写出来,但是只能针对特定的环境,如果要能够适应不同的环境,就需要对算法进行优化,在优化的过程中,你会觉得非常神奇,下面来看一个简单的四则运算的算法编写方式: 1 ...

  6. Linux:使用互斥量进行线程同步

    基础知识 同步概念 所谓同步,即同时起步,协调一致.不同的对象,对"同步"的理解方式略有不同.如,设备同步,是指在两个设备之间规定一个共同的时间参考:数据库同步,是指让两个或多个数 ...

  7. NetStream 记录

    bufferLength : Number [只读] 数据当前存在于缓冲区中的秒数.(已进入缓冲区的秒数) bufferTime : Number 指定在开始显示流之前需要多长时间将消息存入缓冲区.( ...

  8. unzip解压带密码的压缩包

    // 解压 String pw = "123456"; String cmd = "unzip -P " + pw + " /root/lianlia ...

  9. k8s删除一个Node并重新加入集群

    k8s删除一个节点使用以下命令 删除一个节点前,先驱赶掉上面的pod kubectl drain 172.17.3.51 --delete-local-data 然后我们来删除节点 kubectl d ...

  10. 使用navigator对象,输出当前浏览器的信息

    <script type="text/javascript"> with(document) { write("你的浏览器信息:<ol>" ...