本题传送门

本题知识点:宽度优先搜素 + 字符串

题意很简单,如何把用两个杯子,装够到第三个杯子的水。

操作有六种,这样就可以当作是bfs的搜索方向了

// FILL(1) 把第一个杯子装满
// FILL(2) 把第二个杯子装满
// POUR(1,2) 把第一个杯子的水倒进第二个杯子
// POUR(2,1) 把第二个杯子的水倒进第一个杯子
// DROP(1) 把第一个杯子的水都倒掉
// DROP(2) 把第二个杯子的水都倒掉

本题的难点是如何记录路径,我们可以用一个巧妙的方法去解决掉,详细请看代码

// POJ 3414
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
using namespace std; bool take[102][102], ok;
int A, B, C;
struct node{
string str;
int l, r, pla[102], cnt; // cnt 记录有多少条路径
};
queue<node> que;
string str[] = {
"FILL(1)",
"FILL(2)",
"POUR(1,2)",
"POUR(2,1)",
"DROP(1)",
"DROP(2)"
}; void show(int len, int pla[]){
printf("%d\n", len);
for(int i = 1; i <= len; i++){
cout << str[pla[i]] << endl;
}
} void bfs(){
ok = false;
take[0][0] = true;
node a;
a.str = "NONE";
a.l = a.r = a.cnt = 0;
que.push(a); while(!que.empty()){
node next, now = que.front(); que.pop(); // cout << now.str << " ";
// printf("l:%d r:%d cnt:%d\n", now.l, now.r, now.cnt);
// show(now.cnt, now.pla);
// cout << endl; if(now.l == C || now.r == C){
show(now.cnt, now.pla);
ok = true;
break;
} // FILL(1)
if(now.l < A && !take[A][now.r]){
next.str = str[0];
next.l = A;
next.r = now.r;
// 这句循环是为了保存之前的路径 下同
for(int i = 1; i <= now.cnt; i++){
next.pla[i] = now.pla[i];
}
next.pla[now.cnt + 1] = 0;
next.cnt = now.cnt + 1;
take[A][now.r] = true;
que.push(next);
} // FILL(2)
if(now.r < B && !take[now.l][B]){
next.str = str[1];
next.l = now.l;
next.r = B;
for(int i = 1; i <= now.cnt; i++){
next.pla[i] = now.pla[i];
}
next.pla[now.cnt + 1] = 1;
next.cnt = now.cnt + 1;
take[now.l][B] = true;
que.push(next);
} // POUR(1, 2)
if(0 < now.l && now.r < B){
int R = now.l + now.r >= B ? B : now.l + now.r;
int L = R - now.r >= now.l ? 0 : now.l - (R - now.r);
if(!take[L][R]){
next.str = str[2];
next.l = L;
next.r = R;
for(int i = 1; i <= now.cnt; i++){
next.pla[i] = now.pla[i];
}
next.pla[now.cnt + 1] = 2;
next.cnt = now.cnt + 1;
take[L][R] = true;
que.push(next);
}
} // POUR(2,1)
if(now.l < A && 0 < now.r){
int L = now.l + now.r >= A ? A : now.l + now.r;
int R = L - now.l >= now.r ? 0 : now.r - (L - now.l);
if(!take[L][R]){
next.str = str[3];
next.l = L;
next.r = R;
for(int i = 1; i <= now.cnt; i++){
next.pla[i] = now.pla[i];
}
next.pla[now.cnt + 1] = 3;
next.cnt = now.cnt + 1;
take[L][R] = true;
que.push(next);
}
} // DROP(1)
if(!take[0][now.r]){
next.str = str[4];
next.l = 0;
next.r = now.r;
for(int i = 1; i <= now.cnt; i++){
next.pla[i] = now.pla[i];
}
next.cnt = now.cnt + 1;
next.pla[now.cnt + 1] = 4;
take[0][now.r] = true;
que.push(next);
} // DROP(2)
if(!take[now.l][0]){
next.str = str[5];
next.l = now.l;
next.r = 0;
for(int i = 1; i <= now.cnt; i++){
next.pla[i] = now.pla[i];
}
next.cnt = now.cnt + 1;
next.pla[now.cnt + 1] = 5;
take[now.l][0] = true;
que.push(next);
} } if(!ok) printf("impossible\n");
} int main()
{
scanf("%d %d %d", &A, &B, &C); bfs(); return 0;
}

