【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 ...
随机推荐
- hbase:ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet
hbase连接deug:DEBUG [main-SendThread(bigdata.server1:2181)] - Reading reply sessionid:0x16f764e7f6e000 ...
- Npoi XWPF Word 导出时插入图片无法显示 bug 完美解决
一.来自客户的需求 最近客户来个新需求生成一个word 标签纸,并且需要在标签纸上插入一个logo,并且将erp 中的数据取出来自动写在文档上,不由得淡淡一笑,这不难呀! 于是乎我就写下了这样的代码: ...
- windows端口占用
原文链接http://zhhll.icu/2020/04/08/windows/windows%E4%B9%8B%E7%AB%AF%E5%8F%A3%E5%8D%A0%E7%94%A8/ 1.查看当前 ...
- 知识图谱和neo4j的基本操作
一.知识图谱的简介 1.知识图谱是什么 知识图谱本质上是语义网络(Semantic Network)的知识库 可以理解为一个关系图网络. 2.什么是图 图(Graph)是由节点(Vertex)和边(E ...
- ASP.NET Core 上传文件到共享文件夹
参考资料:ASP.NET 上传文件到共享文件夹 创建共享文件夹参考资料:https://www.cnblogs.com/dansediao/p/5712657.html 一.配置上传文件相关参数并读取 ...
- Linux基础命令整合
linux基础命令整理 1.系统相关命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours:minu ...
- Linux调整lvm逻辑分区大小
转载自:https://www.cnblogs.com/kevingrace/p/5825963.html 个人记录一下 Linux下对lvm逻辑卷分区大小的调整(针对xfs和ext4不同文件系 ...
- 摆脱 996——GitHub 热点速览 v.21.03
作者:HelloGitHub-小鱼干 Twitter 有位程序员总结了本周的 GitHub 中文程序员的看点:国内程序员日常--考公务员.996.抢茅台.刷算法.整健康码.在本期热点速览里,小鱼干收录 ...
- SDUST数据结构 - chap7 图
判断题: 选择题: 函数题: 6-1 邻接矩阵存储图的深度优先遍历: 裁判测试程序样例: #include <stdio.h> typedef enum {false, true} boo ...
- [工作札记]03: 微软Winform窗体中ListView、DataGridView等控件的Bug,会导致程序编译失败,影响范围:到最新的.net4.7.2都有
工作中,我们发现了微软.net WinForm的一个Bug,会导致窗体设计器自动生成的代码失效,这个Bug从.net4.5到最新的.net4.7.2都存在,一直没有解决.最初是我在教学工作中发现的,后 ...