Pots

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 21   Accepted Submission(s) : 9
Special Judge
Problem Description

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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)
 
Source
PKU
 
 
 
BFS,用A中的水量乘1000加上B中的水量标记出现过的状态。用指针记录上一步操作的地址。
 
 
#include<iostream>
#include<cstring>
using namespace std; struct status //用于存储每一步操作后的状态
{
int x,y;
int op; //用于存储这一步的操作,用数字表示
status *last; //用于存储上一步操作的位置
}; int a,b,c;
int operate[1000]; //用于最后统计各步操作的
status que[1000];
bool f[100111]; //用来标记各步操作的结果是否出现过 status BFS()
{
int front=0,rear=1;
que[0].x=0;
que[0].y=0;
que[0].op=0;
que[0].last=NULL; //用来标记初始状态
f[que[0].x*1000+que[0].y]=true;
while(front<rear)
{
if(que[front].x!=a)
{
que[rear]=que[front];
que[rear].x=a;
if(!f[que[rear].x*1000+que[rear].y]) //如果没出现过这种状态
{
f[que[rear].x*1000+que[rear].y]=true;
que[rear].last=&que[front];
que[rear].op=1;
if(que[rear].x==c||que[rear].y==c)
return que[rear];
rear++;
}
}
if(que[front].y!=b)
{
que[rear]=que[front];
que[rear].y=b;
if(!f[que[rear].x*1000+que[rear].y])
{
f[que[rear].x*1000+que[rear].y]=true;
que[rear].last=&que[front];
que[rear].op=2;
if(que[rear].x==c||que[rear].y==c)
return que[rear];
rear++;
}
}
if(que[front].x!=0)
{
que[rear]=que[front];
que[rear].x=0;
if(!f[que[rear].x*1000+que[rear].y])
{
f[que[rear].x*1000+que[rear].y]=true;
que[rear].last=&que[front];
que[rear].op=3;
if(que[rear].x==c||que[rear].y==c)
return que[rear];
rear++;
}
}
if(que[front].y!=0)
{
que[rear]=que[front];
que[rear].y=0;
if(!f[que[rear].x*1000+que[rear].y])
{
f[que[rear].x*1000+que[rear].y]=true;
que[rear].last=&que[front];
que[rear].op=4;
if(que[rear].x==c||que[rear].y==c)
return que[rear];
rear++;
}
}
if(que[front].x!=0&&que[front].y!=b)
{
if(que[front].x+que[front].y<=b)
{
que[rear].x=0;
que[rear].y=que[front].x+que[front].y;
}
else
{
que[rear].x=que[front].x-(b-que[front].y);
que[rear].y=b;
}
if(!f[que[rear].x*1000+que[rear].y])
{
f[que[rear].x*1000+que[rear].y]=true;
que[rear].last=&que[front];
que[rear].op=5;
if(que[rear].x==c||que[rear].y==c)
return que[rear];
rear++;
}
}
if(que[front].x!=a&&que[front].y!=0)
{
if(que[front].x+que[front].y<=a)
{
que[rear].x=que[front].x+que[front].y;
que[rear].y=0;
}
else
{
que[rear].x=a;
que[rear].y=que[front].y-(a-que[front].x);
}
if(!f[que[rear].x*1000+que[rear].y])
{
f[que[rear].x*1000+que[rear].y]=true;
que[rear].last=&que[front];
que[rear].op=6;
if(que[rear].x==c||que[rear].y==c)
return que[rear];
rear++;
}
}
front++;
}
status no_ans; //如果while循环中没出现过答案
no_ans.op=-1; //标记没有结果
return no_ans;
} int main()
{
while(cin>>a>>b>>c)
{
memset(f,false,sizeof(f));
memset(operate,0,sizeof(operate));
status ans=BFS();
int i=0;
if(ans.op==-1)
cout<<"impossible"<<endl;
else
{
status *p=&ans;
while(p->op!=0)
{
operate[i++]=p->op; //倒序记录各步操作
p=p->last;
}
cout<<i<<endl;
for(i--;i>=0;i--) //正序输出
{
switch(operate[i])
{
case 1:cout<<"FILL(1)"<<endl;break;
case 2:cout<<"FILL(2)"<<endl;break;
case 3:cout<<"DROP(1)"<<endl;break;
case 4:cout<<"DROP(2)"<<endl;break;
case 5:cout<<"POUR(1,2)"<<endl;break;
case 6:cout<<"POUR(2,1)"<<endl;break;
}
}
}
}
}
 