【POJ3414】Pots的更多相关文章

  1. 【BFS】Pots

    [poj3414]Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16925   Accepted: 7168   ...

  2. 【转】ACM训练计划

    [转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...

  3. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  4. 【原】谈谈对Objective-C中代理模式的误解

    [原]谈谈对Objective-C中代理模式的误解 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 这篇文章主要是对代理模式和委托模式进行了对比,个人认为Objective ...

  5. 【原】FMDB源码阅读(三)

    [原]FMDB源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 FMDB比较优秀的地方就在于对多线程的处理.所以这一篇主要是研究FMDB的多线程处理的实现.而 ...

  6. 【原】Android热更新开源项目Tinker源码解析系列之一:Dex热更新

    [原]Android热更新开源项目Tinker源码解析系列之一:Dex热更新 Tinker是微信的第一个开源项目,主要用于安卓应用bug的热修复和功能的迭代. Tinker github地址:http ...

  7. 【调侃】IOC前世今生

    前些天,参与了公司内部小组的一次技术交流,主要是针对<IOC与AOP>,本着学而时习之的态度及积极分享的精神,我就结合一个小故事来初浅地剖析一下我眼中的“IOC前世今生”,以方便初学者能更 ...

  8. Python高手之路【三】python基础之函数

    基本数据类型补充: set 是一个无序且不重复的元素集合 class set(object): """ set() -> new empty set object ...

  9. Python高手之路【一】初识python

    Python简介 1:Python的创始人 Python (英国发音:/ˈpaɪθən/ 美国发音:/ˈpaɪθɑːn/), 是一种解释型.面向对象.动态数据类型的高级程序设计语言,由荷兰人Guido ...

随机推荐

  1. jquery 获取后台实时数据

    第一步.提醒后台处理数据1.$.ajax({}) 提交数据,2.后台返回状态3.后台开始处理数据,并每秒记录状态到 data.json 文件4.前台每秒请求 data.json 文件,直到处理完成 第 ...

  2. Spring Security 解析(六) —— 基于JWT的单点登陆(SSO)开发及原理解析

    Spring Security 解析(六) -- 基于JWT的单点登陆(SSO)开发及原理解析   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把 ...

  3. HTTP发展简史

    HTTP发展简史 HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文本到本地浏览器的 ...

  4. excel 宏循环行数据 ,Excel统计所有sheet数据行数 VBA

    Sub fun1() '统计每一个sheet有多少行数据 Set s1 = Sheets("Sheet1") 'totalok = 0 To Sheets.Count s1.Cel ...

  5. JavaScript 之 基本包装类型

    基本包装类型 为了方便操作基本数据类型,JavaScript 还提供了三个特殊的引用类型:String/Number/Boolean.  下面先看一段代码: var s1 = "Hello ...

  6. FreePascal - CodeTyphon 如何调整代码编辑器背景色?

    当前版本的CodeTyphon默认背景色是黑色,看起来很不习惯,通过下面操作,修改了它的代码编辑器的背景色: 1,打开CodeTyphon的菜单“工具”->“选项”. 2,选择左侧列表项目“颜色 ...

  7. python递归函数的执行过程

    举例: def nove(n,a,b,c): if n == 1: print(a,'------------>',c) else: nove(n-1,a,c,b) nove(1,a,b,c) ...

  8. Linux 环境变量配置(Nodejs/MongoDB/JDK/Nginx)

    一.环境变量配置 注:配置环境变量的文件 全局变量(系统级别): /etc/bashrc /etc/profile /etc/environment 用户变量(用户级别): ~/.bash_profi ...

  9. 如何提交多个具有相同name属性的表单

    有的时候我们会遇到这样一个需求,一个用户页面中有多条履历信息,每条履历信息对应数据表中的一条记录,用户可以进行添加或修改,点击保存时同时提交到了后台.有两个难点:1.前端怎样一次性提交多条履历信息?2 ...

  10. Could not get lock /var/lib/dpkg/lock-frontend解决

    在安装软件包时如果出现Could not get lock /var/lib/dpkg/lock-frontend,说明之前使用apt时出现异常,没有正常关闭,还在运行. lgj@lgj-Lenovo ...