POJ3414 Pots
题目:
输入:
有且只有一行,包含3个数A,B,C(1<=A,B<=100,C<=max(A,B))
输出:
样例:
分析:简单的BFS,难点在于回溯,给每个状态用数组记录路径
- #include<iostream>
- #include<sstream>
- #include<cstdio>
- #include<cstdlib>
- #include<string>
- #include<cstring>
- #include<algorithm>
- #include<functional>
- #include<iomanip>
- #include<numeric>
- #include<cmath>
- #include<queue>
- #include<vector>
- #include<set>
- #include<cctype>
- #define PI acos(-1.0)
- const int INF = 0x3f3f3f3f;
- const int NINF = -INF - ;
- typedef long long ll;
- using namespace std;
- int a, b, c;
- int used[][];
- struct node
- {
- int x, y;
- int flag;
- int path[];//数组中0-5分别表示6种不同操作
- }st;
- string print[] = {"FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};
- void bfs()
- {
- queue<node> q;
- for (int i = ; i <= a; ++i)
- {
- for (int j = ; j <= b; ++j)
- used[i][j] = INF;
- }
- memset(used, , sizeof(used));
- st.x = , st.y = ;
- st.flag = ;
- memset(st.path, -, sizeof(st.path));
- q.push(st);
- used[st.x][st.y] = ;
- while (q.size())
- {
- node temp = q.front();
- q.pop();
- if (temp.x == c || temp.y == c)
- {
- cout << temp.flag << endl;
- for (int i = ; i < temp.flag; ++i)
- cout << print[temp.path[i]] << endl;
- return;
- }
- for (int i = ; i < ; ++i)
- {
- node now = temp;
- now.flag++;
- if (i == && now.x != a)
- {
- now.x = a;
- if (!used[now.x][now.y])
- {
- used[now.x][now.y] = ;
- now.path[temp.flag] = ;
- q.push(now);
- }
- }
- else if (i == && now.y != b)
- {
- now.y = b;
- if (!used[now.x][now.y])
- {
- used[now.x][now.y] = ;
- now.path[temp.flag] = ;
- q.push(now);
- }
- }
- else if (i == && now.x != )
- {
- now.x = ;
- if (!used[now.x][now.y])
- {
- used[now.x][now.y] = ;
- now.path[temp.flag] = ;
- q.push(now);
- }
- }
- else if (i == && now.y != )
- {
- now.y = ;
- if (!used[now.x][now.y])
- {
- used[now.x][now.y] = ;
- now.path[temp.flag] = ;
- q.push(now);
- }
- }
- else if (i == )
- {
- if (now.x + now.y > b)
- {
- now.x -= b - now.y;
- now.y = b;
- }
- else
- {
- now.y += now.x;
- now.x = ;
- }
- if (!used[now.x][now.y])
- {
- used[now.x][now.y] = ;
- now.path[temp.flag] = ;
- q.push(now);
- }
- }
- else if (i == )
- {
- if (now.x + now.y > a)
- {
- now.y -= a - now.x;
- now.x = a;
- }
- else
- {
- now.x += now.y;
- now.y = ;
- }
- if (!used[now.x][now.y])
- {
- used[now.x][now.y] = ;
- now.path[temp.flag] = ;
- q.push(now);
- }
- }
- }
- }
- cout << "impossible" << endl;
- }
- int main()
- {
- cin >> a >> b >> c;
- bfs();
- return ;
- }
POJ3414 Pots的更多相关文章
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- POJ-3414 Pots (BFS)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- 快速切题 poj3414 Pots
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10042 Accepted: 4221 Special J ...
- poj3414 Pots (BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12198 Accepted: 5147 Special J ...
- POJ3414 Pots —— BFS + 模拟
题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ3414 Pots BFS搜素
题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...
- POJ-3414.Pots.(BFS + 路径打印)
这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...
- poj3414 Pots(BFS)
题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...
- 【BFS】Pots
[poj3414]Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16925 Accepted: 7168 ...
随机推荐
- SqlServer 导出指定表数据 生成Insert脚本
版权声明:本文为博主原创文章,未经博主允许不得转载.
- 关于angular双向绑定的一个问题,百度无果,还请帮忙解惑。
用了一段时间anjular蛮好用的.其实用的功能不多.主要用于列表数据绑定以及一些简单效果的绑定,但是最近出现一个现象,百度无果,居然没有人遇到.现在描述一下,截图不方便,希望有人解惑. 列表ng-r ...
- HDFS 处理命令记录
hdfs dfs -ls hdfs dfs -mkdir hdfs dfs -put hdfs dfs -get hdfs dfs -cat hadoop 执行jar 输出的目录 必须要不存在的 y ...
- python3:语法变动 及新特性
python3.0 对python2.x 升级后重大语法变动,幸好留下2.7.6及后续2版本,保持一些语法兼容. 原始地址:http://hi.baidu.com/jxq61/item/3a24883 ...
- react基础篇三
事件处理 React事件绑定属性的命名采用驼峰式写法,而不是小写. 如果采用 JSX 的语法你需要传入一个函数作为事件处理函数,而不是一个字符串(DOM元素的写法) 例如,传统的 HTML: < ...
- js 请求单个文件 并验证扩展名
function suffix(file_name) { var three=file_name.split("."); ]; return last; } $('#btnSear ...
- Memcached 之取模与哈希算法命中率实验
当5台memcache服务器中有一台宕机时的命中率实验. 一.php实现代码 1. config.php $server = array( "A" => array(&quo ...
- tomcat8版本实现虚拟主机
vim /etc/hosts192.168.30.21 www.crushlinux.com192.168.30.21 www.cloud.com [root@localhost ~]# cd ...
- 软件工程1916|W(福州大学)_助教博客】团队Beta冲刺作业(第9次)成绩公示
1. 作业链接: 项目Beta冲刺(团队) 2. 评分准则: 本次作业包括现场Beta答辩评分(映射总分为100分)+团队互评分数(总分40分)+博客分(总分130分)+贡献度得分,其中博客分由以下部 ...
- Problem 48
Problem 48 The series, 11 + 22 + 33 + ... + 1010 = 10405071317. Find the last ten digits of the seri ...