HDOJ-三部曲一(搜索、数学)-1010-Pots的更多相关文章

  1. 三部曲一(搜索、数学)-1016-Code

    Code Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submissi ...

  2. HDU 4294 Multiple(搜索+数学)

    题意: 给定一个n,让求一个M,它是n个倍数并且在k进制之下 M的不同的数字最少. 思路: 这里用到一个结论就是任意两个数可以组成任何数的倍数.知道这个之后就可以用搜索来做了.还有一个问题就是最多找n ...

  3. HDOJ三部曲-DP-1017-pearls

    Pearls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submis ...

  4. 生日蛋糕 POJ - 1190 搜索 数学

    http://poj.org/problem?id=1190 题解:四个剪枝. #define _CRT_SECURE_NO_WARNINGS #include<cstring> #inc ...

  5. Leetcode初级算法(排序和搜索+数学篇)

    合并两个有序数组 开始的时候将这道题理解错了,发现几个奇怪的测试案例后才明白这道题什么意思.本来的想法就是把nums2全部放到num1里面,然后删除重复元素.排序一下,就有了下面的代码: class ...

  6. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  7. HDOJ的题目分类

    模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 10 ...

  8. HDOJ 题目分类

    HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:   ...

  9. hdoj分类

    http://blog.csdn.net/lyy289065406/article/details/6642573 模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 ...

  10. HDOJ题目分类

    模拟题, 枚举1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 104 ...

随机推荐

  1. js-- 一些题目

    1. ~~3.14~~3.14=-((~3.14)+1)=-(-(3.14+1)+1)=-(-(3+1)+1)=-(-4+1) =-(-3)=3 按位非(NOT)(~)操作数的负值减1. 2. var ...

  2. VS2012外接程序VMDebugger未能加载或导致了异常

    转http://blog.csdn.net/maryhuan/article/details/42676915 故障现象:打开Visual Studio 2010后弹出错误框,外接程序VMDebugg ...

  3. linq to xml学习

    http://www.cnblogs.com/greatverve/archive/2010/07/09/linq-to-xml-add-delete-update-query.html 记录一下,别 ...

  4. Java 集合系列 14 hashCode

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  5. MVC扩展ValueProvider,通过实现IValueProvider接口创建SessionValueProvider

    □ ValueProvider的大致工作原理 →通过Request.Form, Request.QueryString, Request.Files, RouteData.Values获取数据.→然后 ...

  6. [译]Quartz 框架 教程(中文版)2.2.x 之第一课 开始使用Quartz框架

    第一课:开始使用Quartz框架 在你使用调度器之前,需要借助一些具体的例子去理解(谁愿意只是猜啊?).你可以使用SchedulerFactory类来达到程序调度的目的.有一些Quartz框架的用户可 ...

  7. ios基础篇(三)——UIButton的详细介绍

    按钮UIButton是ios开发中最常见的控件之一,下面来介绍UIButton的详细内容: 一.UIButton的定义 UIButton *button=[[UIButton buttonWithTy ...

  8. apache2.4域名配置参数

    apache2.4和 2.2版本的配置有区别 2.4的配置如下 <VirtualHost *:80>DocumentRoot /www/web/projectServerName www. ...

  9. python使用urllib2抓取网页

    1.使用python的库urllib2,用到urlopen和Request方法. 2.方法urlopen原形 urllib2.urlopen(url[, data][, timeout]) 其中: u ...

  10. 一个QQ木马的逆向分析浅谈(附带源码)

    程序流程:首先注册自己程序的窗口以及类等一系列窗口操作,安装了一个定时器,间隔为100ms,功能搜索QQ的类名,如果找到就利用FindWindow("5B3838F5-0C81-46D9-A ...