H - Pots POJ - 3414

题意: (两个锅互相倒水)希望能通过题目中的几种操作使一个锅中的水量为目标水量
bfs 搜索每一步的每种操作,直到所有操作用完,则impossible,否则输出答案
为了方便操作,用两个map,一个存六种操作对应的结果输出,一个当搜索时的visit函数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
const int maxn = 1e5;
int n, m, goal;
struct node {int a1, a2; // 两锅中的剩余水量 int step; // 操作次数 int op[maxn];
};map<int, string> opera;
map<pair<int, int>,int> mp;node bfs() {node s;s.a1 = 0; s.a2 = 0; s.step = 0;mp[make_pair(0,0)] = 1;queue<node> Q;Q.push(s);while(!Q.empty()) {node now = Q.front(); Q.pop();if(now.a1 == goal || now.a2 == goal) return now;//      cout << "*" << now.a1 << " " << now.a2 << " " << now.op[now.step] << " " << now.step << endl; for(int i = 1; i <= 6; i++) {node tmp= now; tmp.step = now.step + 1;if(i == 1 && now.a1 < n) {tmp.a1 = n; tmp.op[tmp.step] = 1;if(!mp[make_pair(tmp.a1, tmp.a2)]) mp[make_pair(tmp.a1, tmp.a2)] = 1, Q.push(tmp);}else if(i == 2 && now.a2 < m) {tmp.a2 = m; tmp.op[tmp.step] = 2; if(!mp[make_pair(tmp.a1, tmp.a2)]) mp[make_pair(tmp.a1, tmp.a2)] = 1, Q.push(tmp);}else if(i == 3 && now.a1 > 0) {tmp.a1 = 0; tmp.op[tmp.step] = 3; if(!mp[make_pair(tmp.a1, tmp.a2)]) mp[make_pair(tmp.a1, tmp.a2)] = 1, Q.push(tmp);}else if(i == 4 && tmp.a2 > 0) {tmp.a2 = 0; tmp.op[tmp.step] = 4; if(!mp[make_pair(tmp.a1, tmp.a2)]) mp[make_pair(tmp.a1, tmp.a2)] = 1, Q.push(tmp);}else if(i == 5 && tmp.a1 > 0 && tmp.a2 < m) {int temp2 = m - tmp.a2; tmp.op[tmp.step] = 5; if(temp2 > tmp.a1) tmp.a2 += tmp.a1, tmp.a1 = 0;else tmp.a1 -= temp2, tmp.a2 = m;if(!mp[make_pair(tmp.a1, tmp.a2)]) mp[make_pair(tmp.a1, tmp.a2)] = 1, Q.push(tmp);}else if(i == 6 && tmp.a1 < n && tmp.a2 > 0) {int temp1 = n - tmp.a1; tmp.op[tmp.step] = 6;if(temp1 > tmp.a2) tmp.a1 += tmp.a2, tmp.a2 = 0;else tmp.a2 -= temp1, tmp.a1 = n;if(!mp[make_pair(tmp.a1, tmp.a2)]) mp[make_pair(tmp.a1, tmp.a2)] = 1, Q.push(tmp);}}}return s;
}int main() {//  freopen("test.in", "r", stdin);ios::sync_with_stdio(false);opera[1] = "FILL(1)";opera[2] = "FILL(2)";opera[3] = "DROP(1)";opera[4] = "DROP(2)";opera[5] = "POUR(1,2)";opera[6] = "POUR(2,1)";while(~scanf("%d%d%d", &n, &m, &goal)) {node ans;ans = bfs();if(ans.step == 0) cout << "impossible" << endl;else {cout << ans.step << endl;for(int i = 1; i <= ans.step; i++) {cout << opera[ans.op[i]] << endl;}}}return 0;
}

H - Pots POJ - 3414(两个锅互相倒水)相关推荐

  1. Pots POJ - 3414

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

  2. Pots POJ - 3414(bfs)

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

  3. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  4. POJ 3414 Pots(深搜并打印路径)

    POJ 3414 Pots(深搜并打印路径) You are given two pots, having the volume of A and B liters respectively. The ...

  5. POJ 3414 Pots【BFS】+ Python

    原题链接: 3414 -- Pots 参考资料:POJ 3414 - Pots | 眈眈探求 POJ 3414 Pots[BFS][图搜] - it610.com 一 特别注意: 1. 每一种操作对应 ...

  6. H - Fibonacci POJ - 3070 (矩阵快速幂)

    H - Fibonacci POJ - 3070 (矩阵快速幂) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and ...

  7. H.264与MPEG4两种压缩算法

    H.264与MPEG4两种压缩算法 JVT(Joint Video Team,视频联合工作组)于2001年12月在泰国Pattaya成立.它由ITU-T和ISO两个国际标准化组织的有关视频编码的专家联 ...

  8. poj 3414 Pots(广搜BFS+路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=3414 此题和poj ...

  9. POJ 3414 Pots

    题目链接 Description You are given two pots, having the volume of A and B liters respectively. The follo ...

最新文章

  1. umijs多环境配置_umi 项目多环境打包配置
  2. 多媒体互动受青睐,演绎未来3G网络
  3. Spring Cloud 2022.0.0 M1 发布:需Java 17、兼容Spring Boot 3
  4. Caffe实践】如何利用Caffe训练ImageNet分类网络
  5. 数据库的三大范式以及五大约束
  6. 科研神器推荐之那些年你用过的工具
  7. centos7安装mysql的rpm_Centos7 安装MySQL(rpm方式)
  8. mobx中的数组需要注意的地方
  9. Oracle XE http端口8080的修改
  10. MySQL的basedir
  11. 7820X+RTX2080Ti(Ubuntu 18.04)新机配置记录
  12. 结构体全局变量_nginx源码分析—内存池结构ngx_pool_t及内存管理
  13. Fireworks免费下载
  14. dns服务器对网速有影响吗,更换DNS服务器可以提高网速吗?
  15. 【计算机视觉】神经网络推理训练
  16. 字符串处理:输入字符串s1和s2以及插入位置f,在字符串s1中的指定位置f处插入字符串s2。如输入BEIJING, 123, 3,则输出:BEI123JING。
  17. [CC2642R1][VSCODE+Embedded IDE+Cortex-Debug] TI CC2642R1 快速搭建VsCode开发环境
  18. 概念肆虐:凭什么让“IPv9”横行无忌?
  19. 在线查看word文档
  20. 生死狙击2等待服务器响应,《生死狙击2》劫掠测试结束:热爱不止 来日再战!...

热门文章

  1. 如何利用Tensorflow和OpenCV构建实时对象识别程序?
  2. ∇SLAM:自动可微分SLAM
  3. CUDA FORTRAN编译器
  4. zabbix-agent 启动不起来
  5. Gitlab用户权限管理
  6. 证券期货交易高并发模型
  7. 转载 Android 多线程处理之多线程用法大集合
  8. 老王教你怎样干掉局域网的IDA验证
  9. css页面重构面试题
  10. 【转】AVAudioPlayer播放音乐,最清晰明了