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 ...
随机推荐
- Android常用异步任务执行方法
Handler原理及基本概念 Message 意为消息,发送到Handler进行处理的对象,携带描述信息和任意数据. MessageQueue 意为消息队列,Message的集合. Looper 有着 ...
- Visual studio之C# 利用Settings保存COM口配置信息
背景 利用C#做一个串口通信项目,客户需求保存串口COM口的配置信息,在此利用Settings来进行保存. 正文 ".Settings"的创建 点击项目 --> 添加新项 - ...
- input-form-select-a-img-ul-dl标签
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Asp.Net Mvc+Localdb数据库项目在IIS部署的配置
1.将数据库文件放到App_Data文件夹下 2.Web.config连接字符串配置 <add name="TestEntities" connectionString=&q ...
- U盘装系统:安装GHOST Win7系统教程
可以搜索:装机吧 原文地址:http://www.zhuangjiba.com/jiaocheng/show-27-247-1.html
- FusionCharts:tooltip分行显示
FusionCharts:tooltip分行显示 tooltip分行显示:如果tooltip过长,可分行显示,在tooltip中增加{br} <chart> <set label=' ...
- position固定菜单栏
position:fixed来实现. <!DOCTYPE html> <html lang="en"> <head> <meta char ...
- zookeeper伪分布式安装
本文介绍zookeeper伪分布式安装. 所谓 “伪分布式集群” 就是在1台PC中启动多个zookeeper的实例.“完全分布式集群” 是每1台PC启动1个ZooKeeper实例. 由于我的测试环境P ...
- The Way to Go读书笔记_第4章_基本结构和基本数据类型
“_”标识符 _ 本身就是一个特殊的标识符,被称为空白标识符.它可以像其他标识符那样用于变量的声明或赋值(任何类型都可以赋值给它),但任何赋给这个标识符的值都将被抛弃,因此这些值不能在后续的代码中使用 ...
- 小马哥课堂-统计学-z分数
Standard score(z-分数) The standard score is the signed number of standard deviations by which the val ...