Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7783   Accepted: 3261   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)

Source

Northeastern Europe 2002, Western Subregion
 
 

问题链接:POJ3414 Pots。

题意简述:

  给出两个壶的容量A和B, 一个目标水量C,对A、B可以有3种操作,求最少经过几步操作能够在某个壶中得到目标水量C。输入A、B和C,输入最少操作数和操作过程。

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 potj is full (and there may be some water left in the poti), or the poti is empty (and all its contents have been moved to the potj).
问题分析:

  把A和B壶中水量作为状态,初始状态为<0,0>,每个操作都是状态变化的过程。因为有2个壶,所以总共有6种操作。使用BFS搜索来找到最少的操作步数。同时需要考虑操作的条件,以减少操作来加快程序运行速度。

程序说明:

  搜索过的状态就不需要再搜索了,用数组notvist[][]来标记搜索过的状态。操作的前提条件已经写在程序中。

BFS实现代码

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring> using namespace std; const int maxn=; string op[]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(2,1)","POUR(1,2)"}; int l,r;
int a,b,c;
int vis[maxn][maxn],step[maxn*maxn]; struct node{
int x,y;
int opr;
int pre;
}info[maxn*maxn]; void Solve(int x,int y,int opr){
if(vis[x][y])
return ;
vis[x][y]=;
info[r].x=x;
info[r].y=y;
info[r].opr=opr;
info[r].pre=l;
r++;
} void Print(){
int ans=;
while(l!=){
step[ans++]=info[l].opr;
l=info[l].pre;
}
printf("%d\n",ans);
for(int i=ans-;i>=;i--)
cout<<op[step[i]]<<endl;
} void BFS(){
info[].x=;
info[].y=;
vis[][]=;
l=;
r=;
int tx,ty;
while(l!=r){
if(info[l].x==c || info[l].y==c){
Print();
return ;
} tx=a;
ty=info[l].y;
Solve(tx,ty,); tx=info[l].x;
ty=b;
Solve(tx,ty,); tx=;
ty=info[l].y;
Solve(tx,ty,); tx=info[l].x;
ty=;
Solve(tx,ty,); tx=info[l].x+min(a-info[l].x,info[l].y);
ty=info[l].y-min(a-info[l].x,info[l].y);
Solve(tx,ty,); tx=info[l].x-min(b-info[l].y,info[l].x);
ty=info[l].y+min(b-info[l].y,info[l].x);
Solve(tx,ty,); l++;
}
if(l>=r)
printf("impossible\n");
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%d",&a,&b,&c)){
memset(vis,,sizeof(vis));
BFS();
}
return ;
}

DFS实现代码

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std; int a, b, c;
int va, vb; const int MAX = ;
int ans = MAX; int arr[] = {};
int arr1[] = {};
int k = ; int m[][] = {}; // 1:fill(1) 2: drop(1) 3: pour(1,2) 4: fill(2) 5: drop(2) 6: pour(2,1); int dfs(int a, int b, int cnt)
{
if(m[a][b] )
{
return ;
}
else
{
m[a][b] = ;
}
if(a == c || b == c)
{
if(ans >= cnt)
{
ans = cnt;
for(int i = ; i < cnt; i++)
{
arr1[i] = arr[i];
}
m[a][b] = ;
return ;
}
} arr[cnt] = ;
dfs(va, b, cnt + ); arr[cnt] = ;
//printf("cnt = %d\n", cnt);
dfs(, b, cnt + ); arr[cnt] = ;
if(vb - b < a)
{
dfs(a - vb + b, vb, cnt + );
}
else
{
dfs(, b + a, cnt + );
} arr[cnt] = ;
dfs(a, vb, cnt + ); arr[cnt] = ;
dfs(a, , cnt + ); arr[cnt] = ;
if(va - a < b)
{
dfs(va, b - va + a, cnt + );
}
else
{
dfs(a + b, , cnt + );
} m[a][b] = ;
} int main()
{
scanf("%d%d%d", &va, &vb, &c);
a = ;
b = ;
dfs(, , ); if(ans == MAX)
{
printf("impossible\n");
}
else
{
printf("%d\n", ans); for(int i = ; i < ans; i++)
{
if(arr1[i] == )
{
printf("FILL(1)\n");
} else if(arr1[i] == )
{
printf("DROP(1)\n");
} else if(arr1[i] == )
{
printf("POUR(1,2)\n");
} else if(arr1[i] == )
{
printf("FILL(2)\n");
} else if(arr1[i] == )
{
printf("DROP(2)\n");
} else
{
printf("POUR(2,1)\n");
}
}
} return ;
}

最新文章

  1. Handlebars 模板引擎之前后端用法
  2. 下载aptana插件jar包
  3. 《理解 ES6》阅读整理:函数(Functions)(一)Default Parameter Values
  4. 十一、常用的NSArray和NSMutableArray方法
  5. 用defy来潜水最终还是挂了........
  6. [转]SqlServer数据库同步方案详解
  7. Win7 64位 安装E10后 打不开的解决方案 -摘自网络
  8. ReactiveCocoa框架学习1
  9. guid 新建
  10. [jQuery编程挑战]006 生成一个倒计时效果
  11. log4net logfornet 配置和用法
  12. udp和tcp
  13. java.util.concurrent.Executors类的常用方法介绍
  14. WPF 10天修炼 第八天 - 形状、画刷和变换
  15. [Swift]LeetCode910. 最小差值 II | Smallest Range II
  16. 【译】使用 ndb 调试 node 应用
  17. VUE中v-on:click事件中获取当前dom元素
  18. libgdx学习记录13——矩形CD进度条绘制
  19. 【刷题】BZOJ 3252 攻略
  20. E 定向 牛客练习赛25

热门文章

  1. 关于文件属性(Java)
  2. es之路由:进一步提高Elasticsearch的检索效率(适用大规模数据集)
  3. antd form表单一行多个组件并对其校验
  4. mysql 安装相关
  5. maven 报错 Cannot resolve plugin org.apache.maven.plugins:maven-war-plugin:2.1.1
  6. 第四周总结&amp;实验报告二
  7. async await task.Result 卡死
  8. HDU6025 Coprime Sequence(gcd)
  9. p3863 序列
  10. spir 合并单元格