POJ 3414:Pots
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 11661 | Accepted: 4940 | Special Judge |
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its
contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the
desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
题意是每次对面前的两个壶有六种选择,倒掉1壶的水,倒掉2壶的水。填满1壶的水,填满2壶的水。把2壶的水倒入1壶,把1壶的水倒入2壶。
然后求想要达到结果C毫升水的最小路径,并输出这个路径。
这个题目形式又是 选择+最小路径 。广度优先搜索无疑。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; queue <int> pot1;
queue <int> pot2; int buzhou[105][105];
string path[105][105];// drop[1] drop[2] fill[1] fill[2] pour[2,1] pour[1,2] int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int A,B,C,flag,v1,v2,i;
cin>>A>>B>>C; memset(buzhou,0,sizeof(buzhou)); pot1.push(0);
pot2.push(0); flag=0;
v1=0;
v2=0;
buzhou[v1][v2]=1; while(pot1.size()&&pot2.size())
{
v1=pot1.front();
v2=pot2.front(); pot1.pop();
pot2.pop(); if(v1==C||v2==C)
{
flag=1;
cout<<buzhou[v1][v2]-1<<endl;
for(i=0;i<path[v1][v2].size();i++)
{
if(path[v1][v2][i]=='1')
cout<<"DROP(2)"<<endl;
else if(path[v1][v2][i]=='2')
cout<<"DROP(1)"<<endl;
else if(path[v1][v2][i]=='3')
cout<<"FILL(1)"<<endl;
else if(path[v1][v2][i]=='4')
cout<<"FILL(2)"<<endl;
else if(path[v1][v2][i]=='5')
cout<<"POUR(2,1)"<<endl;
else if(path[v1][v2][i]=='6')
cout<<"POUR(1,2)"<<endl;
}
break;
}
if(!buzhou[0][v2])//drop1
{
buzhou[0][v2]=buzhou[v1][v2]+1;
path[0][v2] = path[v1][v2]+"2";
pot1.push(0);
pot2.push(v2);
}
if(!buzhou[v1][0])//drop2
{
buzhou[v1][0]=buzhou[v1][v2]+1;
path[v1][0] = path[v1][v2]+"1";
pot1.push(v1);
pot2.push(0);
}
if(!buzhou[v1][B])//fill2
{
buzhou[v1][B]=buzhou[v1][v2]+1;
path[v1][B] = path[v1][v2]+"4";
pot1.push(v1);
pot2.push(B);
}
if(!buzhou[A][v2])//fill1
{
buzhou[A][v2]=buzhou[v1][v2]+1;
path[A][v2] = path[v1][v2]+"3";
pot1.push(A);
pot2.push(v2);
} if(v1+v2<A)//b to a
{
if(!buzhou[v1+v2][0])
{
buzhou[v1+v2][0]=buzhou[v1][v2]+1;
path[v1+v2][0]= path[v1][v2]+"5";
pot1.push(v1+v2);
pot2.push(0);
}
}
else
{
if(!buzhou[A][v1+v2-A])
{
buzhou[A][v1+v2-A]=buzhou[v1][v2]+1;
path[A][v1+v2-A]= path[v1][v2]+"5";
pot1.push(A);
pot2.push(v1+v2-A);
}
} if(v1+v2<B)
{
if(!buzhou[0][v1+v2])
{
buzhou[0][v1+v2]=buzhou[v1][v2]+1;
path[0][v1+v2]= path[v1][v2]+"6";
pot1.push(0);
pot2.push(v1+v2);
}
}
else
{
if(!buzhou[v1+v2-B][B])
{
buzhou[v1+v2-B][B]=buzhou[v1][v2]+1;
path[v1+v2-B][B]= path[v1][v2]+"6";
pot1.push(v1+v2-B);
pot2.push(B);
}
}
}
if(!flag)
cout<<"impossible"<<endl;
system("pause");
return 0;
}
POJ 3414:Pots的更多相关文章
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- 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 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,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
- Pots(POJ - 3414)【BFS 寻找最短路+路径输出】
Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
- Pots POJ 3414
/* *POJ 3414 *简单模板bfs *编程应该为了方便理解,尽量提供接口 */ #include<cstdio> #include<algorithm> #includ ...
- 【BFS】POJ 3414
直达 -> POJ 3414 Pots 相似题联动–>HDU 1495 非常可乐 题意:两个壶倒水,三种操作,两个桶其中一个满足等于C的最少操作,输出路径.注意a,b互倒的时候能不能倒满, ...
随机推荐
- HDU 2476 区间DP String painter
题解 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm ...
- Centos7 安装python3详细教程,解决升级后不兼容问题
一.确认当前python版本 [root@centos Python-3.6.1]# python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC ...
- mysql replication常见错误整理
这篇文章旨在记录MySQL Replication的常见错误,包括自己工作中遇到的与网友在工作中遇到的,方面自己及别人以后进行查找.每个案例都是通过Last_IO_Errno/Last_IO_Erro ...
- Python基础数据类型之集合
Python基础数据类型之集合 集合(set)是Python基本数据类型之一,它具有天生的去重能力,即集合中的元素不能重复.集合也是无序的,且集合中的元素必须是不可变类型. 一.如何创建一个集合 #1 ...
- 【LeetCode】Maximize Sum Of Array After K Negations(K 次取反后最大化的数组和)
这道题是LeetCode里的第1005道题. 题目描述: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次. ...
- windows和ubuntu14.04双系统设置默认启动项
首先开机或者重启,在启动项选择菜单处记住win7对应的序号,从上至下的序号从0开始计数,我的win7系统选项处于第5个,那么序号就应该是4,记住后,打开ubuntu系统. 2 按下Ctrl+alt+T ...
- 【Ts 3】Nginx的Http(图片)服务器配置+ftp上传使用说明
在前两篇博客中提到了搭建Nginx和Ftp服务器,在本篇博客,主要是介绍Nginx的配置文件的使用,怎样修改配置文件使其成为一个图片服务器. 一.Nginx图片服务器配置 <span style ...
- HDU-3065 病毒侵袭持续中 AC自动机又是一板子!
病毒侵袭持续中 上一题是求出现多少病毒输出病毒序号,而这题输出每个病毒出现的次数.这题有字典树基础都能做出来,把叶子节点用相应的编号标记起来,匹配的时候遍历到叶子节点用一个数组把次数存起来就行了. 有 ...
- noip普及组考纲+样题合集——初级篇(OIer必看)
很明显我是想发提高组合集的.普及组考纲……用发么. 当然如果你想看的话也可以,就一点点: 递归.排序…… 很明显上面那都不是重点.普及组只要掌握搜索.二分.单调队列.数学.随机化等等,一等奖没问题的, ...
- VMware VMnet8 模式共享主机网络配置静态 IP 和 DNS
一.简介 NAT网络模式: 1. 宿主机可以看做一个路由器,虚拟机通过宿主机的网络来访问 Internet: 2. 可以安装多台虚拟机,组成一个小型局域网,例如:搭建 hadoop 集群.分布式服务 ...