首页 技术 正文
技术 2022年11月17日
0 收藏 799 点赞 4,213 浏览 1620 个字

直达 —> POJ 3414 Pots

相似题联动–>HDU 1495 非常可乐

题意:两个壶倒水,三种操作,两个桶其中一个满足等于C的最少操作,输出路径。注意a,b互倒的时候能不能倒满,或者还有剩下的。

a->b || b->a || a->0 || b->0 || a->A || b->B (0<=a<=A&&0<=b<=B)

思路:虽说是BFS但是情况就这几种,分别写出来之后判断即可。输出路径可以用递归,我这里用了string来存。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
using namespace std;
const int maxn = 1000+5;
char *S[3] ={"FILL","POUR","DROP"};
int A,B,C;
int vis[maxn][maxn];
struct node{
int a,b;
string opt[maxn]; //存路径再输出
int step ;
}ans;
bool check(node x){
if(x.a == C||x.b == C)
return true;
return false;
}
int bfs(){
queue<node>Q;
node p,t;
p.a = p.b = p.step = 0;
Q.push(p);
while(Q.size()){
p = Q.front();
Q.pop();
if(check(p)) {
ans = p;
return true;
}
//DROP(1)
if(!vis[0][p.b]){
t = p;
t.a = 0;
t.step++;
t.opt[t.step] = "DROP(1)";
Q.push(t);
vis[0][p.b] = 1;
}
//DROP(2)
if(!vis[p.a][0]){
t = p;
t.b = 0;
t.step++;
t.opt[t.step] = "DROP(2)";
Q.push(t);
vis[p.a][0] = 1;
}
//FILL(1)
if(!vis[A][p.b]){
t = p;
t.a = A;
t.step++;
t.opt[t.step] = "FILL(1)";
Q.push(t);
vis[A][p.b] = 1;
}
//FILL(2)
if(!vis[p.a][B]){
t = p;
t.b = B;
t.step++;
t.opt[t.step] = "FILL(2)";
Q.push(t);
vis[p.a][B] = 1;
}
//POUR(2,1)
if(!vis[p.b>(A-p.a)?A:(p.a+p.b)][p.b>(A-p.a)?(p.b-A+p.a):0]){
t = p;
t.a = p.b>(A-p.a)?A:(p.a+p.b);
t.b = p.b>(A-p.a)?(p.b-A+p.a):0;
t.step++;
t.opt[t.step] = "POUR(2,1)";
Q.push(t);
vis[t.a][t.b] = 1;
}
//POUR(1,2)
if(!vis[p.a>(B-p.b)?(p.a-B+p.b):0][p.a>(B-p.b)?B:(p.a+p.b)]){
t = p;
t.a = p.a>(B-p.b)?(p.a-B+p.b):0;
t.b = p.a>(B-p.b)?B:(p.a+p.b);
t.step++;
t.opt[t.step] = "POUR(1,2)";
Q.push(t);
vis[t.a][t.b] = 1;
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&A,&B,&C)){
memset(vis,0,sizeof(vis));
if(bfs()>0){
cout<<ans.step<<endl;
for(int i=1;i<=ans.step;i++)
cout<<ans.opt[i]<<endl;
}
else printf("impossible\n");
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291