Pots POJ 3414
/*
*POJ 3414
*简单模板bfs
*编程应该为了方便理解,尽量提供接口
*/
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=1e2+10;
int VA,VB,VC;
bool inq[maxn][maxn];
vector<int>vec;
struct Node{
int a,b,step;//a,b的值,以及步数
vector<int>ope;
Node(){}
Node(int _a,int _b,int _s){
a=_a;
b=_b;
step=_s;
}
};
void fill(int index,int &A,int &B){
if(index==1) A=VA;
else if(index==2) B=VB;
else printf("Illegal operation!\n");
}
void drop(int index,int &A,int &B){
if(index==1) A=0;
else if(index==2) B=0;
else printf("Illegal operation!\n");
}
void pour(int in1,int in2,int &A,int &B){
if(in1==1&&in2==2){
if(A+B>=VB){
A=A+B-VB;
B=VB;
}
else{
B=A+B;
A=0;
}
}
else if(in1==2&&in2==1){
if(A+B>VA){
B=A+B-VA;
A=VA;
}
else{
A=A+B;
B=0;
}
}
}
Node BFS(){
memset(inq,false,sizeof(inq));
queue<Node>que;
que.push(Node(0,0,0));
inq[0][0]=true;
while(!que.empty()){
Node now=que.front();
que.pop();
int a=now.a,b=now.b,step=now.step;
if(a==VC||b==VC) return now;
for(int i=0;i<6;i++){
int tempa=a,tempb=b;
if(i==0) fill(1,tempa,tempb);
else if(i==1) fill(2,tempa,tempb);
else if(i==2) drop(1,tempa,tempb);
else if(i==3) drop(2,tempa,tempb);
else if(i==4) pour(1,2,tempa,tempb);
else if(i==5) pour(2,1,tempa,tempb);
if(inq[tempa][tempb]==false){
Node next=Node(tempa,tempb,step+1);
next.ope=now.ope;
next.ope.push_back(i);
que.push(next);
inq[tempa][tempb]=true;
}
}
}
return Node(-1,-1,-1);
}
int main(){
scanf("%d%d%d",&VA,&VB,&VC);
Node ans=BFS();
if(ans.a==-1) printf("impossible\n");
else{
printf("%d\n",ans.step);
for(int i=0;i<ans.ope.size();i++){
if(ans.ope[i]==0) printf("FILL(1)\n");
else if(ans.ope[i]==1) printf("FILL(2)\n");
else if(ans.ope[i]==2) printf("DROP(1)\n");
else if(ans.ope[i]==3) printf("DROP(2)\n");
else if(ans.ope[i]==4) printf("POUR(1,2)\n");
else if(ans.ope[i]==5) printf("POUR(2,1)\n");
}
}
return 0;
}
Pots POJ 3414的更多相关文章
- Pots(POJ - 3414)【BFS 寻找最短路+路径输出】
Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...
- Pots POJ - 3414 (搜索+记录路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22688 Accepted: 9626 Special J ...
- kuangbin专题 专题一 简单搜索 Pots POJ - 3414
题目链接:https://vjudge.net/problem/POJ-3414 题意:给你两个杯子,分别容量为A(1),B(2)和一个C,C是需要经过下列操作,得到的一个升数.(1) FILL(i) ...
- Pots POJ - 3414【状态转移bfs+回溯】
典型的倒水问题: 即把两个水杯的每种状态视为bfs图中的点,如果两种状态可以转化,即可认为二者之间可以连一条边. 有3种倒水的方法,对应2个杯子,共有6种可能的状态转移方式.即相当于图中想走的方法有6 ...
- POJ 3414 Pots
Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
随机推荐
- JDK中的Proxy技术实现AOP功能
1.需求,用户在执行某个业务方法时我们需要对这个用户进行判断是否具有权限(或者说用户是否登录了)? 例如修改用户信息,我们在update()方法前判断当前用户user是否为null(表示没有权限或者没 ...
- redis desktop manager安装及连接方式
1.下载安装包 官网下载地址:https://redisdesktop.com/pricing 官网下载需要付费使用 再此附上一个免费的破解版本,绿色安全可用 链接:https://pan.baidu ...
- xcode简介及安装
1. 简介 Xcode 是运行在操作系统Mac OS X上的集成开发工具(IDE),由苹果公司开发. Xcode是开发OS X 和 iOS 应用程序的最快捷的方式. Xcode 具有统一的用户界面设计 ...
- CF940A Points on the line 思维
A. Points on the line time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 运维核心基础知识之——MD5sum校验文件
如何使用MD5sum工具校验你的文件. 演示过程截图: 先给文件创建一个md5值 md5sum oldboy.txt 然后将md5sum生成的md5值写入到一个文件police.log md5sum ...
- SpringCloud 学习(二)-2 :Securing The Eureka Server
由于工作等种种原因未能连续进行学习,现在继续学习微服务,不过是新建的demo,springcloud版本用的是Finchley.SR2. 之前用简单demo实现了注册中心,现在来对注册中心加安全验证: ...
- 去重合并两个有序链表之直接操作和Set集合操作
两者思路对比: 直接操作:因为传入的是两个有序的链表,所以说我就直接以其中一个链表为基准,与另外一个链表比较,只将比返回值链表的最后一个记录的值大的插入,不将等值的插入,理论时间复杂度为O(n) Se ...
- 【ThinkPHP】API控制器中加入析构函数
<?php namespace app\api\controller; use think\Controller; class User extends Controller { public ...
- Spring Boot 入门之单元测试篇(五)
博客地址:http://www.moonxy.com 一.前言 JUnit 是一个由 Java 语言编写的开源的回归测试(回归测试是指重复以前全部或部分的相同测试)框架,由Erich Gamma 和 ...
- vim编辑python脚本时Tab补全
所属分类:成长之路 使用Linux写python脚本的时候,初期最痛苦的是什么?当然是各种库的不熟悉,知道了库,里面的方法还要挨个看,挨个记. 所以这时候,很多小伙伴使用了ipython,最强大的功能 ...