Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10042   Accepted: 4221   Special Judge

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 ≤ ≤ 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 AB, 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) 应用时:10min
实际用时:57min
原因:六种操作都是不可逆转的.
思路:时间非常充裕到不需要建反边,数据太小
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=101;
int n,m;
typedef unsigned long long ull;
int A,B,C;
int vis[maxn][maxn];
int ans[maxn][maxn][maxn*maxn];
struct node{
int a,b;
node (int ta,int tb):a(ta),b(tb){}
};
void printop(int op){
switch(op){
case 0:
puts("FILL(1)");
break;
case 1:
puts("FILL(2)");
break;
case 2:
puts("POUR(1,2)");
break;
case 3:
puts("POUR(2,1)");
break;
case 4:
puts("DROP(1)");
break;
case 5:
puts("DROP(2)");
break;
}
}
void op(int &a,int &b,int op){
switch(op){
case 0:
a=A;
break;
case 1:
b=B;
break;
case 2:
if(b+a<=B){
b+=a;
a=0;
}
else {
a-=B-b;
b=B;
}
break;
case 3:
if(b+a<=A){
a+=b;
b=0;
}
else {
b-=A-a;
a=A;
}
break;
case 4:
a=0;
break;
case 5:
b=0;
break;
}
}
void bfs(){
queue <node> que;
que.push(node(0,0));
vis[0][0]=0;
while(!que.empty()){
node tp=que.front();que.pop();
int ta=tp.a;
int tb=tp.b;
if(tp.a==C||tp.b==C){
printf("%d\n",vis[tp.a][tp.b]);
for(int i=0;i<vis[ta][tb];i++){
int op=ans[ta][tb][i];
printop(op);
}
return ;
}
for(int i=0;i<6;i++){
int ta=tp.a;
int tb=tp.b;
op(ta,tb,i);
if(vis[ta][tb]==-1){
vis[ta][tb]=vis[tp.a][tp.b]+1;
for(int j=0;j<vis[tp.a][tp.b];j++){
ans[ta][tb][j]=ans[tp.a][tp.b][j];
}
ans[ta][tb][vis[tp.a][tp.b]]=i;
que.push(node(ta,tb));
}
}
}
puts("impossible");
}
int main(){
scanf("%d%d%d",&A,&B,&C);
memset(vis,-1,sizeof(vis));
bfs();
return 0;
}

  

快速切题 poj3414 Pots的更多相关文章

  1. POJ3414—Pots(bfs加回溯)

    http://poj.org/problem?id=3414                                       Pots Time Limit: 1000MS   Memor ...

  2. POJ-3414 Pots (BFS)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  3. poj3414 Pots (BFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12198   Accepted: 5147   Special J ...

  4. POJ3414 Pots —— BFS + 模拟

    题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  5. POJ3414 Pots BFS搜素

    题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...

  6. POJ-3414.Pots.(BFS + 路径打印)

    这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...

  7. 快速切题sgu127. Telephone directory

    127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...

  8. 快速切题sgu126. Boxes

    126. Boxes time limit per test: 0.25 sec. memory limit per test: 4096 KB There are two boxes. There ...

  9. 快速切题 sgu123. The sum

    123. The sum time limit per test: 0.25 sec. memory limit per test: 4096 KB The Fibonacci sequence of ...

随机推荐

  1. 风景区的面积及道路状况分析问题 test

    参考文献:   https://wenku.baidu.com/view/b6aed86baf1ffc4ffe47ac92.html #include <bits/stdc++.h> us ...

  2. 获取String类型汉字乱码,如何进行编码

    本文为博主原创,未经允许不得转载: 在解析properties文件中的汉字时,在java代码中解析得到的是一个乱码字符,形如图下: 导致乱码原因:由于在jdk中,默认为gbk编码方式进行编码盒接收的, ...

  3. 汇编语言调用Linux系统调用read和write

    .section .data UserMsg: .ascii "Please input the message:" LenOfUserMsg: .equ lenMsg, LenO ...

  4. [ios]IOS的AppDelegate方法中的事件触发调用 以及 关闭 ios应用程序

    IOS的AppDelegate方法中的事件触发调用 参考:http://blog.sina.com.cn/s/blog_a573f7990101bphp.html //当应用程序将要进入非活动状态执行 ...

  5. Codeforces 855B - Marvolo Gaunt's Ring

    855B - Marvolo Gaunt's Ring 思路:①枚举a[j],a[i]和a[k]分别用前缀最小值最大值和后缀最小值和后缀最大值确定. ②dp,dp[i][j]表示到第j为止,前i+1个 ...

  6. vue.js循环for(列表渲染)详解

    vue.js循环for(列表渲染)详解 一.总结 一句话总结: v-for <ul id="example-1"> <li v-for="item in ...

  7. [.NET开发] C# 合并、拆分PDF文档

    在整理文件时,将多个同类型文档合并是实现文档归类的有效方法,也便于文档管理或者文档传输.当然,也可以对一些比较大的文件进行拆分来获取自己想要的部分文档.可以任意地对文档进行合并.拆分无疑为我们了提供极 ...

  8. 4-12 如何搜索API

    遇到一个参数prompt,使用rails ,API没有找到,怎么办? site关键字 在全网搜索 或者google一下,或是在stack overflow 上找答案 prompt 是FormOptio ...

  9. Confluence 6 管理多目录概述

    这里是有关目录顺序如何影响处理流程: 目录中的顺序是被用来如何查找用户和组的顺序. 修改用户和用户组将会仅仅应用到应用程序具有修改权限的第一个目录中. 配置目录载入顺序 你可以修改在 Confluen ...

  10. Hololens 开发环境配置(转)

    转自 Vangos Pterneas, 4 Apr 2016 CPOL 5.00 (1 vote) vote 1vote 2vote 3vote 4vote 5 The past few days h ...