【BFS】hdu 1973 Prime Path
题目描述:
http://poj.org/problem?id=3414
中文大意:
使用两个锅,盛取定量水。
两个锅的容量和目标水量由用户输入。
允许的操作有:灌满锅、倒光锅内的水、一个锅中的水倒入另一个锅。
在两个锅互相倒水的过程中,若一个锅被倒满,而原来的锅中还有水,则剩余在原来的锅中。
不断执行这三个过程,直到任意一个锅中,贮存了目标数量的水,过程结束。
思路:
队列节点记录的是当前两个锅的贮水量和之前的一系列操作。
在弹出队列首节点,获取了当前两个锅的水量信息后,后续怎么操作,有 6 种选择:FILL(1)、FILL(2)、DROP(1)、DROP(2)、POUR(1,2)、POUR(2,1)。
注意:记得声明一个 visited[105][105] 数组,用来记录当前的情况是否出现过,若没有出现过,再将其压入队列,否则会超时。
代码:
#include<iostream>
#include<queue>
#include<string>
using namespace std; int a,b,c; struct node{
int x,y;
vector<string> msg;
node(int x, int y, vector<string> msg){
this->x = x;
this->y = y;
this->msg = msg;
};
node(int x, int y){
this->x = x;
this->y = y;
};
}; bool visited[105][105] = {false}; void bfs(){
node start = node(0, 0);
node next = node(0, 0); visited[0][0] = true; queue<node> q;
q.push(start); while(!q.empty()){
start = q.front();
q.pop(); if(start.x == c || start.y == c){
int n = start.msg.size();
printf("%d\n", n); for(int i=0;i<n;i++){
cout<<start.msg[i]<<endl;
}
return;
} //FILL(1)
next = node(a, start.y, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("FILL(1)");
q.push(next);
} //FILL(2)
next = node(start.x, b, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("FILL(2)");
q.push(next);
} //DROP(1)
next = node(0, start.y, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("DROP(1)");
q.push(next);
} //DROP(2)
next = node(start.x, 0, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("DROP(2)");
q.push(next);
} //POUR(1,2)
int fill_num = b - start.y;
if(start.x >= fill_num){
next = node(start.x - fill_num, b, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("POUR(1,2)");
q.push(next);
}
}
else{
next = node(0, start.y + start.x, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("POUR(1,2)");
q.push(next);
}
} //POUR(2,1)
fill_num = a - start.x;
if(start.y >= fill_num){
next = node(a, start.y - fill_num, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("POUR(2,1)");
q.push(next);
}
}
else{
next = node(start.x + start.y, 0, start.msg);
if(!visited[next.x][next.y]){
visited[next.x][next.y] = true; next.msg.push_back("POUR(2,1)");
q.push(next);
}
}
}
printf("impossible\n");
} int main(){
scanf("%d %d %d", &a, &b, &c);
bfs();
}
【BFS】hdu 1973 Prime Path的更多相关文章
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- HDU - 1973 - Prime Path (BFS)
Prime Path Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
- 【BFS】HDU 1495
直达–> HDU 1495 非常可乐 相似题联动–>POJ 3414 Pots 题意:中文题,不解释. 思路:三个杯子倒来倒去,最后能让其中两个平分即可.可能性六种.判定的时候注意第三个杯 ...
- 【CF653G】Move by Prime 组合数
[CF653G]Move by Prime 题意:给你一个长度为n的数列$a_i$,你可以进行任意次操作:将其中一个数乘上或者除以一个质数.使得最终所有数相同,并使得操作数尽可能小.现在我们想要知道$ ...
- 【题解】HDU Homework(倍增)
[题解]HDU Homework(倍增) 矩阵题一定要多多检查一下是否行列反了... 一百个递推项一定要存101个 说多了都是泪啊 一下午就做了这一道题因为实在是太菜了太久没写这种矩阵的题目... 设 ...
- 【动态规划】HDU 5492 Find a path (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意: 一个N*M的矩阵,一个人从(1,1)走到(N,M),每次只能向下或向右走.求(N+ ...
- poj3278-Catch That Cow 【bfs】
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
随机推荐
- Centos 6.5 Rabbitmq 安装和集群,镜像部署
centos 6.5 rabbitmq 安装和集群,镜像部署 安装erlang: yum install gcc glibc-devel make ncurses-devel openssl-deve ...
- 十八:SQL注入之堆叠及绕WAF
堆叠查询注入 (双查询注入) stacked injections(堆叠注入)从名词的含义就可以看到是一堆的SQL语句一起执行,而在真实的运用中也是这样的,我们知道在mysql中,主要是命令行中,每一 ...
- Linux 用户操作之用户管理 (用户增删改操作)
目录 添加用户 删除用户 修改用户 切换用户 配置用户密码 查看配置文件 cat /etc/pwsswd 添加用户 可选项 -c comment 指定一段注释性描述. -d 目录 指定用户主目录,如果 ...
- 【Linux】dlopen failed: /lib/lsiRAID.so: cannot open shared object file: No such file or directory
遇到这个问题,首先第一反应,是看其他的服务器中是否有这个库文件,如果有的话直接cp过来一份就行 但是检查发现,其他的系统中也不存在lsiRAID.so这个库文件,很神奇.. 但是看日志持续报错,查看s ...
- P1273 有线电视网(树形动规,分组背包)
题目链接: https://www.luogu.org/problemnew/show/P1273 题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树 ...
- LeetCode938. 二叉搜索树的范围和
题目 1 class Solution { 2 public: 3 int sum = 0; 4 int rangeSumBST(TreeNode* root, int low, int high) ...
- vagrant up报错【io.rb:32:in `encode': "\x95" followed by "\"" on GBK (Encoding::InvalidByteSequenceError)】
vagrant up报错[io.rb:32:in `encode': "\x95" followed by """ on GBK (Encoding: ...
- MySQL数据库基础知识及优化
MySQL数据库基础知识及优化必会的知识点,你掌握了多少? 推荐阅读: 这些必会的计算机网络知识点你都掌握了吗 关于数据库事务和锁的必会知识点,你掌握了多少? 关于数据库索引,必须掌握的知识点 目录 ...
- spring boot 集成 websocket 实现消息主动
来源:https://www.cnblogs.com/leigepython/p/11058902.html pom.xml 1 <?xml version="1.0" en ...
- ECharts图表——封装通用配置
前言 前段时间在做大屏项目,大量用到echarts图表,大屏对设计规范要求比较高,而大屏项目,经常会因为业务方面的原因.或者是数据方面的原因改动UI设计,所有图表的代码也是三天一小改.五天一大改 因此 ...