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,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
随机推荐
- redis之mq实现发布订阅模式
示例代码-github 概述 Redis不仅可作为缓存服务器,还可用作消息队列,本示例演示如何使用redis实现发布/订阅消息队列. 在Redis中,发布者没有将消息发送给特定订阅者的程序.相反,发布 ...
- Linux系统调用表(x86_64)
内核版本 Linux 4.7 Note:64位系统中,不再使用int 0x80来进行系统调用,取而代之的是syscall指令 %rax System call %rdi %rsi %rdx %r10 ...
- 漫谈Spring Security 在Spring Boot 2.x endpoints中的应用(一)
Spring Boot 2.x极大简化了默认的安全配置,并不是说有很多安全相关的配置,现在你只需要提供一个WebSecurityConfigurerAdapter继承类这样一个简单的操作,Spring ...
- Java连载28-内存分析
一.方法在执行过程中是如何分配内存的,内存是如何变化的? 1.方法只定义,不调用,是不会执行的,并且在JVM中也不会给该方法分配”运行所属“的内存空间,只有在调用这个方法的时候,才会动态的给这个方法分 ...
- GA,RC,Alpha,Beta,Final等软件版本名词释义
对应上图的表格如下: 名词 说明 Alpha α是希腊字母的第一个,表示最早的版本,内部测试版,一般不向外部发布,bug会比较多,功能也不全,一般只有测试人员使用. Beta β是希腊字母的第二个,公 ...
- windows系统下安装JDK8
学习JAVA,必须得安装一下JDK(java development kit java开发工具包),配置一下环境就可以学习JAVA了,下面是下载和安装JDK的教程: 一.下载 1.JDK下载地址: h ...
- PythonI/O进阶学习笔记_4.自定义序列类(序列基类继承关系/可切片对象/推导式)
前言: 本文代码基于python3 Content: 1.python中的序列类分类 2. python序列中abc基类继承关系 3. 由list的extend等方法来看序列类的一些特定方法 4. l ...
- Linux下Mysql启动异常排查方案
遇到Mysql启动异常问题,可以从以下几个方面依次进行问题排查: (1)如果遇到“Can't connect to local MySQL server through socket '/tmp/my ...
- GO 语言学习笔记--数组切片篇
1.对于make 数组切片,长度和容量需要理解清楚: 容量表示底层数组的大小,长度是你可以使用的大小: 容量的用处在哪?在与当你用 appen d扩展长度时,如果新的长度小于容量,不会更换底层数组,否 ...
- AtomicInteger 一个提供原子操作的Integer类
转自:http://www.blogjava.net/freeman1984/archive/2011/10/17/361402.html AtomicInteger,一个提供原子操作的Integer ...