poj 3414(简单bfs)
题目链接:http://poj.org/problem?id=3414
思路:bfs简单应用,增对瓶A或者瓶B进行分析就可以了,一共6种状态。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; struct Node{
int a,b,step;
char str[][];
}; int A,B,C;
bool mark[][];
bool bfs()
{
memset(mark,false,sizeof(mark));
queue<Node>que;
Node p,q;
p.a=,p.b=,p.step=;
que.push(p);
mark[][]=true;
while(!que.empty()){
p=que.front();
que.pop();
if(p.a==C||p.b==C){
printf("%d\n",p.step);
for(int i=;i<=p.step;i++){
printf("%s\n",p.str[i]);
}
return true;
}
if(p.a==){
q=p;
q.a=A;
q.step++;
strcpy(q.str[q.step],"FILL(1)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}else if(p.a<=A){
q=p;
q.a=;
q.step++;
strcpy(q.str[q.step],"DROP(1)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
if(p.b<B){
q=p;
if(q.a+q.b<=B){
q.b+=q.a;
q.a=;
}else {
q.a=(q.b+q.a)-B;
q.b=B;
}
q.step++;
strcpy(q.str[q.step],"POUR(1,2)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}
}
if(p.b==){
q=p;
q.b=B;
q.step++;
strcpy(q.str[q.step],"FILL(2)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}else if(p.b<=B){
q=p;
q.b=;
q.step++;
strcpy(q.str[q.step],"DROP(2)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
if(p.a<A){
q=p;
if(q.b+q.a<=A){
q.a+=q.b;
q.b=;
}else {
q.b=(q.b+q.a)-A;
q.a=A;
}
q.step++;
strcpy(q.str[q.step],"POUR(2,1)");
if(!mark[q.a][q.b]){
mark[q.a][q.b]=true;
que.push(q);
}
}
}
}
return false;
} int main()
{
while(~scanf("%d%d%d",&A,&B,&C)){
if(!bfs())puts("impossible");
}
return ;
}
poj 3414(简单bfs)的更多相关文章
- Pots(POJ - 3414)【BFS 寻找最短路+路径输出】
Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
- poj 3414 Pots bfs+模拟
#include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...
- POJ 1101 简单BFS+题意
The Game 题意: Description One morning, you wake up and think: "I am such a good programmer. Why ...
- POJ 3414 Pots ( BFS , 打印路径 )
题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...
- POJ 3414 Pots(BFS)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
- poj 3278 简单BFS
题意:给定农夫和奶牛的初始位置,农夫可以当前位置+1.-1.*2三种移动方式,问最少需要多少分钟抓住奶牛 AC代码: #include<cstdio> #include<cstrin ...
- poj 3414 Pots(bfs+输出路径)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ - 3414 Pots BFS(著名倒水问题升级版)
Pots You are given two pots, having the volume of A and B liters respectively. The following operati ...
随机推荐
- TI BSL in python
今天花了一下午在这个上面,被这个工具整没脾气了快.花点功夫记录一下. 代码是在linux下下过来的: bzr branch lp:python-msp430-tools 看了下,有我想要的器件.然后想 ...
- 3、jQuery的DOM基础
DOM模型在页面文档中,通过树状模型展示页面的元素和内容,其展示的方式则是通过节点(node)来实现的. 3.1 访问元素 3.1.1 元素属性操作 Attr()可以对元素属性执行获取和设置操作,而r ...
- Hadoop-2.4.1学习之怎样确定Mapper数量
MapReduce框架的优势是能够在集群中并行运行mapper和reducer任务,那怎样确定mapper和reducer的数量呢,或者说怎样以编程的方式控制作业启动的mapper和reducer数量 ...
- struts 防止重复提交表单
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC &qu ...
- 服务器server2008网站iis7+php环境的搭建
IIS+FastCGI+PHP5_32_Gzip环境搭建-------------------0.装iis71.把php_5.32_win86解压到d:\php2.php.ini文件已经配置好不需要在 ...
- Nav titleView 设置的两个方式
1.self.navigationItem.titleView = vv; 2.[self.navigationController.navigationBar addSubview:vv];
- Mongodb更新数组$pull修饰符
http://blog.csdn.net/yaomingyang/article/details/78701643 一.$pull修饰符会删除掉数组中符合条件的元素,使用的格式是: { $pull: ...
- atitit.php 流行框架 前三甲为:Laravel、Phalcon、Symfony2 attilax 总结
atitit.php 流行框架 前三甲为:Laravel.Phalcon.Symfony2 attilax 总结 1. ,最流行的PHP框架前三甲为:Laravel.Phalcon.Symfony2. ...
- PHPCMS 学习
1.碎片管理2.为了升级操作 MY_ thinkphp为大写phpcms里面也是大写 然后继承如果加构造函数要调用一次父类的构造函数,最好在最上面调用 final 不可重写 重写最好调用一次paren ...
- sdut Mountain Subsequences 2013年山东省第四届ACM大学生程序设计竞赛
Mountain Subsequences 题目描述 Coco is a beautiful ACMer girl living in a very beautiful mountain. There ...